"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 = (
{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 && }
);
})}
)}
);
}