"use client"; import { useEffect, useState } from "react"; import type { Heading } from "@/lib/posts"; interface TableOfContentsProps { headings: Heading[]; } export default function TableOfContents({ headings }: TableOfContentsProps) { const [activeSlug, setActiveSlug] = useState(""); useEffect(() => { if (headings.length === 0) return; const observer = new IntersectionObserver( (entries) => { for (const entry of entries) { if (entry.isIntersecting) { setActiveSlug(entry.target.id); } } }, { rootMargin: "0% 0% -70% 0%", threshold: 0 } ); headings.forEach(({ slug }) => { const el = document.getElementById(slug); if (el) observer.observe(el); }); return () => observer.disconnect(); }, [headings]); if (headings.length === 0) return null; return ( ); }