"use client"; import { useCallback, useEffect, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; // "Prepare clips for the walk" (lib/report/benchPrep.mjs): the clip bench's // first-open work -- a long recording's excerpt, the waveform -- made by a // runner in the background, one clip at a time in the heavy slot, so opening // a clip on the walk does not wait on ffmpeg. A status line and one button. // With `take` (the take's walk): the take's clips, its own runner. type PrepState = { counts: { clips: number; ready: number; todo: number; failed: number; noWindow: number; excerpts: number }; running: boolean; runner: { pid: number | null; done: number; total: number; current: { id: string; step: string } | null } | null; last: { state: string; endedAt: string | null; done: number; total: number } | null; off?: boolean; error?: string; }; const POLL_MS = 2500; export default function BenchPrepPanel({ project, take = null }: { project: string; take?: string | null }) { const [s, setS] = useState(null); const [asking, setAsking] = useState(false); const [err, setErr] = useState(null); const load = useCallback(async () => { const q = new URLSearchParams({ project }); if (take) q.set("take", take); const r = await fetch(`/api/report/prep?${q.toString()}`, { cache: "no-store" }); const j = (await r.json().catch(() => ({}))) as PrepState; if (!r.ok) setErr(j.error ?? `HTTP ${r.status}`); else { setErr(null); setS(j); } }, [project, take]); useEffect(() => { void load(); }, [load]); useEffect(() => { if (!s?.running) return; const id = window.setTimeout(() => void load(), POLL_MS); return () => window.clearTimeout(id); }, [s, load]); if (!s && !err) return null; const c = s?.counts; const fetched = c ? c.clips - c.noWindow : 0; if (c && fetched === 0) return null; const left = c ? c.todo + c.failed : 0; async function prepare() { setAsking(true); setErr(null); try { const r = await fetch("/api/report/prep", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, ...(take ? { take } : {}) }), }); const j = (await r.json().catch(() => ({}))) as PrepState; if (!r.ok) setErr(j.error ?? `HTTP ${r.status}`); else setS(j); if (j.off) setErr("prep is switched off here (UMTOOL_BENCH_PREP=0)"); } finally { setAsking(false); } } const run = s?.runner; return (
{left > 0 && !s?.running && ( )} {c && ( {c.ready} of {fetched} ready to open {s?.running && run ? ` · preparing ${Math.min(run.done + 1, Math.max(run.total, 1))} of ${run.total}${ run.current ? ` — ${run.current.id}, ${run.current.step === "waiting" ? "waiting for the heavy slot" : run.current.step}` : "" }` : left ? ` · ${c.todo} to prepare${c.failed ? ` · ${c.failed} failed` : ""}` : ""} )} {err && ( {err} )}
); }