"use client"; import { useEffect, useState } from "react"; import type { RenderCut } from "@/lib/report/playCut.mjs"; // Where the render will cut each clip (GET /api/report/cut-points: the build's // own snap, lib/report/cutPoints.mjs), for the Play view's plan. Asked again // whenever the cut's entries change (a reload after an edge was moved in the // bench): the route measures only what moved. Until it answers the plan plays // the manifest's edges, and says so. type Row = RenderCut & { id: string; missing?: boolean; error?: string; montage?: boolean }; export type RenderCuts = { state: "measuring" | "render" | "estimate"; cuts: Map | null; /** render.transitionStyle, as the route read it. */ style: string; /** A montage cuts each clip to its phrase, which the route does not model. */ montage: boolean; why: string | null; }; const MEASURING: RenderCuts = { state: "measuring", cuts: null, style: "fade", montage: false, why: null }; export function useRenderCuts(project: string, take: string | null, key: string): RenderCuts { // Each answer says which cut it is for: another cut's (a take switched to, before its own // answer) is never returned -- not even for the render between the switch and its effect. const which = `${project}|${take ?? ""}`; const [got, setGot] = useState({ ...MEASURING, for: which }); useEffect(() => { let live = true; const q = new URLSearchParams({ project, ...(take ? { take } : {}) }); fetch(`/api/report/cut-points?${q}`, { cache: "no-store" }) .then(async (r) => { const j = (await r.json().catch(() => null)) as { clips?: Row[]; transitionStyle?: string; montage?: boolean; error?: string } | null; if (!live) return; if (!r.ok || !j?.clips) { setGot({ state: "estimate", cuts: null, style: "fade", montage: false, why: j?.error ?? `HTTP ${r.status}`, for: which }); return; } const cuts = new Map(); for (const c of j.clips) if (c && !c.missing && !c.error && Number.isFinite(c.cutStart) && Number.isFinite(c.cutEnd)) cuts.set(c.id, c); setGot({ state: "render", cuts, style: j.transitionStyle ?? "fade", montage: !!j.montage, why: null, for: which }); }) .catch((e: unknown) => { if (live) setGot({ state: "estimate", cuts: null, style: "fade", montage: false, why: e instanceof Error ? e.message : String(e), for: which }); }); return () => { live = false; }; }, [project, take, key, which]); // The same cut with an edge moved: its edges stand until the new ones are measured (only what moved is). return got.for === which ? got : MEASURING; }