import type { Route } from "./+types/repo-code-graph"; import { refsErrorCode } from "@refs/backend/contract"; import { refs, fullName } from "../lib/session"; /** * The code map of one commit, as JSON. A commit never changes, so a ready map is cached for good and a second * visit opens it without a request. A map that is still building answers 202, so the page asks again. */ export async function loader({ params, context }: Route.LoaderArgs) { try { const repo = await refs(context).repo(fullName(params)); const map = await repo.codeGraph(params.commit); if (map.kind !== "ready") return Response.json(map, { status: map.kind === "indexing" ? 202 : 409, headers: { "cache-control": "no-store" } }); return new Response(JSON.stringify(map.graph), { headers: { "content-type": "application/json", "cache-control": `${repo.summary.visibility === "public" ? "public" : "private"}, max-age=31536000, immutable` }, }); } catch (error) { if (refsErrorCode(error)) return Response.json({ kind: "unavailable" }, { status: 404 }); throw error; } }