"use client"; import { cn } from "@/lib/utils"; import { ListCollapse, ChevronRight } from "lucide-react"; import Link from "next/link"; import { useEffect, useState } from "react"; export interface TocItem { id: string; title: string; level: number; } interface TableOfContentsProps { items?: TocItem[]; className?: string; } export default function TableOfContents({ items = [], className, }: TableOfContentsProps) { const [activeId, setActiveId] = useState(""); useEffect(() => { if (items.length === 0) return; const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { setActiveId(entry.target.id); } }); }, { rootMargin: "0% 0% -70% 0%" }, ); items.forEach((item) => { const el = document.getElementById(item.id); if (el) observer.observe(el); }); return () => observer.disconnect(); }, [items]); if (!items || items.length === 0) return null; return ( ); }