"use client"; import { ChevronDown, ChevronUp, PanelRight, MoreVertical, FileText } from "lucide-react"; import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTrigger } from "@/components/ui/sheet"; import DocsMenu from "@/components/DocsMenu"; import { ModeToggle } from "@/components/ThemeToggle"; import { DialogTitle, DialogDescription } from "@/components/ui/dialog"; import ContextPopover from "@/components/ContextPopover"; import TocObserver from "./TocObserver"; import * as React from "react"; import { useRef, useMemo } from "react"; import { usePathname } from "next/navigation"; import { Button } from "./ui/button"; import { useActiveSection } from "@/hooks"; import { TocItem } from "@/lib/toc"; import Search from "@/components/SearchBox"; import GitHubButton from "@/components/Github"; import { NavMenu } from "@/components/navbar"; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; interface MobTocProps { tocs: TocItem[]; title?: string; } const subscribe = () => () => undefined; const getMountedSnapshot = () => true; const getServerSnapshot = () => false; const useClickOutside = (ref: React.RefObject, callback: () => void) => { const callbackRef = useRef(callback); React.useEffect(() => { callbackRef.current = callback; }); React.useEffect(() => { const handleClick = (event: MouseEvent) => { if (ref.current && !ref.current.contains(event.target as Node)) { callbackRef.current(); } }; document.addEventListener("mousedown", handleClick); return () => document.removeEventListener("mousedown", handleClick); }, [ref]); }; export default function MobToc({ tocs, title }: MobTocProps) { const pathname = usePathname(); const [isExpanded, setIsExpanded] = React.useState(false); const tocRef = useRef(null); const contentRef = useRef(null); // Use custom hooks const { activeId, setActiveId } = useActiveSection(tocs); // Only show on /docs pages const isDocsPage = useMemo(() => pathname?.startsWith("/docs"), [pathname]); // Get title from active section if available, otherwise document title const activeSection = useMemo(() => { return tocs.find((toc) => toc.href.slice(1) === activeId); }, [tocs, activeId]); const displayTitle = activeSection?.text || title || "On this page"; const mounted = React.useSyncExternalStore(subscribe, getMountedSnapshot, getServerSnapshot); // Toggle expanded state const toggleExpanded = React.useCallback((e: React.MouseEvent) => { e.stopPropagation(); setIsExpanded((prev) => !prev); }, []); // Close TOC when clicking outside useClickOutside(tocRef, () => { if (isExpanded) { setIsExpanded(false); } }); // Don't render anything if not on docs page if (!isDocsPage || !mounted) return null; const chevronIcon = isExpanded ? ( ) : ( ); return (
Navigation Menu Main navigation menu with links to different sections
{tocs?.length ? ( ) : (

No headings

{`This page doesn't have section headings yet.`}

)}
); }