Files
archie/ui/scripts/pages.ts
T
sam 32fbf8b69c refactor(gateway,ui): the page catalogue is generated from the router (#2050)
The assistant's catalogue was a 33-entry literal in Go, hand-maintained beside
the dashboard's routes: eight real pages were missing from it, so page_index
could not name them, and descriptions had drifted where both copies existed.

The routes move to ui/src/router/routes.ts, free of component imports, and
ui/scripts/pages.ts writes the catalogue from them. The nav and page_index now
read the same generated file, and task pages:check fails when it is stale, the
way the proto, sqlc and OpenAPI outputs do.
2026-11-21 03:53:10 +11:00

64 lines
2.2 KiB
TypeScript

// Writes pages/pages.json from the router's own route table.
//
// The dashboard's pages are the routes it serves, so deriving the catalogue
// from them is what stops the assistant naming a page the dashboard lost or
// missing one it gained. `task pages:check` fails when the committed file is
// stale, which is what makes a new route carry a catalogue entry.
import { writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { routes } from "../src/router/routes.ts";
type Meta = { label?: string; description?: string; nav?: boolean };
type Record_ = {
path: string;
meta?: Meta;
children?: Record_[];
};
export type Page = { path: string; label: string; description: string };
const join_ = (parent: string, path: string): string => {
if (path.startsWith("/")) return path;
if (path === "") return parent;
return `${parent.replace(/\/$/, "")}/${path}`;
};
// dynamic is true for a route the operator reaches with an id in it; the
// assistant cannot name one, so it stays out of the catalogue.
const dynamic = (path: string): boolean => path.includes(":");
const collect = (records: Record_[], parent = "", out: Page[] = []): Page[] => {
for (const route of records) {
const path = join_(parent, route.path);
if (route.meta?.label && route.meta.nav !== false && path && !dynamic(path)) {
out.push({
path,
label: route.meta.label,
description: route.meta.description ?? "",
});
}
if (route.children) collect(route.children, path, out);
}
return out;
};
// The org shell and its index child both answer at /org, so the first label
// for a path wins rather than the catalogue carrying the page twice.
const unique = (pages: Page[]): Page[] => {
const seen = new Set<string>();
return pages.filter((page) => {
if (seen.has(page.path)) return false;
seen.add(page.path);
return true;
});
};
const pages = unique(collect(routes as Record_[]));
const here = dirname(fileURLToPath(import.meta.url));
const target = join(here, "..", "pages", "pages.json");
writeFileSync(target, `${JSON.stringify(pages, null, 2)}\n`);
console.log(`pages: ${pages.length} written to ${target}`);