import { channelsDirFor, readManifest } from "@/lib/projects/report.mjs"; import { manifestToken } from "@/lib/report/manifest.mjs"; import { deckGeometry, deckOn, feedGeometry, feedOn } from "umtool-report-to-video/deck"; import { postItemPlan, panelEntry } from "umtool-report-to-video/post-item"; import { playableEntry } from "umtool-report-to-video/post-thread"; import { planContext } from "umtool-report-to-video/post-conversation"; import { projectCombos } from "umtool-report-to-video/post-combo-prints"; import { mediaSceneOf, sceneEntry } from "umtool-report-to-video/post-media-scene"; import type { PostPlan } from "@/components/projects/review/PostItemPlayer"; import type { ComboInfo } from "@/components/projects/review/ComboBadge"; // --------------------------------------------------------------------------- // THE POST BENCH'S DATA (report-to-video/post-item.mjs; knobs.md, "A post item"): what PostBench is drawn from // for one post item of the draft -- its entry, the frame's box, its plan in the cut (the item before it, its // conversation's scene, its media scene, its combo counted over the whole cut), its neighbours among the posts. // One function, two callers: the bench's own page (PostBenchPage.tsx) and the workspace's inspector // (GET /api/report/post-bench), so the two draw the same item the same way (umtool redesign L7a). // --------------------------------------------------------------------------- type Entry = Record & { id: string; type?: string }; export type PostBenchData = { entry: Entry; token: string | null; box: { width: number; height: number }; plan: PostPlan; scene: ReturnType; render: Record; before: Record | null; conversation: Record | null; combo: ComboInfo | null; /** The post items of the cut, in order (the bench's ‹ prev / next ›). */ neighbours: string[]; /** This item's place among them, 0-based. */ index: number; }; /** The combos counted per state of a draft (`\0`), the newest few kept. */ const combosByDraft = new Map>(); const COMBOS_KEPT = 8; /** The post item `id` of the draft at `dir`, or null when the manifest has no such post item. */ export async function postBenchData(dir: string, id: string): Promise { const m = (await readManifest(dir)) as Record | null; if (!m) return null; const tl = (Array.isArray(m.timeline) ? m.timeline : []) as Entry[]; const at = tl.findIndex((e) => e?.type === "post" && e.id === id); if (at < 0) return null; const entry = tl[at]; const render = (m.render ?? {}) as Record; const box = deckOn(render) ? (feedOn({ render, posts: (m.posts ?? []) as never[], entries: tl as never[] }) ? feedGeometry(render).footage : deckGeometry(render).footage) : { width: Number(render.width ?? 1920), height: Number(render.height ?? 1080) }; const neighbours = tl.filter((e) => e?.type === "post").map((e) => e.id); // What it opens on (post-conversation.mjs): the item before it and the scene its run leaves on screen. const ctx = planContext(tl, at, render) as { prev: Record | null; scene: Record | null }; // Its combo (post-combo.mjs): counted over the whole cut, as the build counts it -- once per state of the draft // (its token): counting hashes every attachment, and the workspace asks again on each post block's click. const token = await manifestToken(dir); const key = `${dir}\0${token ?? ""}`; let counting = combosByDraft.get(key); if (!counting) { counting = projectCombos(dir, m, { channelsDir: channelsDirFor(dir, m) }).catch(() => ({ combos: {}, groups: [] })); combosByDraft.set(key, counting); while (combosByDraft.size > COMBOS_KEPT) combosByDraft.delete(combosByDraft.keys().next().value as string); } const counted = (await counting) as { combos: unknown; groups: unknown }; const combos = counted.combos as Record; // Its media scene (post-media-scene.mjs): its neighbours with the same attachment, by the combo's groups. const scene = mediaSceneOf(tl, at, counted.groups as never, render); return { entry, token, box: { width: box.width, height: box.height }, // Never a read the build refuses (post-thread.mjs playableEntry): planned as the bench's player plays it. plan: postItemPlan(playableEntry(panelEntry(sceneEntry(entry, scene), render)), render, { ...ctx, mediaScene: ctx.scene ? null : scene }) as unknown as PostPlan, scene, render, before: ctx.prev, conversation: ctx.scene, combo: combos[id] ?? null, neighbours, index: neighbours.indexOf(id), }; }