"use client"; import { useRouter } from "next/navigation"; import { useCallback, useEffect, useRef, useState } from "react"; import { badgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { MANIFEST_CHANGED, announceManifestChanged } from "../timelineApi"; import CensorOverlay, { type CensorRegion } from "../review/CensorOverlay"; // --------------------------------------------------------------------------- // THE CANDIDATES INBOX on the Cut tab (UI notes item 14). A sweep or an agent // PROPOSES clips (`umtool candidates

import ` / `add`, into // /candidates.json); each row here is one cited moment -- Play, its // verdict, the section (and group) it proposes -- and only a press here puts // it in the cut: Insert into section (its end) or Insert into group (after // the group's last clip, one of its clips), each one write through `timeline // insert`'s writer with an undo point first. Not this records the row as set // aside and hides it; nothing is ever deleted (lib/report/candidates.mjs). // --------------------------------------------------------------------------- type Place = { section: number | null; group: string | null; why: string | null }; type Candidate = { id: string; channel?: string; video: string; t: number; title?: string; date?: string; quote?: string; why?: string; verdict?: string; kind?: string; ref?: string; score?: number; rank?: number; top?: number; heading?: string; citeUrl?: string; status: "proposed" | "inserted" | "dismissed"; inserted?: { clip: string; into: string; section?: number; group?: string }; dismissed?: { at: string; note?: string }; place: Place; inCut: string | null; /** Inserted once as this clip, which has since left the cut (an Undo): it waits again. */ left?: string; }; type View = { exists: boolean; error: string | null; candidates: Candidate[]; counts: { proposed: number; inserted: number; dismissed: number }; sections: { index: number; label: string | null; words: string }[]; groups: { id: string; claim: string; section: number; ids: string[] }[]; token: string | null; playable: Record; /** Each candidate's recording's hidden regions (render.censor): overlaid on its player. */ hidden?: Record; }; const clock = (t: number) => { const s = Math.floor(t); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); return `${h ? `${h}:${String(m).padStart(2, "0")}` : m}:${String(s % 60).padStart(2, "0")}`; }; /** The cited moment, played here from a cached window (seeked to its second). */ function InlinePlayer({ project, c, hidden = [] }: { project: string; c: Candidate; hidden?: CensorRegion[] }) { const ref = useRef(null); const [from, setFrom] = useState(null); const [at, setAt] = useState(null); const [aspect, setAspect] = useState(null); const [err, setErr] = useState(null); const src = `/api/report/raw?project=${encodeURIComponent(project)}&candidate=${encodeURIComponent(c.id)}`; useEffect(() => { let live = true; void fetch(src, { headers: { range: "bytes=0-0" } }) .then((r) => { if (!live) return; if (!r.ok) setErr(`HTTP ${r.status}`); else setFrom(Number(r.headers.get("x-fetch-start") ?? 0)); }) .catch((e: unknown) => live && setErr(String(e))); return () => { live = false; }; }, [src]); if (err) return could not play: {err}; if (from === null) return opening…; const video = (

{video}
); } export default function CandidatesPanel({ project }: { project: string }) { const router = useRouter(); const [view, setView] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(null); const [showDone, setShowDone] = useState(false); const [playing, setPlaying] = useState(null); const [pickSection, setPickSection] = useState>({}); const [pickGroup, setPickGroup] = useState>({}); const load = useCallback(async () => { const r = await fetch(`/api/report/candidates?project=${encodeURIComponent(project)}`, { cache: "no-store" }).catch(() => null); if (!r) return; const j = (await r.json().catch(() => null)) as (View & { error?: string }) | null; if (!r.ok || !j) { setError(j?.error ?? `HTTP ${r.status}`); return; } setView(j); }, [project]); useEffect(() => { void load(); const on = (e: Event) => { if ((e as CustomEvent).detail?.project === project) void load(); }; window.addEventListener(MANIFEST_CHANGED, on); return () => window.removeEventListener(MANIFEST_CHANGED, on); }, [load, project]); const post = async (id: string, body: Record) => { setBusy(id); setError(null); try { const r = await fetch("/api/report/candidates", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, id, ...body }) }); const j = (await r.json().catch(() => ({}))) as Record; if (r.status === 409 && j.stale) setError(`${String(j.error ?? "the cut changed")} — reloaded; check and insert again`); else if (!r.ok) setError(String(j.error ?? `HTTP ${r.status}`)); else if (j.attributionError) setError(`${id} is in as ${String(j.clip)}, but its header was refused: ${String(j.attributionError)}`); if (r.ok && body.op === "insert") { announceManifestChanged(project); router.refresh(); } return r.ok; } finally { setBusy(null); await load(); } }; if (!view) { return error ? (

{error}

) : null; } const waiting = view.candidates.filter((c) => c.status === "proposed"); const done = view.candidates.filter((c) => c.status !== "proposed"); const rows = showDone ? view.candidates : waiting; const sectionWords = (n: number | null | undefined) => view.sections.find((s) => s.index === n)?.words ?? (n === null || n === undefined ? "" : `§${n}`); return (

candidates — {view.counts.proposed} proposed

{view.counts.inserted} inserted · {view.counts.dismissed} set aside · nothing goes in the cut until you press Insert {done.length > 0 && ( )}
{error && (

{error}

)} {view.error &&

{view.error}

} {!view.candidates.length && (

No proposals. A sweep’s bullets come in with umtool candidates {project} import <sweep.md>; an agent’s with{" "} umtool candidates {project} add <json>.

)} {rows.length > 0 && (
    {rows.map((c) => { const sec = pickSection[c.id] ?? (c.place.section !== null ? String(c.place.section) : ""); const groupsHere = view.groups; const grp = pickGroup[c.id] ?? c.place.group ?? ""; const waitingRow = c.status === "proposed"; return (
  • {c.id} {c.top !== undefined && #{c.top} overall} {c.verdict ?? c.kind ?? "proposed"} {typeof c.score === "number" ? ` · ${c.score}` : ""} {c.rank !== undefined && {c.rank}. in its section} {c.title ?? c.video} {c.channel ? `${c.channel}/` : ""} {c.video} @ {clock(c.t)} {c.date ? ` · ${c.date}` : ""} {c.inCut && waitingRow && ( in the cut as {c.inCut} )} {c.left && waitingRow && ( was {c.left}, no longer in the cut )} {c.status === "inserted" && inserted as {c.inserted?.clip}} {c.status === "dismissed" && not this{c.dismissed?.note ? ` — ${c.dismissed.note}` : ""}}
    {c.quote &&

    “{c.quote}”

    } {c.why &&

    {c.why}

    } {waitingRow && (
    {view.playable[c.id] ? ( ) : c.citeUrl ? ( Play ↗ ) : null} {c.place.section !== null ? `proposes ${sectionWords(c.place.section)}${c.place.group ? ` · group ${c.place.group}` : ""}${c.place.why ? ` (${c.place.why})` : ""}` : c.heading ? `under “${c.heading}” — pick a section` : "pick a section"} {view.sections.length > 0 || view.groups.length === 0 ? ( <> ) : null} {groupsHere.length > 0 && ( <> )}
    )} {playing === c.id &&
  • ); })}
)}
); }