import { cn } from '@/lib/utils'; import { useEffect, useState } from 'react'; import type { TocEntry } from '@/lib/types'; export interface PageLinksProps { items?: TocEntry[]; title: string; navigationLabel: string; } export function PageLinks({ items = [], title, navigationLabel }: PageLinksProps) { const [hash, setHash] = useState(items?.[0]?.id); useEffect(() => { setHash(items?.[0]?.id); const callback = () => { const found = [...items].reverse().find(({ id }) => { const rect = document.getElementById(id)?.getBoundingClientRect(); return rect && rect.top <= 0; }); if (found) { setHash(found.id); } }; window.addEventListener('scroll', callback, false); return () => { window.removeEventListener('scroll', callback, false); }; }, [items]); if (!items?.length) { return null; } const indent = (size: number) => { if (size <= 2) { return '0px'; } return `${(size - 2) * 10}px`; }; return (
{title}
); }