"use client"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@suphark/ui/components/ui/collapsible"; import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@suphark/ui/components/ui/dropdown-menu"; import { SidebarGroup, SidebarGroupLabel, SidebarMenu, SidebarMenuAction, SidebarMenuBadge, SidebarMenuButton, SidebarMenuItem, SidebarMenuSub, SidebarMenuSubButton, SidebarMenuSubItem, useSidebar, } from "@suphark/ui/components/ui/sidebar"; import { Tooltip, TooltipContent, TooltipTrigger } from "@suphark/ui/components/ui/tooltip"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronRight, FlaskConical, Pin } from "lucide-react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { type ReactNode, useEffect, useState } from "react"; import { type AppNavGroup, type AppNavItem, isNavActive } from "./types"; /** ปุ่ม pin — แสดงเมื่อแอปส่ง `pinnedHrefs` + `onTogglePin` (supplychain เก็บใน user preferences) */ export interface AppNavPinProps { pinnedHrefs?: string[]; onTogglePin?: (href: string) => void; pinLabel?: string; unpinLabel?: string; } function MenuTitle({ title, isLabs }: { title: string; isLabs?: boolean }) { return ( } > {title} {isLabs && } {title} ); } function PinAction({ href, pin, className, }: { href: string; pin: AppNavPinProps; className?: string; }) { if (!pin.onTogglePin) return null; const isPinned = pin.pinnedHrefs?.includes(href) ?? false; const label = isPinned ? (pin.unpinLabel ?? "เลิกปักหมุด") : (pin.pinLabel ?? "ปักหมุดที่ header"); return ( pin.onTogglePin?.(href)} title={label} // จอสัมผัสไม่มี hover: ปุ่มจางล่องหนแต่ยังแตะโดน (ปักหมุดโดยไม่รู้ตัว) → แสดงให้เห็นเสมอ className={cn("pointer-coarse:opacity-100", className)} > {label} ); } /** * กลุ่มเมนูใน sidebar (ยกจาก supplychain NavMain): 2 ชั้น, กางได้, ยุบเป็นไอคอนแล้วเป็น dropdown, * ไฮไลต์ตาม pathname (prefix match), ป้าย badge, ฟีเจอร์ labs, และ pin (ถ้าแอปส่งมา) */ export function AppNavGroupMenu({ group, pin = {}, forceOpen = false, }: { group: AppNavGroup; pin?: AppNavPinProps; /** กางทุกกลุ่มย่อย (ใช้ตอนค้นหาเมนู) */ forceOpen?: boolean; }) { const { state, isMobile, setOpenMobile } = useSidebar(); const pathname = usePathname(); const isCollapsed = state === "collapsed" && !isMobile; // บนมือถือ sidebar เป็น Sheet — กดลิงก์แล้วปิดให้ const closeMobile = () => setOpenMobile(false); const renderLeaf = (item: AppNavItem & { href: string }) => { const active = isNavActive(pathname, item.href, item.exact); return ( } > {item.icon} {item.badge !== undefined && {item.badge}} ); }; return ( {group.title && {group.title}} {group.items.map((item) => { const children = item.items ?? []; const hasChildren = children.length > 0; // ใบ (ไม่มีลูก) if (!hasChildren) { if (!item.href) return null; return renderLeaf({ ...item, href: item.href }); } const childActive = children.some((c) => isNavActive(pathname, c.href, c.exact)); // ยุบเป็นไอคอน + มีลูก → dropdown ด้านขวา if (isCollapsed) { return ( } > {item.icon} {item.title} {children.map((sub) => ( } > {sub.icon} ))} ); } // กางปกติ — เปิดค้างเมื่อลูกตัวใดตัวหนึ่ง active หรือกำลังค้นหา return ( } > {item.icon} {children.map((sub) => { if (!sub.href) return null; const active = isNavActive(pathname, sub.href, sub.exact); return ( } > {sub.icon} {sub.badge !== undefined && ( {sub.badge} )} ); })} ); })} ); } /** * กลุ่มย่อยที่กางได้: เปิดอัตโนมัติเมื่อ `autoOpen` เปลี่ยนเป็น true/false (ลูก active หรือกำลังค้นหา) * แต่ผู้ใช้ยังกาง/หุบเองได้ระหว่างนั้น — ควบคุม state เอง (เดิม remount ทั้ง subtree ด้วย key ทุกครั้งที่ค่าเปลี่ยน) */ function NavCollapsible({ autoOpen, children }: { autoOpen: boolean; children: ReactNode }) { const [open, setOpen] = useState(autoOpen); useEffect(() => { setOpen(autoOpen); }, [autoOpen]); return ( } open={open} onOpenChange={setOpen} className="group/collapsible" > {children} ); } /** ทุกกลุ่มเมนู */ export function AppNav({ nav, pin, forceOpen, }: { nav: AppNavGroup[]; pin?: AppNavPinProps; forceOpen?: boolean; }) { return ( <> {nav.map((group, i) => ( ))} ); }