'use client'; import { useEffect, useState } from 'react'; import { t } from '@/i18n'; import { cn } from '@/lib/cn'; /** Sticky table of contents (desktop) with the current section highlighted via IntersectionObserver. */ export function Toc({ items }: { items: Array<{ id: string; label: string }> }) { const [active, setActive] = useState(items[0]?.id ?? null); useEffect(() => { if (typeof IntersectionObserver === 'undefined') return; const els = items.map((i) => document.getElementById(i.id)).filter((e): e is HTMLElement => !!e); const io = new IntersectionObserver( (entries) => { const vis = entries.filter((e) => e.isIntersecting).sort((a, b) => a.boundingClientRect.top - b.boundingClientRect.top); if (vis[0]) setActive(vis[0].target.id); }, { rootMargin: '-96px 0px -60% 0px', threshold: 0 }, ); els.forEach((e) => io.observe(e)); return () => io.disconnect(); }, [items]); return ( ); }