import React, { useState, useMemo, useCallback } from 'react'; import type { Block, Annotation } from '../types'; import { buildTocHierarchy, getAnnotationCountBySection, type TocItem, } from '../utils/annotationHelpers'; import { CountBadge } from './sidebar/CountBadge'; import { useScrollViewport } from '../hooks/useScrollViewport'; interface TableOfContentsProps { blocks: Block[]; annotations: Annotation[]; activeId: string | null; onNavigate: (blockId: string) => void; className?: string; style?: React.CSSProperties; linkedDocFilepath?: string | null; onLinkedDocBack?: () => void; backLabel?: string; } interface TocItemProps { item: TocItem; activeId: string | null; onNavigate: (blockId: string) => void; isExpanded: boolean; onToggle: () => void; hasChildren: boolean; } function TocItemComponent({ item, activeId, onNavigate, isExpanded, onToggle, hasChildren, }: TocItemProps) { const isActive = item.id === activeId; const indent = item.level === 1 ? 'pl-1.5' : item.level === 2 ? 'pl-4' : 'pl-6'; return (
  • {hasChildren && ( )}
    {hasChildren && isExpanded && ( )}
  • ); } function TocItemWithState({ item, activeId, onNavigate, }: { item: TocItem; activeId: string | null; onNavigate: (blockId: string) => void; }) { const [isExpanded, setIsExpanded] = useState(true); const hasChildren = item.children.length > 0; return ( setIsExpanded(!isExpanded)} hasChildren={hasChildren} /> ); } export function TableOfContents({ blocks, annotations, activeId, onNavigate, className = '', style, linkedDocFilepath, onLinkedDocBack, backLabel, }: TableOfContentsProps) { // Calculate annotation counts per section const annotationCounts = useMemo( () => getAnnotationCountBySection(blocks, annotations), [blocks, annotations] ); // Build hierarchical TOC structure const tocItems = useMemo( () => buildTocHierarchy(blocks, annotationCounts), [blocks, annotationCounts] ); // The real scroll element is the OverlayScrollArea viewport, not
    . const scrollViewport = useScrollViewport(); // Handle navigation with smooth scroll const handleNavigate = useCallback( (blockId: string) => { onNavigate(blockId); // Find target element and scroll to it. scrollViewport is the // OverlayScrollArea's internal viewport — querying for
    would // return the OverlayScrollArea host (not the scrolling element) and // silently produce wrong offsets. const target = document.querySelector(`[data-block-id="${blockId}"]`); if (target && scrollViewport) { const scrollContainer = scrollViewport; // Account for sticky header (48px = h-12) const headerOffset = 80; const containerRect = scrollContainer.getBoundingClientRect(); const targetRect = target.getBoundingClientRect(); const scrollTop = scrollContainer.scrollTop; const relativeTop = targetRect.top - containerRect.top; const offsetPosition = scrollTop + relativeTop - headerOffset; scrollContainer.scrollTo({ top: offsetPosition, behavior: 'smooth', }); } }, [onNavigate, scrollViewport] ); if (tocItems.length === 0) { return null; } return ( ); }