'use client' import { useState, type ElementType, type ReactNode } from 'react' import { usePathname, useSearchParams } from 'next/navigation' import Link from 'next/link' import { ChevronLeft, ChevronRight } from 'lucide-react' export type SidebarSubItem = { name: string path: string } export type SidebarItem = { name: string path: string icon: ElementType status?: ReactNode items?: SidebarSubItem[] } type SidebarProps = { items: SidebarItem[] header?: ReactNode | ((expanded: boolean) => ReactNode) bottomAction?: (expanded: boolean) => ReactNode mobile?: boolean initialExpanded?: boolean onExpandedChange?: (expanded: boolean) => void className?: string } const ITEM_HEIGHT = 48 const GAP = 4 const SUB_STRIDE = 40 export default function Sidebar({ items, header, bottomAction, mobile = false, initialExpanded = true, onExpandedChange, className = '', }: SidebarProps) { const [expanded, setExpanded] = useState(initialExpanded) const pathname = usePathname() const searchParams = useSearchParams() const fullPath = pathname + (searchParams.toString() ? `?${searchParams.toString()}` : '') function toggleExpanded() { const next = !expanded setExpanded(next) onExpandedChange?.(next) } function isSubActive(sub: SidebarSubItem) { return sub.path.includes('?') ? fullPath.startsWith(sub.path) : pathname === sub.path } function isItemActive(item: SidebarItem) { return pathname === item.path || !!(item.items?.some(sub => sub.path.includes('?') ? fullPath.startsWith(sub.path) : pathname === sub.path || pathname.startsWith(sub.path + '/'))) } const activeIndex = items.findIndex(isItemActive) const activeOffset = items .slice(0, Math.max(activeIndex, 0)) .reduce((acc, item) => acc + ITEM_HEIGHT + (item.items ? GAP : 0) + GAP, 0) return ( ) }