/* global React, ReactDOM, SparkTrj, StripTrj, LedgerTrj */ // citation.jsx — citation chip + drawer // Exposes window.Citation, window.CitationProvider, window.useCitation const { createContext, useContext, useState, useEffect, useCallback } = React; const CitationCtx = createContext(null); function CitationProvider({ children }) { const [openKey, setOpenKey] = useState(null); const open = useCallback((key) => setOpenKey(key), []); const close = useCallback(() => setOpenKey(null), []); useEffect(() => { function onKey(e) { if (e.key === "Escape") setOpenKey(null); } window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, []); const tx = openKey ? window.CCT_TRANSCRIPTS[openKey] : null; return ( {children}
); } function renderHighlighted(body, highlight) { if (!highlight) return body; const idx = body.indexOf(highlight); if (idx < 0) return body; return ( <> {body.slice(0, idx)} {highlight} {body.slice(idx + highlight.length)} ); } function useCitation() { return useContext(CitationCtx); } // Citation chip function Citation({ chunk, label, ck }) { const ctx = useCitation(); const isOpen = ctx && ctx.openKey === ck; return ( ); } Object.assign(window, { CitationProvider, Citation, useCitation });