import { useState, useEffect } from 'react'; import { Icon } from '../shared/Icon'; interface TocItem { id: string; title: string; icon: string; } interface DocTableOfContentsProps { items: TocItem[]; roleLabel: string; roleIcon: string; roleColor: string; } export function DocTableOfContents({ items, roleLabel, roleIcon, roleColor }: DocTableOfContentsProps) { const [activeId, setActiveId] = useState(items[0]?.id ?? ''); useEffect(() => { const observer = new IntersectionObserver( (entries) => { const visible = entries.filter((e) => e.isIntersecting); if (visible.length > 0) { setActiveId(visible[0].target.id); } }, { rootMargin: '-80px 0px -60% 0px', threshold: 0.1 } ); items.forEach(({ id }) => { const el = document.getElementById(id); if (el) observer.observe(el); }); return () => observer.disconnect(); }, [items]); const scrollTo = (id: string) => { document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'start' }); }; return (