"use client"; import { useEffect, useRef, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; import { edgeClock } from "@/lib/report/clipEditorModel.mjs"; import type { BlurDefaults, ClipAudio, ClipBlur, KnobDefaults, MarkVerdict, MarkView } from "@/lib/report/clipEditorTypes"; // THE CLIP'S KNOBS (video knobs, plans/video-knobs-ui.md), in the clip editor: // // Marks on every clip (plans/clip-marks.md): its marks, each its // own stamp and tally step -- the verdict (this cut's words), // the source second it lands and the tally steps (`at`, Set // at playhead, Shift+S for the selected mark), how long it // stays (`hold`), whether it is drawn (`stamp`), how much it // counts (`count`), what it says (`label`), the day the // event happened when not the clip's (`eventDate`), remove; Add mark // at playhead (Shift+M); Clear (every mark) // Audio the clip's sound at the encode: a sync nudge in ms (+ = // later), gain in dB, fades in s; "listen" replays the 3 s // around the playhead // Blur the clip's PICTURE blurred at the encode (report-to-video // clip-blur.mjs), its sound untouched: on/off, the caption // drawn on it, its strength, and "only from / to" at the // playhead (a partial span; Whole clip clears it) // // Every field shows the default it gets when its key is absent; Clear removes // the key (the default again). Each change is one write through the clip // editor's route (op marks / op claim / op audio), the same writers `umtool // set` uses. const input = "num w-[6em] rounded border border-seam-2 bg-ground px-1.5 py-0.5 font-mono text-[12px] text-text outline-none focus:border-signal"; const box = "space-y-1.5 rounded-[3px] border border-seam bg-panel p-2 text-[12px] text-text-2"; type Write = (body: Record, done?: string) => Promise; /** A field that writes on Enter or blur; empty clears it. */ function KnobField({ name, label, value, placeholder, unit, kind = "number", step, maxLength, busy, onSave, }: { name: string; label: string; value: number | string | undefined; placeholder: string; unit?: string; kind?: "number" | "text"; step?: number; maxLength?: number; busy: boolean; onSave: (v: string) => void; }) { const shown = value === undefined ? "" : String(value); const [text, setText] = useState(shown); useEffect(() => setText(shown), [shown]); const commit = () => { if (text.trim() === shown) return; onSave(text.trim()); }; return ( ); } const numOrNull = (s: string) => (s === "" ? null : Number(s)); /** * Where a key is typing, not a command: the clip editor's rule, less a select * and a checkbox -- Shift+S / Shift+M still work just after a verdict is * picked or the stamp toggled (the word buttons never take the focus). */ const TYPING = "input:not([type='checkbox']), textarea, [contenteditable=''], [contenteditable='true']"; const r2 = (n: number) => Math.round(n * 100) / 100; /** A verdict's words for the picker: this cut's label, and the verdict's name when the label is not it. */ const verdictText = (v: MarkVerdict) => (v.label.toUpperCase().replace(/[\s-]+/g, "_") === v.verdict ? v.label : `${v.label} (${v.verdict})`); function VerdictPicker({ name, value, verdicts, busy, onPick, }: { name: string; value: string; verdicts: MarkVerdict[]; busy: boolean; onPick: (v: string) => unknown; }) { const stock = verdicts.filter((v) => v.stock); const other = verdicts.filter((v) => !v.stock); const known = verdicts.some((v) => v.verdict === value); // The choice shows at once until the view comes back with it (or does not). const [draft, setDraft] = useState(null); useEffect(() => setDraft(null), [value]); return ( ); } /** "draw the stamp": shows the choice at once (a draft) until the view comes back with it. */ function StampToggle({ on, busy, onSet }: { on: boolean; busy: boolean; onSet: (on: boolean) => Promise }) { const [draft, setDraft] = useState(null); useEffect(() => setDraft(null), [on]); return ( ); } /** The verdicts when the view names none (an older server): the five, by name. */ const FALLBACK_VERDICTS: MarkVerdict[] = ["CORROBORATED", "PARTLY", "CONTRADICTED", "NOT_FOUND", "UNTESTABLE"].map((v) => ({ verdict: v, label: v, stampLabel: v, color: "currentColor", stock: false, })); /** * THE MARKS of one clip (plans/clip-marks.md): a list, each mark its own stamp * and tally step. One mark is selected (click its row): Shift+S puts its * moment at the playhead. Shift+M adds a mark at the playhead with the * verdict beside "Add mark at playhead". The box keeps its own keys (the * editor's own key handler leaves Shift+S and Shift+M to it). */ export function MarksBox({ clipId, marks, defaults, playhead, span, busy, write, onGo, }: { clipId: string; marks: MarkView[]; defaults: KnobDefaults | undefined; playhead: number | null; span: { start: number; end: number; cutStart: number; cutEnd: number }; busy: boolean; write: Write; onGo: (id: string) => void; }) { const verdicts = defaults?.verdicts?.length ? defaults.verdicts : FALLBACK_VERDICTS; const hold = defaults?.hold ?? 3; const [selected, setSelected] = useState(marks[0]?.id ?? null); const [hint, setHint] = useState(null); // A new clip: its first mark selected. A selected mark removed: the first. useEffect(() => { setSelected(marks[0]?.id ?? null); setHint(null); // eslint-disable-next-line react-hooks/exhaustive-deps }, [clipId]); useEffect(() => { if (selected !== null && !marks.some((m) => m.id === selected)) setSelected(marks[0]?.id ?? null); if (selected === null && marks.length) setSelected(marks[0].id); }, [marks, selected]); const current = marks.find((m) => m.id === selected) ?? null; const [addVerdict, setAddVerdict] = useState(verdicts.find((v) => v.stock)?.verdict ?? verdicts[0].verdict); // The verdict a new mark takes follows the selected mark's. useEffect(() => { if (current) setAddVerdict(current.verdict); }, [current?.verdict]); // eslint-disable-line react-hooks/exhaustive-deps const setKnobs = (id: string, knobs: Record, done: string) => void write({ op: "claim", clip: clipId, mark: id, knobs }, done); const addAtPlayhead = () => { if (playhead === null) { setHint("put the playhead where the mark should land first (play, or click the strip)"); return; } if (marks.length >= 8) { setHint("a clip carries at most 8 marks"); return; } setHint(null); const at = r2(playhead); void write({ op: "marks", clip: clipId, add: { verdict: addVerdict, at } }, `Mark added at ${edgeClock(at)}`).then((r) => { const added = (r as { result?: { added?: string } } | null)?.result?.added; if (added) setSelected(added); }); }; const atPlayhead = () => { if (!current) { setHint("no mark to move: add one at the playhead first (⇧M)"); return; } if (playhead === null) { setHint("put the playhead where the stamp should land first"); return; } setHint(null); setKnobs(current.id, { at: r2(playhead) }, `${current.id} lands at ${edgeClock(playhead)}`); }; // Shift+S (the selected mark's moment at the playhead) and Shift+M (a mark // added at the playhead). S and M alone stay Split and Merge. const keys = useRef<(e: KeyboardEvent) => void>(() => {}); keys.current = (e: KeyboardEvent) => { if (e.defaultPrevented || e.metaKey || e.ctrlKey || e.altKey || !e.shiftKey) return; const t = e.target as HTMLElement | null; if (t?.closest?.(TYPING) || t?.closest?.("dialog[open]")) return; const k = e.key.toLowerCase(); if (k === "s") { e.preventDefault(); atPlayhead(); } else if (k === "m") { e.preventDefault(); addAtPlayhead(); } }; useEffect(() => { const on = (e: KeyboardEvent) => keys.current(e); document.addEventListener("keydown", on); return () => document.removeEventListener("keydown", on); }, []); const timedAny = marks.some((m) => typeof m.at === "number"); return (

Marks

{marks.length ? "each its own stamp and count" : "none: this clip stamps nothing and counts nothing"} {marks.length > 0 && ( )}
{marks.map((m, i) => { const on = m.id === selected; const word = m.label ?? defaults?.labels?.[m.verdict] ?? m.verdict; const outsideCut = typeof m.at === "number" && (m.at < span.cutStart || m.at > span.cutEnd); return (
setSelected(m.id)} className={`space-y-1 rounded-[3px] border px-1.5 py-1 ${on ? "border-signal" : "border-seam"}`} >
{i + 1} write({ op: "marks", clip: clipId, mark: m.id, verdict: v }, `${m.id} is ${verdicts.find((x) => x.verdict === v)?.label ?? v}`)} /> {m.id} lands at {typeof m.at === "number" ? edgeClock(m.at) : i < marks.length - 1 ? "default (before the next)" : "default (before the clip leaves)"} {typeof m.at === "number" && ( )}
setKnobs(m.id, { hold: numOrNull(v) }, v ? `${m.id}'s stamp stays ${v} s` : `${m.id}'s stamp stays the cut's default`)} /> write({ op: "claim", clip: clipId, mark: m.id, knobs: { stamp: on ? null : false } }, on ? `${m.id}: stamp drawn` : `${m.id}: counts, no stamp`)} /> setKnobs(m.id, { count: numOrNull(v) }, v ? `${m.id} counts ${v}` : `${m.id} counts 1`)} /> setKnobs(m.id, { label: v || null }, v ? `${m.id}'s stamp says “${v}”` : `${m.id}'s stamp says the verdict's word`)} /> {/* The day the event happened, when not the clip's: a deck claim's window dates it so. */} setKnobs(m.id, { eventDate: v || null }, v ? `${m.id} happened ${v}` : `${m.id} happened on its clip's day`)} />
{m.stampsOn && m.stampsOn !== clipId && (

{m.id} stamps on{" "} {" "} (the last clip carrying it) — its knobs are read there

)} {outsideCut && (

{edgeClock(m.at as number)} is outside the cut ({edgeClock(span.cutStart)}–{edgeClock(span.cutEnd)}): the stamp would land on footage the cut does not play.

)}
); })} {hint && (

{hint}

)} {timedAny && (

The On-screen preview places a mark’s second from the last build’s cut of the clip: it is approximate until the next build.

)} {marks.length > 0 && defaults && !defaults.stampShown &&

The cut draws no stamps (the fact-check’s stamp effect is off): the counts still step here.

}
); } const AUDIO_NUDGES = [-40, -10, 10, 40] as const; export function AudioBox({ clipId, audio, busy, write, canListen, onListen, }: { clipId: string; audio: ClipAudio | null; busy: boolean; write: Write; canListen: boolean; onListen: () => void; }) { const a = audio ?? {}; const set = (patch: Record | null, done: string) => void write({ op: "audio", clip: clipId, audio: patch }, done); const offset = a.offset ?? 0; return (

Audio

applied when the clip is encoded; the player plays the source as it is {audio && ( )}
set({ offset: numOrNull(v) }, v ? `Sound moved ${v} ms` : "Sound in sync as recorded")} /> {AUDIO_NUDGES.map((d) => ( ))} set({ gain: numOrNull(v) }, v ? `Gain ${v} dB` : "Gain as recorded")} /> set({ fadeIn: numOrNull(v) }, v ? `Fades in over ${v} s` : "No fade in")} /> set({ fadeOut: numOrNull(v) }, v ? `Fades out over ${v} s` : "No fade out")} />
); } /** * The clip's picture, blurred at the encode (clip-blur.mjs): for a clip whose * footage a platform's graphic-content rules would refuse. The sound plays as * it is; the caption is drawn on the blurred picture. The player above shows * the source as it is (you are cutting it); the Live preview and the render * show it blurred. */ export function BlurBox({ clipId, blur, defaults, playhead, span, busy, write, }: { clipId: string; blur: ClipBlur | null; defaults: BlurDefaults | undefined; playhead: number | null; /** The clip's window (source seconds): a partial blur's from/to lie inside it. */ span: { start: number; end: number }; busy: boolean; write: Write; }) { const set = (patch: Record | null, done: string) => write({ op: "blur", clip: clipId, blur: patch }, done); // The checkbox shows the click at once (onDraft) until the view comes back: // the saved state then, which is the old one again when the write is refused // (its sentence in the editor's error line). const [onDraft, setOnDraft] = useState<{ clip: string; on: boolean } | null>(null); const checked = onDraft?.clip === clipId ? onDraft.on : !!blur; const toggle = (on: boolean) => { setOnDraft({ clip: clipId, on }); void set(on ? { on: true } : null, on ? "The picture is blurred in the render" : "The picture plays as recorded").finally(() => setOnDraft(null)); }; const fallback = defaults?.caption ?? "Original footage uncensored, blurred here for TOS"; const shown = blur?.caption ?? fallback; const [caption, setCaption] = useState(shown); useEffect(() => setCaption(shown), [shown, clipId]); const saveCaption = () => { const v = caption.trim(); if (v === shown) return; set({ caption: v && v !== fallback ? v : null }, v && v !== fallback ? "Saved the blur's caption" : "The blur's caption is the default"); }; const inside = playhead !== null && playhead >= span.start - 0.02 && playhead <= span.end + 0.02; const at = (edge: "from" | "to") => { if (playhead === null || !inside) return; set({ [edge]: r2(playhead) }, `Blurred ${edge === "from" ? "from" : "to"} ${edgeClock(playhead)}`); }; const strength = defaults?.strength ?? { min: 2, max: 15, default: 5 }; const partial = blur && (blur.from !== undefined || blur.to !== undefined); return (
for footage a platform’s rules would refuse: the picture blurred, the sound as it is
{blur && ( <>
{blur.caption !== undefined && ( )}
set({ strength: numOrNull(v) }, v ? `Blur strength ${v}` : "Blur strength as default")} />
blurred {partial ? `${edgeClock(blur.from ?? span.start)} – ${edgeClock(blur.to ?? span.end)}` : "the whole clip"} {partial && ( )} {playhead !== null && !inside && the playhead is outside the clip’s window}

{defaults && !defaults.rendered ? "This cut renders uncensored (render.blur off, the Look panel): no clip is blurred in it. A take keeps the other version." : "Blurred in the render and in the Live preview; the player above plays the source as it is."}

)}
); }