Something went wrong. Try again.
Fork of backnotprop/plannotator with reduced surface area
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248import 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 ( <li className="list-none"> <div className="flex items-start group"> {hasChildren && ( <button type="button" onClick={(e) => { e.stopPropagation(); onToggle(); }} className="flex-shrink-0 w-4 h-4 mr-0.5 mt-0.5 flex items-center justify-center hover:bg-muted rounded transition-colors" aria-label={isExpanded ? 'Collapse section' : 'Expand section'} > <svg className={`w-3 h-3 transition-transform ${isExpanded ? 'rotate-0' : '-rotate-90'}`} fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true" > <title>{isExpanded ? 'Collapse' : 'Expand'}</title> <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" /> </svg> </button> )} <button type="button" onClick={() => onNavigate(item.id)} className={` flex-1 text-left text-xs py-0.5 px-1.5 rounded transition-colors ${indent} ${hasChildren ? '' : 'ml-5'} ${ isActive ? 'text-primary bg-primary/10' : 'text-foreground/80 hover:text-foreground hover:bg-muted/50' } `} aria-current={isActive ? 'location' : undefined} > <span className="flex items-center justify-between gap-2"> <span className="flex-1 line-clamp-2 leading-normal"> {item.content} </span> {item.annotationCount > 0 && ( <CountBadge count={item.annotationCount} active={isActive} /> )} </span> </button> </div>
{hasChildren && isExpanded && ( <ul className="mt-0.5 space-y-0.5"> {item.children.map((child) => ( <TocItemWithState key={child.id} item={child} activeId={activeId} onNavigate={onNavigate} /> ))} </ul> )} </li> );}
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 ( <TocItemComponent item={item} activeId={activeId} onNavigate={onNavigate} isExpanded={isExpanded} onToggle={() => 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 <main>. 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 <main> 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 ( <nav // Use ?? not || — an explicit empty string from a caller means "I'm // managing my own container styling" (e.g. SidebarContainer wrapping // us in an OverlayScrollArea), which should NOT trigger the default. className={className ?? "bg-card/50 backdrop-blur-sm border-r border-border overflow-y-auto"} aria-label="Table of contents" style={style} > <div className="px-3 py-2"> {linkedDocFilepath && ( <div className="mb-2 pb-1.5 border-b border-border/50"> <div className="flex items-center justify-between"> <span className="text-[10px] font-medium text-primary/80">Viewing</span> {onLinkedDocBack && ( <button onClick={onLinkedDocBack} className="flex items-center gap-0.5 text-[10px] font-medium text-primary hover:text-primary/80 transition-colors" > <svg className="w-2.5 h-2.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5}> <path strokeLinecap="round" strokeLinejoin="round" d="M10.5 19.5L3 12m0 0l7.5-7.5M3 12h18" /> </svg> Back to {backLabel || 'plan'} </button> )} </div> <p className="text-[11px] text-foreground/70 truncate mt-0.5" title={linkedDocFilepath}> {linkedDocFilepath.split('/').pop()} </p> </div> )} <h2 className="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground mb-1.5"> Contents </h2> <ul className="space-y-0.5"> {tocItems.map((item) => ( <TocItemWithState key={item.id} item={item} activeId={activeId} onNavigate={handleNavigate} /> ))} </ul> </div> </nav> );}