"use client";
import { Options } from "@/components/ui/disclosure";
import Link from "next/link";
import { useCallback, useEffect, useRef, useState } from "react";
import { buttonVariants } from "@/components/ui/button";
import { reelPlayHref, reelWalkHref, takesHref } from "@/lib/hrefs";
import { clock, cutSrc, readReelView, type ReelJob, type ReelView } from "./reelTypes";
// CUT THE TRAILER (lane/beat-reels) on the Trailer page: the reel's Play and
// walk on the chosen take's master, and the cut itself as ONE job on umtool's
// runner (POST /api/report/reel → `umtool trailer
--reel --take `,
// which takes the machine's heavy slot; one job at a time; its log; Cancel).
// The result plays here, with its LOCAL PATH (the operator reads output files
// by path), and every earlier cut of the reel is listed by take.
const fmtBytes = (n: number) => (n > 1e9 ? `${(n / 1e9).toFixed(2)} GB` : n > 1e6 ? `${(n / 1e6).toFixed(1)} MB` : `${Math.round(n / 1e3)} kB`);
export default function ReelCutPanel({ project, reel }: { project: string; reel: string }) {
const [view, setView] = useState(null);
const [take, setTake] = useState(null);
const [error, setError] = useState(null);
const [job, setJob] = useState(null);
const [log, setLog] = useState([]);
const since = useRef(0);
const logBox = useRef(null);
// The log follows its newest line.
useEffect(() => {
if (logBox.current) logBox.current.scrollTop = logBox.current.scrollHeight;
}, [log]);
const load = useCallback(async () => {
try {
const v = await readReelView(project, reel, take);
setView(v);
if (v.job && v.job.state === "running") setJob((j) => j ?? v.job ?? null);
setError(null);
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
}
}, [project, reel, take]);
useEffect(() => {
void load();
}, [load]);
// The beats editor wrote: read the reel again (the running length, what maps).
useEffect(() => {
const on = () => void load();
window.addEventListener("umtool:reel-written", on);
return () => window.removeEventListener("umtool:reel-written", on);
}, [load]);
// Follow the job: its log, then the reel read again for the new cut.
useEffect(() => {
if (!job || job.state !== "running") return;
let live = true;
const tick = async () => {
const r = await fetch(`/api/report/reel?job=${encodeURIComponent(job.id)}&since=${since.current}`, { cache: "no-store" }).catch(() => null);
const j = r && r.ok ? ((await r.json()) as ReelJob) : null;
if (!live || !j) return;
if (j.log?.length) setLog((l) => [...l, ...j.log].slice(-300));
since.current = j.next ?? since.current;
if (j.state !== "running") {
setJob(j);
void load();
}
};
const id = window.setInterval(() => void tick(), 1000);
void tick();
return () => {
live = false;
window.clearInterval(id);
};
}, [job, load]);
// The cut's Options (lane/ui-simplify): umtool trailer's --finale, --out and --dry-run.
const [finaleS, setFinaleS] = useState("");
const [outName, setOutName] = useState("");
const [dryRun, setDryRun] = useState(false);
const cutOptions = { ...(Number(finaleS) > 0 ? { finale: Number(finaleS) } : {}), ...(outName.trim() ? { out: outName.trim() } : {}), ...(dryRun ? { dryRun: true } : {}) };
const cut = async () => {
if (!view?.take) return;
setError(null);
const r = await fetch("/api/report/reel", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, reel, take: view.take.id, ...cutOptions }) });
const j = await r.json().catch(() => ({}));
if (!r.ok) {
setError(String(j.error ?? `HTTP ${r.status}`));
return;
}
since.current = 0;
setLog([]);
setJob(j.job as ReelJob);
};
const cancel = async () => {
if (!job) return;
await fetch(`/api/report/reel?cancel=${encodeURIComponent(job.id)}`, { method: "POST" });
};
if (!view) return {error ?? "reading the reel's takes…"};
const t = view.take?.id ?? null;
const running = job?.state === "running";
const here = t ? view.cuts.find((c) => c.take === t) ?? null : null;
return (
);
}