import { LOCAL_STORAGE_KEYS } from 'common' import { noop } from 'lodash' import Link from 'next/link' import { AnchorHTMLAttributes, cloneElement, ComponentPropsWithoutRef, forwardRef, isValidElement, } from 'react' import { cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui' import type { Route } from '@/components/ui/ui.types' import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage' interface NavigationIconButtonProps extends AnchorHTMLAttributes { route: Route isActive?: boolean } const NavigationIconLink = forwardRef( ({ route, isActive = false, onClick = noop, ...props }, ref) => { const [storedAllowNavPanel] = useLocalStorageQuery( LOCAL_STORAGE_KEYS.EXPAND_NAVIGATION_PANEL, true ) // Don't allow the nav panel to expand in playwright tests const allowNavPanelToExpand = process.env.NEXT_PUBLIC_NODE_ENV !== 'test' && storedAllowNavPanel const iconClasses = [ 'absolute left-0 top-0 flex rounded-sm h-10 w-10 items-center justify-center text-foreground-lighter', // Layout 'group-hover/item:text-foreground-light', isActive ? 'text-foreground! [&_svg]:stroke-[1.5]' : '[&_svg]:stroke-1', 'transition-all', ] const classes = [ 'relative', 'h-10 w-full md:w-10 md:group-data-[state=expanded]:w-full', 'transition-all duration-200', 'flex items-center rounded-sm', 'group-data-[state=collapsed]:justify-center', 'group-data-[state=expanded]:-space-x-2', 'hover:bg-surface-200', 'group/item', `${isActive && 'bg-selection! shadow-xs'}`, ] const LinkComponent = forwardRef>( function LinkComponent(props, ref) { if (route.linkElement && isValidElement(route.linkElement)) { return cloneElement(route.linkElement, { ...props, ref }) } return } ) const linkContent = ( { if (!route.link) { e.preventDefault() // Prevent navigation if there's no link } onClick(e) }} className={cn(classes, props.className)} > {route.icon} {route.label} ) if (!allowNavPanelToExpand) { return ( {linkContent} {route.label} ) } return linkContent } ) NavigationIconLink.displayName = 'NavigationIconLink' export default NavigationIconLink