"use client"; import { useEffect, useState } from "react"; import { ChevronDown } from "lucide-react"; import { cn } from "@/lib/utils"; type Heading = { id: string; text: string; depth: 2 | 3 }; // Tracks which section is on screen, so the outline can highlight it function useActiveHeading(headings: Heading[]) { const [active, setActive] = useState(headings[0]?.id ?? null); useEffect(() => { const els = headings.map((h) => document.getElementById(h.id)).filter((el): el is HTMLElement => !!el); if (els.length === 0) return; const onScroll = () => { const line = window.innerHeight * 0.3; let current = els[0].id; for (const el of els) if (el.getBoundingClientRect().top <= line) current = el.id; setActive(current); }; onScroll(); window.addEventListener("scroll", onScroll, { passive: true }); return () => window.removeEventListener("scroll", onScroll); }, [headings]); return active; } function OutlineList({ headings, active, onPick }: { headings: Heading[]; active: string | null; onPick?: () => void }) { let section = 0; return (
    {headings.map((h) => { if (h.depth === 2) section += 1; const on = h.id === active; return (
  1. {h.depth === 2 && ( {String(section).padStart(2, "0")} )} {h.text}
  2. ); })}
); } const heading = "mb-3 flex items-center gap-2 font-mono text-xs font-medium uppercase tracking-[0.08em] text-foreground"; // The sticky "On this page" outline beside the article on wide screens export function TableOfContents({ headings }: { headings: Heading[] }) { const active = useActiveHeading(headings); return ( ); } // A collapsible outline at the top of the article on smaller screens export function InlineTableOfContents({ headings }: { headings: Heading[] }) { const [open, setOpen] = useState(false); const active = useActiveHeading(headings); return ( ); }