"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { ChevronDownIcon, HamburgerIcon } from "@/components/icons"; import { Sheet, SheetTrigger, SheetContent, SheetTitle } from "@/components/ui/sheet"; import type { DocsGroup } from "@/lib/types"; const COLLAPSE_STORAGE_KEY = "docs-sidebar-collapsed"; type SidebarNavProps = { groups: DocsGroup[]; activeSlug: string; onNavigate?: () => void; }; function SidebarNav({ groups, activeSlug, onNavigate }: SidebarNavProps) { const initialCollapsed = new Set( groups .filter((g) => g.section !== "Start Here" && !g.items.some((i) => i.slug === activeSlug)) .map((g) => g.section), ); const [collapsed, setCollapsed] = useState>(initialCollapsed); const restored = useRef(false); // Restore the user's expand/collapse choices so navigating between pages // does not reset sections the user opened. (The active section is always // shown expanded via the `hasActive` check below, regardless of this set.) useEffect(() => { if (restored.current) return; restored.current = true; try { const stored = sessionStorage.getItem(COLLAPSE_STORAGE_KEY); if (stored) setCollapsed(new Set(JSON.parse(stored) as string[])); } catch {} }, []); function toggle(section: string) { setCollapsed((prev) => { const next = new Set(prev); if (next.has(section)) next.delete(section); else next.add(section); try { sessionStorage.setItem(COLLAPSE_STORAGE_KEY, JSON.stringify([...next])); } catch {} return next; }); } return ( ); } type DocsSidebarShellProps = { groups: DocsGroup[]; }; function activeDocForPath(groups: DocsGroup[], pathname: string) { const normalized = pathname.replace(/\/$/, "") || "/"; return groups.flatMap((group) => group.items).find((item) => item.path === normalized); } export function DocsSidebarShell({ groups }: DocsSidebarShellProps) { const pathname = usePathname(); const activeDoc = activeDocForPath(groups, pathname); const activeSlug = activeDoc?.slug ?? ""; const currentTitle = activeDoc?.title; const [open, setOpen] = useState(false); return ( <> {currentTitle ?? "Documentation"} Table of Contents setOpen(false)} /> ); }