"use client"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Field, Input, Select, Textarea } from "@/components/ui/field"; import { useHydrated } from "@/components/ui/use-hydrated"; import { announceManifestChanged } from "./timelineApi"; // --------------------------------------------------------------------------- // FILL BY HAND (release 32 R2): beside Draft everything, the cut's EMPTY media // fields as blanks a person fills -- each clip's on-screen title, a teaser's // lines, the YouTube title and description, the X and Bluesky posts -- and the // flips or the threads (no other editor has them), so a cut reaches a buildable // manifest with no model. Saved through the manifest writer as the operator's // edit (lib/report/hand-fill.mjs): no drafted mark, "written by hand", never // replaced by a draft. Only what is filled is written; a blank stays empty. // // Labels are contracts: the toggle "Fill empty fields by hand", the region // "fill by hand", "Save what is filled"; each field's box is named for it // ("on-screen title of ", "teaser lines of ", "YouTube title", …). // --------------------------------------------------------------------------- type EmptyField = { address: string; entry: string | null; field: string; label: string; context: string }; type Clip = { id: string; label: string; thread: string | null }; type Side = { entry: string; when: string; words: string }; type Pair = { id: string; topic: string; then: Side; now: Side }; type Thread = { id: string; label: string; outcome?: { verdict: string; label?: string } }; export type HandFillView = { empty: EmptyField[]; clips: Clip[]; deck: boolean; flips: { pairs: Pair[] } | null; threads: { list: Thread[]; clips: Record } | null; token: string | null; }; const VERDICTS = ["CORROBORATED", "PARTLY", "CONTRADICTED", "NOT_FOUND", "UNTESTABLE"]; const slug = (s: string, at: number, prefix: string) => { const v = s .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 40); return v || `${prefix}${at + 1}`; }; const blankSide = (entry = ""): Side => ({ entry, when: "", words: "" }); function FlipsEditor({ clips, pairs, setPairs }: { clips: Clip[]; pairs: Pair[]; setPairs: (p: Pair[]) => void }) { const set = (i: number, p: Pair) => setPairs(pairs.map((x, j) => (j === i ? p : x))); const side = (i: number, which: "then" | "now", key: keyof Side, v: string) => set(i, { ...pairs[i]!, [which]: { ...pairs[i]![which], [key]: v } }); return (
{pairs.map((p, i) => (
set(i, { ...p, topic: e.target.value })} className="w-48" />
{(["then", "now"] as const).map((w) => (
{w} side(i, w, "when", e.target.value)} className="w-28" placeholder="2019" /> side(i, w, "words", e.target.value)} />
))}
))}
); } function ThreadsEditor({ clips, list, setList, assign, setAssign, }: { clips: Clip[]; list: Thread[]; setList: (l: Thread[]) => void; assign: Record; setAssign: (a: Record) => void; }) { return (
{list.map((t, i) => (
setList(list.map((x, j) => (j === i ? { ...x, label: e.target.value } : x)))} className="w-56" />
))}
{list.length > 0 && (
each clip's thread {clips.map((c) => ( ))}
)}
); } export default function HandFill({ project, initial }: { project: string; initial: HandFillView }) { const router = useRouter(); const hydrated = useHydrated(); const [view, setView] = useState(initial); const [open, setOpen] = useState(false); const [text, setText] = useState>({}); const [pairs, setPairs] = useState(initial.flips?.pairs ?? []); const [list, setList] = useState(initial.threads?.list ?? []); const [assign, setAssign] = useState>(initial.threads?.clips ?? {}); const [panelDirty, setPanelDirty] = useState<"flips" | "threads" | null>(null); const [busy, setBusy] = useState(false); const [status, setStatus] = useState<{ ok: boolean; text: string; errors: string[] } | null>(null); const val = (k: string) => text[k] ?? ""; const put = (k: string) => (e: React.ChangeEvent) => setText({ ...text, [k]: e.target.value }); // A cut has flips or threads, never both: the one it has, else either. const panel: "flips" | "threads" | "either" = view.flips ? "flips" : view.threads ? "threads" : "either"; const empties = view.empty.length; const fills = () => { const out: { field: string; entry?: string; value: unknown }[] = []; for (const f of view.empty) { if (f.field === "onscreen") { const title = val(`${f.address}:title`).trim(); const subtitle = val(`${f.address}:subtitle`).trim(); if (title || subtitle) out.push({ field: "onscreen", entry: f.entry!, value: { ...(title ? { title } : {}), ...(subtitle ? { subtitle } : {}) } }); } else if (f.field === "lines") { const lines = val(f.address).split("\n").map((l) => l.trim()).filter(Boolean); if (lines.length) out.push({ field: "lines", entry: f.entry!, value: lines }); } else if (f.field === "publish.youtube") { const title = val("youtube:title").trim(); const description = val("youtube:description").trim(); if (title || description) out.push({ field: f.field, value: { title, description } }); } else if (f.field === "publish.x" || f.field === "publish.bluesky") { const post = val(f.field).trim(); if (post) out.push({ field: f.field, value: { post } }); } } if (panelDirty === "flips" && pairs.length) { out.push({ field: "render.chrome.flips", value: { pairs: pairs.map((p, i) => ({ id: p.id || slug(p.topic, i, "p"), topic: p.topic.trim(), then: { ...p.then, when: p.then.when.trim(), words: p.then.words.trim() }, now: { ...p.now, when: p.now.when.trim(), words: p.now.words.trim() } })) }, }); } if (panelDirty === "threads" && list.length) { out.push({ field: "render.chrome.threads", value: { list: list.map((t, i) => ({ id: t.id || slug(t.label, i, "t"), label: t.label.trim(), ...(t.outcome?.verdict ? { outcome: t.outcome } : {}) })), clips: assign }, }); } return out; }; const save = async () => { const f = fills(); if (!f.length) return setStatus({ ok: false, text: "nothing filled yet", errors: [] }); setBusy(true); setStatus(null); try { const r = await fetch("/api/report/hand-fill", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, token: view.token, fills: f }), cache: "no-store" }); const b = (await r.json().catch(() => ({}))) as Partial & { written?: string[]; refused?: { field: string; error: string; errors: string[] } | null; error?: string }; if (b.written?.length) { announceManifestChanged(project); router.refresh(); } if (b.empty) { setView(b as HandFillView); setText({}); setPanelDirty(null); } if (!r.ok && !b.refused) throw new Error(b.error ?? `HTTP ${r.status}`); setStatus( b.refused ? { ok: false, text: `${b.written?.length ? `wrote ${b.written.join(", ")}; ` : ""}${b.refused.field} was refused: ${b.refused.error}`, errors: b.refused.errors } : { ok: true, text: `written by hand: ${(b.written ?? []).join(", ")}`, errors: [] }, ); } catch (e) { setStatus({ ok: false, text: (e as Error).message, errors: [] }); } finally { setBusy(false); } }; const onscreen = view.empty.filter((f) => f.field === "onscreen"); const teasers = view.empty.filter((f) => f.field === "lines"); const has = (field: string) => view.empty.some((f) => f.field === field); return (

by hand

{empties} empty field{empties === 1 ? "" : "s"}
{open && (
{ e.preventDefault(); void save(); }} > {onscreen.length > 0 && (
on-screen titles {onscreen.map((f) => (
{f.entry} {f.context && “{f.context}”}
))}
)} {teasers.map((f) => (