"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; import { DECK_CLAIM_LIMITS, DECK_CLAIM_OPS, DECK_CLAIM_PERS, deckClaimShapeErrors } from "umtool-report-to-video/claims"; // THE DECK'S CLAIM CHECKS (render.chrome.deck.claims; report-to-video // claims.mjs, DESIGN 2026-10-24): his explicit claims drawn against the record // on the footer's rail. A COUNT claim -- a number over a window of dates // (`“less than ten” in a year`), the fact-check tally's stamps its record, or // with `per` the spans that hold more than he said (`“doesn't have multiple // seizures a week”`). A GOALPOST -- a deadline he set and moved: each step his // words, the day it runs from and the two durations of its due window. // // Each claim is said in a clip at a SOURCE second ("use the playhead" takes // the scrubber's clip and second). Validation is the build's own // (claims.mjs deckClaimShapeErrors -- dependency-free, so this bundle takes // it), shown under each claim as it is typed; the clips it names are checked // against the cut's here, and the writer refuses in the build's words. One // save writes `deck.claims` and nothing else of the deck (PUT // /api/report/chrome { claims }), the writer `umtool look deck.claims=…` uses. export type DeckClaim = Record & { kind: string }; export type ClaimClip = { id: string; label: string; day: string | null; from?: number }; /** The record a preview's schedule says for one claim (deck.mjs claimsSchedule's item). */ export type ClaimRecord = { kind: string; n: number; total?: number; noun?: string; window?: { from: string; to: string }; spans?: { label: string }[]; holds?: boolean; steps?: { segment: string; dueText: string }[]; }; type Step = { at: string; t: string; said: string; from: string; due0: string; due1: string }; type CountRow = { kind: "count"; at: string; t: string; said: string; from: string; to: string; tally: string; per: string; op: string; value: string }; type GoalRow = { kind: "goalpost"; label: string; steps: Step[] }; type Row = CountRow | GoalRow; const input = "rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 text-[11px] text-[var(--color-text)] outline-none focus:border-[var(--color-sel)]"; const str = (v: unknown) => (v === undefined || v === null ? "" : String(v)); const num = (s: string): number | undefined => (s.trim() === "" ? undefined : Number(s)); function rowOf(c: DeckClaim): Row { if (c.kind === "goalpost") { const steps = Array.isArray(c.steps) ? (c.steps as Record[]) : []; return { kind: "goalpost", label: str(c.label), steps: steps.map((s) => { const due = Array.isArray(s.due) ? (s.due as unknown[]) : []; return { at: str(s.at), t: str(s.t), said: str(s.said), from: str(s.from), due0: str(due[0]), due1: str(due[1]) }; }), }; } const w = (c.window ?? {}) as Record; const cl = (c.claim ?? {}) as Record; return { kind: "count", at: str(c.at), t: str(c.t), said: str(c.said), from: str(w.from), to: str(w.to), tally: str(c.tally), per: str(c.per), op: str(cl.op), value: str(cl.value), }; } /** A row as the manifest writes it: blanks left out, numbers as numbers. */ function claimOf(r: Row): DeckClaim { if (r.kind === "goalpost") { return { kind: "goalpost", ...(r.label.trim() ? { label: r.label.trim() } : {}), steps: r.steps.map((s) => ({ at: s.at, t: num(s.t), said: s.said.trim(), ...(s.from.trim() ? { from: s.from.trim() } : {}), due: [s.due0.trim(), s.due1.trim()], })), }; } return { kind: "count", at: r.at, t: num(r.t), said: r.said.trim(), window: { from: r.from.trim(), to: r.to.trim() }, tally: r.tally.trim(), ...(r.per ? { per: r.per } : {}), ...(r.op ? { claim: { op: r.op, value: num(r.value) } } : {}), }; } /** A year before a YYYY-MM-DD, as one: the default window of a count claim said on that day. */ const yearBefore = (d: string) => (/^\d{4}-\d{2}-\d{2}$/.test(d) ? `${Number(d.slice(0, 4)) - 1}${d.slice(4)}` : ""); const PER_NAMES: Record = { P1D: "a day", P1W: "a week", P1M: "a month" }; export default function DeckClaimsEditor({ claims, clips, verdicts, playhead, record, busy, save, }: { claims: DeckClaim[]; clips: ClaimClip[]; /** The tally's words a count claim may name (this cut's verdict labels). */ verdicts: string[]; /** The scrubber's clip and source second, or null when it is not on a clip. */ playhead: () => { clip: string; second: number } | null; /** The preview schedule's claim items (deck.mjs claimsSchedule), by their index in the saved list. */ record: ClaimRecord[]; busy: boolean; save: (claims: DeckClaim[] | null) => Promise; }) { const savedKey = JSON.stringify(claims); const [rows, setRows] = useState(() => claims.map(rowOf)); const [dirty, setDirty] = useState(false); // Every edit is counted; `saved` is the count the last save was sent at (-1: none is in // hand -- a save refused, or edits never saved). A save re-reads the deck after it writes // (and recomposes the preview: seconds, on a busy machine), and an edit made meanwhile is // the operator's: the saved value arriving after it must not put the old claims back, // nor the save's end mark it saved. const edits = useRef(0); const saved = useRef(0); useEffect(() => { // The saved value, by its text: a re-read with the same claims keeps the edits; another // value replaces the rows only when it accounts for every edit made (none since the save). if (edits.current !== saved.current) return; setRows(claims.map(rowOf)); setDirty(false); // eslint-disable-next-line react-hooks/exhaustive-deps }, [savedKey]); const change = (next: Row[]) => { edits.current += 1; setRows(next); setDirty(true); }; const onSave = () => { const at = edits.current; saved.current = at; void save(list.length ? list : null).then((ok) => { if (!ok) saved.current = -1; else if (edits.current === at) setDirty(false); }); }; const put = (i: number, patch: Partial | Partial) => change(rows.map((r, j) => (j === i ? ({ ...r, ...patch } as Row) : r))); const putStep = (i: number, k: number, patch: Partial) => change(rows.map((r, j) => (j === i && r.kind === "goalpost" ? { ...r, steps: r.steps.map((s, m) => (m === k ? { ...s, ...patch } : s)) } : r))); const here = () => { const p = playhead(); if (p) return { at: p.clip, t: String(p.second) }; const c = clips[0]; return { at: c?.id ?? "", t: c?.from !== undefined ? String(c.from) : "" }; }; const dayOf = (id: string) => clips.find((c) => c.id === id)?.day ?? ""; const addCount = () => { const h = here(); const to = dayOf(h.at); change([...rows, { kind: "count", ...h, said: "", from: yearBefore(to), to, tally: verdicts[0] ?? "", per: "", op: "", value: "" }]); }; const addGoal = () => change([...rows, { kind: "goalpost", label: "", steps: [{ ...here(), said: "", from: "", due0: "P3W", due1: "P5W" }] }]); // The build's validator, as typed; and the clips each claim names, against this cut's. const list = useMemo(() => rows.map(claimOf), [rows]); const problems = useMemo(() => { const all = deckClaimShapeErrors(list, "claims"); const ids = new Set(clips.map((c) => c.id)); rows.forEach((r, i) => { if (r.kind === "count" && r.at && !ids.has(r.at)) all.push(`claims[${i}].at names ${r.at}, which is not a clip in this cut`); if (r.kind === "goalpost") r.steps.forEach((s, k) => s.at && !ids.has(s.at) && all.push(`claims[${i}].steps[${k}].at names ${s.at}, which is not a clip in this cut`)); }); return all; }, [list, rows, clips]); const problemsOf = (i: number) => problems.filter((p) => p.startsWith(`claims[${i}]`)).map((p) => p.replace(`claims[${i}]`, "").replace(/^\./, "")); const listProblems = problems.filter((p) => !/^claims\[\d+\]/.test(p)); const clipSelect = (value: string, onChange: (v: string) => void, testId: string) => ( ); const atPlayhead = (onSet: (at: string, t: string) => void, testId: string) => ( ); return (
claim checks {rows.length} of {DECK_CLAIM_LIMITS.count} {dirty && unsaved}
{listProblems.map((p) => (

{p}

))} {rows.length === 0 ? (

No claim checks: the rail carries no claim.

) : (
    {rows.map((r, i) => { const errs = problemsOf(i); const rec = !dirty ? record.find((x) => x.n === i) : undefined; return (
  1. {i + 1} {r.kind} {r.kind === "count" ? ( <> {clipSelect(r.at, (v) => put(i, { at: v }), `claim-at-${i}`)} {atPlayhead((at, t) => put(i, { at, t }), `claim-playhead-${i}`)} ) : ( put(i, { label: e.target.value })} className={`${input} w-52`} /> )}
    {r.kind === "count" ? (
    put(i, { said: e.target.value })} className={`${input} w-64`} />
    ) : (
      {r.steps.map((s, k) => (
    1. step {k + 1} {clipSelect(s.at, (v) => putStep(i, k, { at: v }), `claim-step-at-${i}-${k}`)} {atPlayhead((at, t) => putStep(i, k, { at, t }), `claim-step-playhead-${i}-${k}`)} putStep(i, k, { said: e.target.value })} className={`${input} w-32`} /> {r.steps.length > 1 && ( )}
    2. ))} {r.steps.length < DECK_CLAIM_LIMITS.steps && (
    3. )}
    )} {errs.length > 0 && (
      {errs.map((e) => (
    • {e}
    • ))}
    )} {rec && (

    {rec.kind === "count" ? `record: ${rec.total} ${rec.noun} in ${rec.window?.from}–${rec.window?.to}` + (rec.spans ? ` · ${rec.spans.length ? rec.spans.map((s) => s.label).join(", ") : "no span"}` : "") + (rec.holds === undefined ? "" : rec.holds ? " · it does not contradict him" : " · it contradicts him") : `due: ${(rec.steps ?? []).map((s) => s.dueText).join(" → ")}`}

    )}
  2. ); })}
)} {verdicts.map((v) => (
); }