import { Link, useLocation } from 'react-router-dom' import type { LucideIcon } from 'lucide-react' import { cn } from '@/lib/utils' import { prefetchForRoute } from '@/lib/routePrefetch' interface NavSubItem { href: string label: string count?: number isActive?: boolean } interface NavItemProps { href: string icon: LucideIcon label: string badge?: number | 'dot' iconColor?: string matchPaths?: string[] collapsed?: boolean children?: NavSubItem[] } export function NavItem({ href, icon: Icon, label, badge, iconColor, matchPaths, collapsed, children }: NavItemProps) { const location = useLocation() const fullPath = location.pathname + location.search const isActive = matchPaths ? matchPaths.some(p => location.pathname.startsWith(p)) : href === '/' ? location.pathname === '/' : location.pathname.startsWith(href) // Check if any child is specifically active const activeChild = children?.find(c => fullPath === c.href || fullPath.startsWith(c.href + '&')) const isParentDimmed = !!activeChild && isActive if (collapsed) { return ( prefetchForRoute(href)} className={cn( 'group relative flex items-center justify-center rounded-lg p-2 transition-all duration-120', isActive ? 'bg-[var(--sidebar-active)] text-foreground' : 'text-muted-foreground hover:bg-[var(--sidebar-hover)] hover:text-foreground' )} title={label} > {isActive && (
)} {badge !== undefined && badge !== 0 && badge !== 'dot' && ( {badge} )} ) } return (
prefetchForRoute(href)} className={cn( 'group relative flex items-center gap-3 rounded-lg px-3 py-2 text-[0.8125rem] font-medium transition-all duration-120', isActive ? isParentDimmed ? 'bg-[var(--sidebar-active)]/50 text-foreground/70' : 'bg-[var(--sidebar-active)] text-foreground' : 'text-muted-foreground hover:bg-[var(--sidebar-hover)] hover:text-foreground' )} > {/* Active indicator bar */} {isActive && !isParentDimmed && (
)} {label} {/* Badge */} {badge !== undefined && badge !== 0 && ( badge === 'dot' ? ( ) : ( {badge} ) )} {/* Sub-items — visible on hover or when a child is active */} {children && children.length > 0 && (
{children.map(child => { const childActive = fullPath === child.href || fullPath.startsWith(child.href + '&') return ( {child.label} {child.count !== undefined && ( {child.count} )} ) })}
)}
) }