import { invalidateProjects } from "@/lib/projects";
import { withEditNotes } from "@/lib/report/guard";
import { StaleToken, manifestToken } from "@/lib/report/manifest.mjs";
import { SUBTITLE_MAX, TITLE_MAX, updateTitle } 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";
// THE PROJECT'S TITLE AND SUBTITLE (umtool redesign L6, item 7; 9 Nov: four news projects' were set with jq):
// the manifest's `title` and `subtitle`. `umtool title
"
" [--subtitle "…"]` is the same writer.
//
// GET ?project= { title, subtitle, limits, token }
// PUT { project, token, title?, subtitle? } set them (subtitle "" or null removes it); one undo point
const noStore = { "cache-control": "no-store" };
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 m = r.manifest as { title?: unknown; subtitle?: unknown };
return Response.json(
{
title: typeof m.title === "string" ? m.title : null,
subtitle: typeof m.subtitle === "string" ? m.subtitle : null,
limits: { title: TITLE_MAX, subtitle: SUBTITLE_MAX },
token: await manifestToken(r.project.dir),
},
{ headers: noStore },
);
}
export async function PUT(request: Request) {
const refused = refuseCaller(request, "/api/report/title");
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 });
const patch: Record = {};
if ("title" in body) patch.title = body.title;
if ("subtitle" in body) patch.subtitle = body.subtitle;
try {
const { result, editNotes } = await withEditNotes(r.project, () => updateTitle(r.project.dir, patch, { token: typeof body.token === "string" ? body.token : null }));
invalidateProjects();
return Response.json({ ok: true, ...result, editNotes }, { headers: noStore });
} catch (e) {
if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409 });
return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 });
}
}