import { EachRoute } from "@/lib/routes"; import Anchor from "./anchor"; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"; import { cn } from "@/lib/utils"; import { SheetClose } from "@/components/ui/sheet"; import { ChevronDown, ChevronRight } from "lucide-react"; import { useState, useMemo } from "react"; import { usePathname } from "next/navigation"; interface SubLinkProps extends EachRoute { level: number; isSheet: boolean; parentHref?: string; } export default function SubLink({ title, href, items, noLink, level, isSheet, parentHref = "", }: SubLinkProps) { const path = usePathname(); // Full path including parent's href const fullHref = parentHref ? `${parentHref}${href}` : `/docs${href}`; const shouldBeOpen = level === 0 || (!!items && path.startsWith(fullHref) && path !== fullHref); const [isOpen, setIsOpen] = useState(shouldBeOpen); // Check if any child is active (for parent items) const hasActiveChild = useMemo(() => { if (!items) return false; return items.some((item) => { const childHref = `${fullHref}${item.href}`; return path.startsWith(childHref) && path !== fullHref; }); }, [items, path, fullHref]); // Sync open state when path changes (expand if child becomes active) if (shouldBeOpen && !isOpen) { setIsOpen(true); } // Only apply active styles if it's an exact match and not a parent with active children const Comp = useMemo( () => ( {title} ), [title, fullHref, hasActiveChild, level] ); const titleOrLink = !noLink ? ( isSheet ? ( {Comp} ) : ( Comp ) ) : (

{title}

); if (!items) { return
{titleOrLink}
; } return (
{titleOrLink} {!isOpen ? (
0 && "border-border ml-1.5 border-l pl-4" )} > {items?.map((innerLink) => ( ))}
); }