import { useEffect, useRef, useState } from 'react'; interface ReportNavProps { sections: { id: string; label: string }[]; } export function ReportNav({ sections }: ReportNavProps) { const [activeId, setActiveId] = useState(sections[0]?.id ?? ''); const navRef = useRef(null); const tabRefs = useRef>(new Map()); useEffect(() => { const observer = new IntersectionObserver( (entries) => { const visible = entries .filter((e) => e.isIntersecting) .sort((a, b) => a.boundingClientRect.top - b.boundingClientRect.top); if (visible.length > 0) { setActiveId(visible[0].target.id); } }, { rootMargin: '-100px 0px -60% 0px', threshold: 0 } ); sections.forEach(({ id }) => { const el = document.getElementById(id); if (el) observer.observe(el); }); return () => observer.disconnect(); }, [sections]); useEffect(() => { const activeTab = tabRefs.current.get(activeId); if (activeTab && navRef.current) { activeTab.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center', }); } }, [activeId]); const handleClick = (id: string) => { const el = document.getElementById(id); if (el) { el.scrollIntoView({ behavior: 'smooth' }); } }; return ( ); }