'use client' /** * The page's conversations, folded onto the lines they were held at. * * Each thread is an orb in the right gutter beside the first line of its * block; a line with several holds them as a stack that fans out under the * pointer. The orb says what the conversation is doing — idle, still running, * failed, or answered since it was last read — and hovering it previews the * conversation, with open and delete one click away. * * Positions are measurements of the editor's DOM, recomputed when the * document, the layout or the thread list changes; nothing here is stored in * the page. */ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react' import { createPortal } from 'react-dom' import { useTranslation } from 'react-i18next' import { Sparkles, Trash2 } from 'lucide-react' import type { NoteThreadView } from '../model/threads' import { relativeTime } from '../model/threads' type Placement = { blockId: string; top: number } /** Where a block's first line sits, relative to the editor container. */ export function blockLineCenter(container: HTMLElement, blockId: string): number | null { const outer = container.querySelector( `.bn-block-outer[data-id="${CSS.escape(blockId)}"]` ) const content = outer?.querySelector('.bn-block-content') if (!content) return null const inline = content.querySelector('.bn-inline-content') const base = container.getBoundingClientRect().top if (inline) { const style = getComputedStyle(inline) const line = parseFloat(style.lineHeight) || parseFloat(style.fontSize) * 1.5 return inline.getBoundingClientRect().top - base + line / 2 } // Cards (learning, files, embeds) have no text line: align with their header. return content.getBoundingClientRect().top - base + 22 } export default function NoteThreadMarkers({ editor, container, threads, activeSessionId, activeBlockId, interactive, onOpen, onDelete, onAsk, }: { /** Anything that reports document changes; the measured layout follows them. */ editor: { onChange: (callback: () => void) => (() => void) | void } container: HTMLDivElement | null threads: NoteThreadView[] /** The thread open in the side panel; its orb is drawn as the open one. */ activeSessionId: string | null /** The line the open (maybe not yet saved) thread is about. */ activeBlockId: string | null interactive: boolean onOpen: (sessionId: string) => void onDelete: (sessionId: string) => Promise onAsk: (blockId: string) => void }) { const [placements, setPlacements] = useState([]) const [hoverBlock, setHoverBlock] = useState<{ blockId: string; top: number } | null>(null) const frame = useRef(0) const blockIds = [...new Set(threads.map(t => t.block_id))] const key = blockIds.join('|') const measure = useCallback(() => { cancelAnimationFrame(frame.current) frame.current = requestAnimationFrame(() => { if (!container) return const next: Placement[] = [] for (const blockId of key ? key.split('|') : []) { const top = blockLineCenter(container, blockId) if (top !== null) next.push({ blockId, top }) } setPlacements(current => current.length === next.length && current.every( (p, i) => p.blockId === next[i].blockId && Math.abs(p.top - next[i].top) < 0.5 ) ? current : next ) }) }, [container, key]) useLayoutEffect(() => { measure() }, [measure]) useEffect(() => { if (!container) return const unsubscribe = editor.onChange(measure) const observer = new ResizeObserver(measure) observer.observe(container) window.addEventListener('resize', measure) void document.fonts?.ready.then(measure) return () => { unsubscribe?.() observer.disconnect() window.removeEventListener('resize', measure) cancelAnimationFrame(frame.current) } }, [container, editor, measure]) // A faint "ask here" orb follows the pointer down lines with no thread yet. useEffect(() => { if (!container || !interactive) return const move = (event: PointerEvent) => { const target = event.target as HTMLElement if (target.closest('.dt-note-orbs, .dt-note-ask-orb')) return // The innermost block under the pointer is the line it means. const blockId = target.closest('.bn-block-outer')?.dataset.id if (!blockId) return const top = blockLineCenter(container, blockId) setHoverBlock(current => top === null ? null : current?.blockId === blockId && current.top === top ? current : { blockId, top } ) } const leave = () => setHoverBlock(null) container.addEventListener('pointermove', move) container.addEventListener('pointerleave', leave) return () => { container.removeEventListener('pointermove', move) container.removeEventListener('pointerleave', leave) } }, [container, interactive]) if (!container) return null const byBlock = new Map() for (const thread of threads) { const list = byBlock.get(thread.block_id) ?? [] list.push(thread) byBlock.set(thread.block_id, list) } const showAsk = hoverBlock && !byBlock.has(hoverBlock.blockId) && hoverBlock.blockId !== activeBlockId return (
{placements.map(({ blockId, top }) => ( ))} {showAsk && interactive && ( onAsk(hoverBlock.blockId)} /> )}
) } function AskOrb({ top, onAsk }: { top: number; onAsk: () => void }) { const { t } = useTranslation() return ( ) } function OrbStack({ top, threads, activeSessionId, open, interactive, onOpen, onDelete, }: { top: number threads: NoteThreadView[] activeSessionId: string | null open: boolean interactive: boolean onOpen: (sessionId: string) => void onDelete: (sessionId: string) => Promise }) { const { t } = useTranslation() const [anchor, setAnchor] = useState(null) const [hover, setHover] = useState(false) const leaveTimer = useRef | null>(null) const shown = threads.slice(-4) const hidden = threads.length - shown.length const enter = () => { if (leaveTimer.current) clearTimeout(leaveTimer.current) setHover(true) } const leave = () => { if (leaveTimer.current) clearTimeout(leaveTimer.current) leaveTimer.current = setTimeout(() => setHover(false), 160) } useEffect( () => () => { if (leaveTimer.current) clearTimeout(leaveTimer.current) }, [] ) const running = threads.some(thread => thread.state === 'running') return ( <>
{shown.map(thread => (
{hover && interactive && anchor && createPortal( { setHover(false) onOpen(sessionId) }} onDelete={onDelete} />, document.body )} ) } function ThreadPreview({ anchor, threads, onEnter, onLeave, onOpen, onDelete, }: { anchor: HTMLElement threads: NoteThreadView[] onEnter: () => void onLeave: () => void onOpen: (sessionId: string) => void onDelete: (sessionId: string) => Promise }) { const { t, i18n } = useTranslation() const [confirming, setConfirming] = useState(null) const [deleting, setDeleting] = useState(null) const rect = anchor.getBoundingClientRect() const width = 300 // Below the line, right-aligned to the orbs; flipped up near the bottom. const left = Math.max(12, Math.min(rect.right - width + 10, window.innerWidth - width - 12)) const below = rect.bottom + 8 const flip = below + 220 > window.innerHeight return (
{[...threads].reverse().map(thread => (
{confirming === thread.session_id ? ( ) : ( )}
))}
) } export function stateLabel(thread: NoteThreadView, t: (key: string) => string) { if (thread.state === 'running') return t('Running…') if (thread.state === 'failed') return t('Failed') return '' }