import { composeTeaserPreviews } from "@/lib/report/onscreen.mjs"; import { analysisPreviewSrc, resolveReport, teaserPreviewSrc } from "@/lib/report/serve.mjs"; import { analysisSeconds } from "umtool-report-to-video/analysis-plan"; import { selectVariant } from "umtool-report-to-video/build-video"; import { teaserSeconds, transitionOf } from "umtool-report-to-video/deck"; import { cachedTakeManifest, takePlayDir, writePlayManifest } from "@/lib/report/takeManifest.mjs"; import { isTakeId } from "@/lib/report/takes.mjs"; export const dynamic = "force-dynamic"; // THE TEASERS' PAGES for Play the cut (lane leaflit-labels): every `teaser` // entry of the cut -- a text card, an X post quoted on one -- composed for // preview by the build's own page (compose-chrome's `teaser` region, under // out//chrome/teaser-preview-/, Play's seek appended: // onscreen.mjs composeTeaserPreviews), so Play draws the card as the render // does instead of a placeholder. No render. With or without a deck: a teaser is // full frame, the deck (when there is one, and the card counts) is laid over it // by the deck's own page. // // POST { project, take? } -> { teasers: [{ id, seconds, width, height, src } | // { id, error }] }. `seconds` is the card's length (deck.mjs teaserSeconds); the // page is seeked in seconds from its start. A take's pages are composed under // its own directory and served under `@take//`, as the deck's are. export async function POST(request: Request) { let body: Record; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const r = await resolveReport(String(body.project ?? ""), body.variant ? String(body.variant) : null); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const take = body.take === undefined || body.take === null || body.take === "" ? null : String(body.take); if (take !== null && !isTakeId(take)) return Response.json({ error: "take is a take id" }, { status: 400 }); let manifest = r.manifest; let target: { dir: string; manifestFile?: string } = r.project; if (take) { try { manifest = (await cachedTakeManifest(r.project.dir, take)).manifest; target = { dir: takePlayDir(r.project.dir, take), manifestFile: await writePlayManifest(r.project.dir, take, manifest) }; } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 404 }); } } const selected = selectVariant(manifest, r.variant); const render = (selected.render ?? {}) as Record; // An analysis item (analysis-plan.mjs) is a full-frame page as a teaser is: composed and served the same way. const timeline = ((selected.timeline ?? []) as Record[]).filter((e) => e && (e.type === "teaser" || e.type === "analysis")); const composed = await composeTeaserPreviews(target, r.variant, timeline); const D = transitionOf(render); const fps = typeof render.fps === "number" ? render.fps : 30; const width = typeof render.width === "number" ? render.width : 1920; const height = typeof render.height === "number" ? render.height : 1080; const stamp = Date.now(); return Response.json( { teasers: composed.map((c) => { const e = timeline.find((x) => x.id === c.id)!; return c.ok ? e.type === "analysis" ? { id: c.id, seconds: analysisSeconds(e), width, height, src: `${analysisPreviewSrc(r.project.id, r.variant, c.id, take)}?v=${stamp}` } : { id: c.id, seconds: teaserSeconds(e, D, fps), width, height, src: `${teaserPreviewSrc(r.project.id, r.variant, c.id, take)}?v=${stamp}` } : { id: c.id, error: c.error ?? "did not compose" }; }), }, { headers: { "cache-control": "no-store" } }, ); }