import { useEffect, useMemo, useState } from 'react'; import { tr } from '../lib/i18n'; import type { TimeSkipFile } from '../lib/time-skip'; import { HTML_PREVIEW_SANDBOX, diffStats, fileKind, htmlPreviewDocument, needsOutsideFiles, lineDiff, segmentLines, timeSkipImageUrl, wordDiff, type Segment } from '../lib/time-skip-compare'; import { Btn, Dialog, Icon } from './ui'; const baseName = (path: string) => path.split('/').pop() || path; const statusText = (status: TimeSkipFile['status']) => tr(status === 'added' ? 'new' : status === 'removed' ? 'removed' : 'changed'); /** Lines of marked words laid out as the document they are: headings, list items, code fences and paragraphs (Markdown), or plain lines. */ function MarkedDoc({ segments, markdown }: { segments: Segment[]; markdown: boolean }) { const lines = segmentLines(segments); let fenced = false; return
{lines.map((line, i) => { const plain = line.map(s => s.text).join(''); const marks = (parts: Segment[]) => parts.map((s, k) => s.changed ? {s.text} : {s.text}); if (!markdown) return

{plain ? marks(line) :
}

; if (/^\s*```/.test(plain)) { fenced = !fenced; return

{plain ? marks(line) : null}

; } if (fenced) return

{plain ? marks(line) :
}

; const heading = /^(#{1,6})\s+/.exec(plain); if (heading) return

{marks(stripLead(line, heading[0].length))}

; const item = /^(\s*)([-*+]|\d+[.)])\s+/.exec(plain); if (item) return

{marks(stripLead(line, item[0].length))}

; return

{plain ? marks(line) :
}

; })}
; } /** Drop the first `count` characters (the Markdown marker) from the front of a line of segments. */ function stripLead(line: Segment[], count: number): Segment[] { const out: Segment[] = []; let left = count; for (const seg of line) { if (left >= seg.text.length) { left -= seg.text.length; continue; } out.push({ text: seg.text.slice(left), changed: seg.changed }); left = 0; } return out; } function DiffView({ original, updated }: { original: string; updated: string }) { const rows = useMemo(() => lineDiff(original, updated), [original, updated]); const stats = diffStats(rows); return
+{stats.added}−{stats.removed}
{rows.map((r, i) => r.t === 'gap' ?
{tr('{0} unchanged lines', [r.n])}
:
{r.text || ' '}
)}
; } function Picture({ src, label, missing, onOpen }: { src: string; label: string; missing?: string; onOpen: () => void }) { const [failed, setFailed] = useState(false); useEffect(() => { setFailed(false); }, [src]); if (missing || failed) return
{missing ?? tr("This picture could not be loaded.")}
; return ; } function FileView({ jobId, file }: { jobId: string; file: TimeSkipFile }) { const kind = fileKind(file.path, file); const [code, setCode] = useState(false); const [big, setBig] = useState<'original' | 'updated' | null>(null); useEffect(() => { setCode(false); setBig(null); }, [file.path]); const original = file.original ?? '', updated = file.updated ?? ''; const words = useMemo(() => (kind === 'markdown' || kind === 'text') ? wordDiff(original, updated) : null, [kind, original, updated]); const heads =
A · {tr("Original")}B · {tr("Updated")}
; if (kind === 'other') return

{tr("This file is too big, or not text, so it is listed but not shown.")}

; if (kind === 'image') { const a = timeSkipImageUrl(jobId, file.path, 'original'), b = timeSkipImageUrl(jobId, file.path, 'updated'); return <>{heads}
setBig('original')}/> setBig('updated')}/>
setBig(null)} sheet> {big ? {baseName(file.path)}/ : null} ; } if (kind === 'html') return <>
setCode(v => !v)}>{tr(code ? 'View preview' : 'View code')}
{code ? : <>{heads}
{/* The page may run its own scripts in an isolated frame; the policy inside blocks every outside request. */} {file.status === 'added' ?
{tr("Not there before.")}
: