"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useCallback, useRef, useState } from "react"; import { badgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { clipBenchHref } from "@/lib/hrefs"; // --------------------------------------------------------------------------- // PROPOSE TIGHT WINDOWS (UI notes 2026-10-30, item 4): every clip of the cut // tightened to its sentence and each edge into its pause // (lib/report/tightPropose.mjs, GET /api/report/tighten), as a review list -- // before and after, the seconds either way, what moved and why. NOTHING // APPLIES WITHOUT A PRESS: each row's Apply is one window save (PUT // /api/report/window, the token, the cite that follows the edge), and an // applied row offers Undo, which writes back what was there. // --------------------------------------------------------------------------- type Edge = { start: number; end: number; seconds: number }; type Row = { id: string; quote: string | null; before: Edge; after: Edge | null; changed: boolean; how: string | null; said: string[]; why: string | null; fetched: boolean; locked: boolean; words: string | null; clear: { cut: boolean; mute: boolean }; prior: { cutStart: number | null; cutEnd: number | null; muteFrom: number | null; cite: number | null; citeUrl: string | null }; }; type Got = { token: string | null; rows: Row[]; counts: { clips: number; proposed: number; same: number; none: number } }; type Applied = { k: "applying" } | { k: "applied"; undo: Record; note: string } | { k: "undone" } | { k: "failed"; error: string }; const hms = (t: number) => { const s = Math.max(0, t); const m = Math.floor(s / 60); return `${m}:${(s - m * 60).toFixed(2).padStart(5, "0")}`; }; const signed = (d: number) => `${d > 0 ? "+" : d < 0 ? "−" : "±"}${Math.abs(d).toFixed(2)} s`; export default function TightWindowsPanel({ project }: { project: string }) { const router = useRouter(); const [got, setGot] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [all, setAll] = useState(false); const [done, setDone] = useState>({}); const token = useRef(null); // One write at a time: each reads the token the last one was handed back. const chain = useRef>(Promise.resolve()); const propose = useCallback(async () => { setLoading(true); setError(null); try { const r = await fetch(`/api/report/tighten?${new URLSearchParams({ project })}`, { cache: "no-store" }); const j = (await r.json()) as Got & { error?: string }; if (!r.ok) { setError(j.error ?? `HTTP ${r.status}`); return; } token.current = j.token; setGot(j); setDone({}); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setLoading(false); } }, [project]); const put = useCallback( async (clip: string, patch: Record) => { const r = await fetch("/api/report/window", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, clip, token: token.current, ...patch }), }); const j = (await r.json().catch(() => ({}))) as Record; if (r.ok) token.current = String(j.token ?? ""); return { ok: r.ok, j }; }, [project], ); const apply = useCallback( (row: Row) => { if (!row.after) return; setDone((d) => ({ ...d, [row.id]: { k: "applying" } })); const after = row.after; const patch: Record = { start: after.start, end: after.end, ...(row.clear.cut ? { cutStart: "", cutEnd: "" } : {}), ...(row.clear.mute ? { muteFrom: "" } : {}), }; chain.current = chain.current.then(async () => { const { ok, j } = await put(row.id, patch); if (!ok) { setDone((d) => ({ ...d, [row.id]: { k: "failed", error: j.stale ? "the manifest changed since these were proposed — Propose again" : String(j.error ?? "refused") }, })); return; } const before = row.prior; const followed = (j.citeFollowed ?? null) as { cite?: { from: number; to: number }; citeUrl?: unknown } | null; const undo: Record = { start: row.before.start, end: row.before.end, ...(row.clear.cut ? { cutStart: before.cutStart ?? "", cutEnd: before.cutEnd ?? "" } : {}), ...(row.clear.mute ? { muteFrom: before.muteFrom ?? "" } : {}), ...(followed?.cite ? { cite: before.cite ?? "" } : {}), ...(followed?.citeUrl ? { citeUrl: before.citeUrl ?? "" } : {}), }; const note = followed?.cite ? `the cite followed: ${hms(followed.cite.from)} → ${hms(followed.cite.to)}${followed.citeUrl ? ", and the QR" : ""}` : ""; setDone((d) => ({ ...d, [row.id]: { k: "applied", undo, note } })); router.refresh(); }); }, [put, router], ); const undo = useCallback( (row: Row) => { const s = done[row.id]; if (s?.k !== "applied") return; setDone((d) => ({ ...d, [row.id]: { k: "applying" } })); chain.current = chain.current.then(async () => { const { ok, j } = await put(row.id, s.undo); setDone((d) => ({ ...d, [row.id]: ok ? { k: "undone" } : { k: "failed", error: j.stale ? "the manifest changed since — Propose again" : String(j.error ?? "refused") } })); if (ok) router.refresh(); }); }, [done, put, router], ); const rows = got ? got.rows.filter((r) => all || r.changed) : []; // Until it is pressed, one button among the Cut tab's tools (it adds nothing to the page's // height); the review list then opens on a line of its own under them. const button = ( ); if (!got && !error) return {button}; return (

tight windows

{button} {got && ( {got.counts.proposed} of {got.counts.clips} clips would change · {got.counts.same} already tight {got.counts.none ? ` · ${got.counts.none} with nothing to go by` : ""} {" · "} )}
{error && (

{error}

)} {got && rows.length === 0 &&

every clip is already tight

} {rows.length > 0 && (
    {rows.map((r) => { const s = done[r.id]; const d = r.after ? r.after.seconds - r.before.seconds : 0; return (
  • {r.id} {hms(r.before.start)}–{hms(r.before.end)} ({r.before.seconds.toFixed(2)} s) {r.after ? ( <> → {hms(r.after.start)}–{hms(r.after.end)} ({r.after.seconds.toFixed(2)} s) {r.changed && ( {signed(d)} )} ) : ( {r.why} )} {r.locked && locked} {!r.fetched && not fetched} {r.clear.cut && clears the cut} {r.changed && r.after && ( {s?.k === "applied" ? ( <> applied{s.note ? ` — ${s.note}` : ""} ) : s?.k === "undone" ? ( undone ) : ( )} {s?.k === "failed" && ( {s.error} )} )} {(r.how || r.said.length > 0) && ( {r.how} {r.said.length ? ` — ${r.said.join("; ")}` : ""} {r.words ? ` · words: ${r.words}` : ""} )}
  • ); })}
)} {got && (

umtool tighten {project} prints the same list; --apply <clip>[,…] applies rows.

)}
); }