import { useCallback, useMemo, useState } from 'react' import { CaretDown, CaretRight, ListBullets, X } from '@phosphor-icons/react' import { Button } from '@/components/ui/button' import { cn } from '@/lib/utils' import { useDragRegion } from '../hooks/useDragRegion' import { translate, type AppLocale } from '../lib/i18n' import type { VaultEntry } from '../types' import { extractTableOfContents, type TableOfContentsItem } from '../utils/tableOfContents' interface TableOfContentsEditor { document: unknown focus: () => void setTextCursorPosition: (blockId: string, placement: 'start' | 'end') => void } interface TableOfContentsPanelProps { activeEntry: VaultEntry | null documentRevision: number editor: TableOfContentsEditor locale?: AppLocale onClose: () => void onHeadingSelected?: (item: TableOfContentsItem) => void } const EMPTY_COLLAPSED_IDS = new Set() function headingLabel(item: TableOfContentsItem, locale: AppLocale): string { return item.text || translate(locale, 'tableOfContents.untitledHeading') } function findBlockElement(blockId: string): HTMLElement | null { const candidates = document.querySelectorAll('[data-id], [data-block-id]') for (const candidate of candidates) { if (candidate.dataset.id === blockId) return candidate if (candidate.dataset.blockId === blockId) return candidate } return null } function scrollHeadingIntoView(blockId: string) { findBlockElement(blockId)?.scrollIntoView({ block: 'start', behavior: 'smooth' }) } function EmptyTableOfContents({ children }: { children: string }) { return (
{children}
) } function TableOfContentsHeader({ locale, onClose, }: { locale: AppLocale onClose: () => void }) { const { onMouseDown } = useDragRegion() return (

{translate(locale, 'tableOfContents.title')}

) } function ToggleChildrenButton({ collapsed, item, locale, onToggle, }: { collapsed: boolean item: TableOfContentsItem locale: AppLocale onToggle: (itemId: string) => void }) { const label = headingLabel(item, locale) return ( ) } function TogglePlaceholder() { return