"use client" import { ChevronRightIcon } from "lucide-react" import Link from "next/link" import { usePathname } from "next/navigation" import { useEffect, useState } from "react" import { dashboardNavSectionHasActiveItem, getDashboardNavSectionStorageKey, isDashboardNavItemActive, parseDashboardNavSectionOpenState, } from "@/lib/navigation-active" import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible" import { SidebarGroup, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarMenuSub, SidebarMenuSubButton, SidebarMenuSubItem, } from "@/components/ui/sidebar" export function NavMain({ icon, sectionKey, title, items, }: { icon?: React.ReactNode sectionKey: string title: string items: ReadonlyArray<{ title: string url: string icon?: React.ReactNode }> }) { const pathname = usePathname() const hasActiveItem = dashboardNavSectionHasActiveItem(items, pathname) const storageKey = getDashboardNavSectionStorageKey(sectionKey) const [open, setOpen] = useState(() => { if (typeof window === "undefined") { return true } return parseDashboardNavSectionOpenState(window.localStorage.getItem(storageKey)) ?? true }) useEffect(() => { const storedOpen = parseDashboardNavSectionOpenState( window.localStorage.getItem(storageKey) ) if (storedOpen !== undefined) { setOpen(storedOpen) return } if (hasActiveItem) { setOpen(true) } }, [hasActiveItem, storageKey]) const handleOpenChange = (nextOpen: boolean) => { setOpen(nextOpen) window.localStorage.setItem(storageKey, String(nextOpen)) } return ( } > }> {icon} {title} {items.map((item) => ( } isActive={isDashboardNavItemActive(pathname, item.url)} > {item.title} ))} ) }