"use client"; import Link from "next/link"; import { useCallback, useEffect, useState } from "react"; import { badgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { Panel } from "@/components/ui/panel"; import { fmtAgo, fmtBytes } from "@/lib/format"; import { clipEditorHref } from "@/lib/hrefs"; // MAKE VARIANTS and SYNC (video knobs, plans/video-knobs-ui.md), on Deliver: // // the ladder render.variants -- each a name and a height + crf, or a size // cap (maxMiB); `master` is the build itself. Absent: the // default ladder. Saved through PUT /api/report/render. // Make variants one job (POST /api/report/deliver { action: "variants", // take? }), of the project's build or a take's; the encoder // takes the heavy slot // the list what variants.json says was made: size, frame, bitrate, a link // Sync the build's sync report (/sourced/sync.json): each // clip's sound against its picture, a lag past one frame // flagged, each linking to that clip's Audio box // // `umtool variants [--take t]` and `umtool look variants=…` // do the same from a terminal. type Variant = { name: string; height?: number; crf?: number; maxMiB?: number; preset?: string }; type Made = { name: string; width: number | null; height: number | null; bytes: number | null; kbps: number | null; exists: boolean; mtimeMs: number | null }; type SyncItem = { id: string; start: number | null; lagMs: number | null; peak: number | null; flagged: boolean; clip: boolean }; type State = { take: string | null; slug: string; built: { size: number; mtimeMs: number } | null; ladder: Variant[]; ladderIsDefault: boolean; ladderDefault: Variant[]; made: { at: string | null; items: Made[] } | null; stale: boolean; sync: { fps: number | null; frameMs: number; items: SyncItem[] } | null; takes: string[]; }; type Job = { id: string; state: string; error: string | null; kind?: string; log?: string[]; next?: number }; type LadderRow = { name: string; height: string; crf: string; maxMiB: string; preset: string }; const input = "num rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 font-mono text-[11px] text-[var(--color-text)] outline-none focus:border-[var(--color-sel)]"; const rowOf = (v: Variant): LadderRow => ({ name: v.name, height: v.height == null ? "" : String(v.height), crf: v.crf == null ? "" : String(v.crf), maxMiB: v.maxMiB == null ? "" : String(v.maxMiB), preset: v.preset ?? "" }); const variantOf = (r: LadderRow): Variant => { const n = (s: string) => (s.trim() === "" ? undefined : Number(s)); const out: Variant = { name: r.name.trim() }; for (const k of ["height", "crf", "maxMiB"] as const) { const v = n(r[k]); if (v !== undefined) out[k] = v; } if (r.preset.trim()) out.preset = r.preset.trim(); return out; }; const what = (v: Variant) => (v.name === "master" ? "the build as it is" : v.maxMiB ? `at most ${v.maxMiB} MiB${v.height ? `, up to ${v.height}p` : ""}` : `${v.height ? `${v.height}p` : "the build's height"}, crf ${v.crf ?? 23}`) + (v.preset ? ` · ${v.preset}` : ""); export default function VariantsPanel({ project }: { project: string }) { const [take, setTake] = useState(""); const [state, setState] = useState(null); const [job, setJob] = useState(null); const [log, setLog] = useState([]); const [error, setError] = useState(null); const [ladder, setLadder] = useState(null); const q = `project=${encodeURIComponent(project)}${take ? `&take=${encodeURIComponent(take)}` : ""}`; const load = useCallback(async () => { const r = await fetch(`/api/report/variants?${q}`, { cache: "no-store" }); const j = (await r.json()) as State & { error?: string }; if (!r.ok) throw new Error(j.error ?? `HTTP ${r.status}`); setState(j); }, [q]); useEffect(() => { load().catch((e) => setError(e instanceof Error ? e.message : String(e))); }, [load]); useEffect(() => { if (!job || job.state !== "running") return; let since = 0; const t = setInterval(async () => { const r = await fetch(`/api/report/deliver?job=${job.id}&since=${since}`, { cache: "no-store" }).catch(() => null); if (!r?.ok) return; const j = (await r.json()) as Job; since = j.next ?? since; if (j.log?.length) setLog((l) => [...l, ...(j.log ?? [])].slice(-40)); if (j.state !== "running") { setJob(j); await load().catch(() => {}); } }, 1200); return () => clearInterval(t); }, [job, load]); const make = async () => { setError(null); setLog([]); const r = await fetch("/api/report/deliver", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, action: "variants", ...(take ? { take } : {}) }) }); const j = (await r.json().catch(() => ({}))) as { job?: Job; error?: string }; if (!r.ok || !j.job) setError(j.error ?? `HTTP ${r.status}`); else setJob(j.job); }; const cancel = async () => { if (job) await fetch(`/api/report/deliver?cancel=${job.id}`, { method: "POST" }).catch(() => null); }; const saveLadder = async (variants: Variant[] | null) => { setError(null); const t = (await (await fetch(`/api/report/render?project=${encodeURIComponent(project)}`, { cache: "no-store" })).json()) as { token?: string }; const r = await fetch("/api/report/render", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, token: t.token ?? null, variants }) }); const j = (await r.json().catch(() => ({}))) as { error?: string; errors?: string[] }; if (!r.ok) { setError(j.errors?.join("; ") ?? j.error ?? `HTTP ${r.status}`); return; } setLadder(null); await load(); }; const running = job?.state === "running"; const file = (name: string) => `/api/report/variants?${q}&file=${encodeURIComponent(name)}`; return (
{state?.built ? `${fmtBytes(state.built.size)} · built ${fmtAgo(state.built.mtimeMs)}` : state ? "not built yet" : "…"} {running && ( )}
{/* ---- the ladder ---- */}
ladder {state?.ladderIsDefault ? "the default" : "this cut's own"} {ladder === null ? ( ) : ( <> )} {!state?.ladderIsDefault && ladder === null && ( )}
{ladder === null ? (
    {(state?.ladder ?? []).map((v) => (
  • {v.name} {what(v)}
  • ))}
) : (
    {ladder.map((r, i) => (
  1. {(["name", "height", "crf", "maxMiB", "preset"] as const).map((k) => ( ))}
  2. ))}
)}
{/* ---- what was made ---- */} {state?.made && (
made {state.made.at ? fmtAgo(Date.parse(state.made.at)) : ""} {state.stale && made from an older build} {state.made.items.map((m) => ( ))}
{m.name} {m.width && m.height ? `${m.width}×${m.height}` : "–"} {m.bytes ? fmtBytes(m.bytes) : "–"} {m.kbps ? `${m.kbps} kb/s` : ""} {m.exists ? ( open ) : ( gone )}
)} {job && (
{running ? "Making the variants…" : job.state === "done" ? "Variants made." : `Variants ${job.state}${job.error ? `: ${job.error}` : ""}`} {log.length > 0 &&
{log.join("\n")}
}
)} {/* ---- sync ---- */}
sync {!state?.sync ? (

No sync report for this build: the build writes one (sourced/sync.json) as it finishes.

) : ( <> each clip’s sound against its picture; past one frame ({state.sync.frameMs} ms) is flagged ·{" "} {state.sync.items.filter((x) => x.flagged).length} flagged {state.sync.items.map((x) => ( ))}
{x.clip ? ( {x.id} ) : ( x.id )} {x.start != null ? `${x.start.toFixed(2)} s` : ""} {x.lagMs != null ? `${x.lagMs > 0 ? "+" : ""}${Math.round(x.lagMs)} ms` : "–"} {x.flagged ? (x.lagMs != null && x.lagMs < 0 ? "sound early" : "sound late") : ""}
)}
{error && (

{error}

)}
); }