import { invalidateProjects } from "@/lib/projects"; import { withEditNotes } from "@/lib/report/guard"; import { StaleToken, StructureRefused, manifestToken, updateTeaser } from "@/lib/report/manifest.mjs"; import { insertTeaser, teaserLimits, teaserLineOf } from "@/lib/report/inspectorEdits.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; export const dynamic = "force-dynamic"; // A TEASER OR CLOSING CARD (umtool redesign L6, the inspector's Insert a card): a `type: "teaser"` entry -- its // lines (the first its title; each with a break and a hold), its beat -- inserted at the start, the end, or // beside an entry, or an existing one's lines and beat changed. Checked by the build's validateTeaser; one write, // one undo point each. `umtool timeline

teaser-insert` / `timeline

teaser ` are the twins. // // GET ?project= the cut's entries (id, type, a label) for the position, // its teasers as written, the limits, the token // POST { project, token, op: "insert", spec: { id?, lines, beat? }, position: "start" | "end" | { after } | { before } } // POST { project, token, op: "set", id, patch: { lines?, beat? } } const noStore = { "cache-control": "no-store" }; type Entry = { id?: string; type?: string; heading?: string; title?: string; label?: string; lines?: unknown[]; beat?: number; video?: string }; export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const tl = (Array.isArray((r.manifest as { timeline?: unknown }).timeline) ? (r.manifest as { timeline: Entry[] }).timeline : []) as Entry[]; return Response.json( { entries: tl.map((e, at) => ({ id: String(e?.id ?? ""), type: String(e?.type ?? "?"), at, label: e?.heading ?? e?.title ?? e?.label ?? e?.video ?? null })), teasers: tl.flatMap((e, at) => (e?.type === "teaser" ? [{ id: String(e.id), at, lines: e.lines ?? [], beat: typeof e.beat === "number" ? e.beat : null }] : [])), limits: teaserLimits(), token: await manifestToken(r.project.dir), }, { headers: noStore }, ); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/teaser"); if (refused) return refused; const cross = crossSiteRefusal(request.headers); if (cross) return Response.json({ error: cross }, { status: 403 }); let body: Record; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); if (typeof body.token !== "string" || !body.token) return Response.json({ error: "token is required: GET the route first (a write over a newer draft is refused, never silent)" }, { status: 400 }); const token = body.token as string; try { if (body.op === "insert") { const { result, editNotes } = await withEditNotes(r.project, () => insertTeaser(r.project.dir, (body.spec ?? {}) as Record, body.position ?? "end", { token })); invalidateProjects(); return Response.json({ ok: true, id: result.id, at: result.at, entry: result.entry, token: result.token, editNotes }, { headers: noStore }); } if (body.op === "set") { const patch = { ...((body.patch ?? {}) as Record) }; // The lines as the insert writes them: a line that is only its words is a string, empty keys left out. if (Array.isArray(patch.lines)) patch.lines = patch.lines.map((l: unknown) => teaserLineOf(l)); const { result, editNotes } = await withEditNotes(r.project, () => updateTeaser(r.project.dir, String(body.id ?? ""), patch, { token })); return Response.json({ ok: true, id: result.id, entry: result.entry, token: result.token, editNotes }, { headers: noStore }); } return Response.json({ error: "op is insert or set" }, { status: 400 }); } catch (e) { if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409 }); if (e instanceof StructureRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400 }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }