'use client'; import React, { useEffect, useState } from 'react'; import { cn } from '@/components/ui/utils'; interface TocItem { id: string; text: string; level: number; } interface TableOfContentsProps { headings: TocItem[]; locale: string; } export default function TableOfContents({ headings, locale }: TableOfContentsProps) { const [activeId, setActiveId] = useState(''); useEffect(() => { const observerOptions = { rootMargin: '-100px 0% -80% 0%', threshold: 0 }; const observer = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { setActiveId(entry.target.id); } }); }, observerOptions); const elements = headings.map((h) => document.getElementById(h.id)); elements.forEach((el) => { if (el) observer.observe(el); }); return () => observer.disconnect(); }, [headings]); if (headings.length === 0) return null; return ( ); }