"use client"; import { useCallback, useEffect, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; // The Trailer page's editor (TrailerPage.tsx): every write is POST // /api/report/trailer-beats, the one writer `umtool trailer-beats` uses too, // for ONE reel (`reel`: promo/-beats.json; `trailer` is // promo/trailer-beats.json). Each write is an undo point (Undo). type Row = { n: number; kind: "clip" | "raw"; id?: string; start?: number; end?: number; at?: number; dur?: number; label: string | null; verdict: "confirmed" | "note" | null; seconds: number; running: number; over: boolean; clip: { start: number; end: number; at: number; section: string | null; quote: string | null } | null; problems: string[]; }; type View = { reel: string; file: string; title: string | null; take: string | null; confirmed: number; undo: { count: number; last: string | null }; limit: number; xfade: number; intro: boolean; introSeconds: number | null; introOwn?: { style?: string; site?: unknown } | null; outro?: { title: string; text: string; image: string | null; seconds: number } | null; outroSetting?: { on: boolean; title?: string; text?: string; image?: string; seconds?: number } | null; total: number; remaining: number; over: boolean; inOrder: boolean; outside: number; rows: Row[]; candidates: { id: string; start: number; end: number; quote: string | null; walkNote: string | null }[]; clips: { id: string; start: number; end: number }[]; limits: { id: string; label: string; seconds: number }[]; token: string | null; exists: boolean; }; const clock = (s: number) => { if (!Number.isFinite(s)) return "—"; const m = Math.floor(Math.abs(s) / 60); const r = Math.abs(s) - m * 60; return `${s < 0 ? "−" : ""}${m}:${r < 10 ? "0" : ""}${r.toFixed(1)}`; }; const input = "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)]"; export default function TrailerBeatsEditor({ project, reel = "trailer" }: { project: string; reel?: string }) { const [view, setView] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [addClip, setAddClip] = useState(""); const [addFrom, setAddFrom] = useState(""); const [addTo, setAddTo] = useState(""); const load = useCallback(async () => { const r = await fetch(`/api/report/trailer-beats?project=${encodeURIComponent(project)}&reel=${encodeURIComponent(reel)}`, { cache: "no-store" }); const j = await r.json().catch(() => ({})); if (!r.ok) setError(j.error ?? `HTTP ${r.status}`); else setView(j as View); }, [project, reel]); useEffect(() => { void load(); }, [load]); const op = async (name: string, args: Record = {}) => { setBusy(true); setError(null); try { const r = await fetch("/api/report/trailer-beats", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, reel, token: view?.token ?? null, op: name, ...args }), }); const j = await r.json().catch(() => ({})); if (!r.ok) { setError(j.stale ? "the beats changed since this page read them — reloaded; try again" : (j.error ?? `HTTP ${r.status}`)); if (j.stale) await load(); return false; } setView(j as View); // The Cut panel and anything else reading this reel: it changed. window.dispatchEvent(new CustomEvent("umtool:reel-written", { detail: { project, reel } })); return true; } finally { setBusy(false); } }; if (!view) return

{error ?? "reading the beats…"}

; const pct = Math.min(100, (view.total / view.limit) * 100); const preset = view.limits.find((l) => l.seconds === view.limit)?.id ?? "custom"; const pickClip = (id: string) => { setAddClip(id); const c = view.clips.find((x) => x.id === id); if (c) { setAddFrom(String(c.start)); setAddTo(String(c.end)); } }; return (
{/* ---- the running length against the limit ---- */}
{clock(view.total)} of {clock(view.limit)} ·{" "} {view.over ? over by {(view.total - view.limit).toFixed(1)} s : `${view.remaining.toFixed(1)} s to spare`} · {view.rows.length} beat {view.rows.length === 1 ? "" : "s"} · crossfade {view.xfade} s{view.intro ? ` · intro ${view.introSeconds ?? "?"} s` : ""}{view.outro ? ` · outro ${view.outro.seconds} s` : ""} {view.confirmed > 0 && ( {view.confirmed} confirmed on the walk )} {view.outside > 0 && ( {view.outside} beat{view.outside === 1 ? "" : "s"} to fix )}
{(view.outro || view.outroSetting) && ( <> e.key === "Enter" && void op("settings", { outro: { title: (e.target as HTMLInputElement).value || null } })} className={`${input} w-56`} /> e.key === "Enter" && void op("settings", { outro: { text: (e.target as HTMLInputElement).value || null } })} className={`${input} w-44`} /> e.key === "Enter" && void op("settings", { outro: { image: (e.target as HTMLInputElement).value || null } })} className={`${input} w-40`} /> { if (e.key !== "Enter") return; const v = (e.target as HTMLInputElement).value; void op("settings", { outro: { seconds: v ? Number(v) : null } }); }} className={`${input} w-12`} /> s )} {error && ( {error} )}
{/* ---- the beats ---- */}

beats — {view.file}

{view.rows.length === 0 ? (

No beats yet: add one below, from the ★ clips, or with Add to trailer in the clip editor.

) : (
    {view.rows.map((r, i) => ( ))}
)}
{/* ---- add ---- */}
add a beat setAddFrom(e.target.value)} className={`${input} w-20`} /> – setAddTo(e.target.value)} className={`${input} w-20`} /> it goes where the cut plays it
{/* ---- the walk's favourites ---- */}

★ from the walk — {view.candidates.length} not yet beats

{view.candidates.length === 0 ? (

Star a clip on the walk and it is offered here.

) : (
    {view.candidates.map((c) => (
  • ★ {c.id} {c.start}–{c.end} {c.walkNote && {c.walkNote}} {c.quote && “{c.quote.slice(0, 100)}{c.quote.length > 100 ? "…" : ""}”}
  • ))}
)}

Cut it here (above), or umtool trailer {project} --reel {view.reel}{view.take ? ` --take ${view.take}` : ""} · the same edits:{" "} umtool trailer-beats {project}{view.reel !== "trailer" ? ` --reel ${view.reel}` : ""}

); } function BeatRow({ row: r, index: i, last, busy, op }: { row: Row; index: number; last: boolean; busy: boolean; op: (name: string, args?: Record) => Promise }) { const [from, setFrom] = useState(String(r.kind === "clip" ? r.start : r.at)); const [to, setTo] = useState(String(r.kind === "clip" ? r.end : r.dur)); const save = () => { const a = Number(from); const b = Number(to); if (!Number.isFinite(a) || !Number.isFinite(b)) return; if (r.kind === "clip" && (a !== r.start || b !== r.end)) void op("set", { index: i, patch: { start: a, end: b } }); if (r.kind === "raw" && (a !== r.at || b !== r.dur)) void op("set", { index: i, patch: { at: a, dur: b } }); }; const fit = () => { if (!r.clip) return; const a = Math.max(r.start ?? r.clip.start, r.clip.start); const b = Math.min(r.end ?? r.clip.end, r.clip.end); void op("set", { index: i, patch: b - a >= 0.5 ? { start: a, end: b } : { start: r.clip.start, end: r.clip.end } }); }; const bad = r.problems.length > 0; return (
  • {r.n} {r.kind === "clip" ? r.id : "master"} {r.verdict && ( {r.verdict === "confirmed" ? "✓" : "✎"} )} setFrom(e.target.value)} onBlur={save} onKeyDown={(e) => e.key === "Enter" && save()} className={`${input} w-20`} title={r.kind === "clip" ? "source second it starts at" : "master second"} /> {r.kind === "clip" ? "–" : "for"} setTo(e.target.value)} onBlur={save} onKeyDown={(e) => e.key === "Enter" && save()} className={`${input} w-20`} title={r.kind === "clip" ? "source second it ends at" : "seconds"} /> {r.seconds.toFixed(2)} s {clock(r.running)} {r.clip?.section && {r.clip.section}} {bad && ( {r.problems.join("; ")} )} {bad && r.clip && ( )} {r.clip?.quote && “{r.clip.quote}”}
  • ); }