Files
archie/ui/src/lib/nav-tree.ts
T
sam 426085a890 feat(webui): Settings > History, and a resizable Settings sidebar
History replaces the Audit card on every settings page: one change log for
all settings, filterable by section, actor and time window, with the
filters in the query so each page's History link opens it filtered to that
page. Rows show the old and new value as a diff and expand to the full
value; Restore still puts a resource back to the version before a change.

The sidebar's edge drags (or takes arrow keys) between 180 and 420px,
resets on double-click and remembers its width per browser.
2026-09-25 15:10:07 +10:00

90 lines
2.3 KiB
TypeScript

export interface NavEntry {
path: string;
label: string;
description: string;
section?: string;
dividerBefore?: string;
soon?: boolean;
}
export interface NavLink {
kind: "link";
entry: NavEntry;
}
export interface NavGroup {
kind: "group";
label: string;
items: NavEntry[];
}
export type NavNode = NavLink | NavGroup;
export interface NavRoute {
path: string;
meta?: {
label?: string;
description?: string;
section?: string;
soon?: boolean;
};
}
export type NavSpec =
| { kind: "link"; path: string }
| {
kind: "group";
label: string;
sections: { label?: string; paths: string[] }[];
};
/**
* buildNav turns the specs into the bar, with the routes the serving
* composition cannot back removed. A group whose every child is hidden is
* dropped; each section's label moves to its first visible item. jumpTargets
* is everything the bar reaches plus the unlisted pages, for the Jump to box.
*/
export function buildNav(
table: NavRoute[],
specs: NavSpec[],
hidden: string[] = [],
unlisted: string[] = [],
): { tree: NavNode[]; jumpTargets: NavEntry[] } {
const entryFor = (path: string): NavEntry => {
const route = table.find((r) => r.path === path);
if (!route) throw new Error(`nav references ${path}, which is not a route`);
return {
path,
label: route.meta?.label ?? path,
description: route.meta?.description ?? "",
section: route.meta?.section,
soon: route.meta?.soon,
};
};
const visible = (path: string) => !hidden.includes(path);
const tree: NavNode[] = [];
for (const spec of specs) {
if (spec.kind === "link") {
if (visible(spec.path))
tree.push({ kind: "link", entry: entryFor(spec.path) });
continue;
}
const items = spec.sections.flatMap((section) =>
section.paths
.filter(visible)
.map(entryFor)
.map((entry, i) =>
i === 0 ? { ...entry, dividerBefore: section.label ?? "" } : entry,
),
);
if (items.length > 0) tree.push({ kind: "group", label: spec.label, items });
}
const reached = tree.flatMap((node) =>
node.kind === "link" ? [node.entry] : node.items,
);
const extra = unlisted.filter(visible).map(entryFor);
return { tree, jumpTargets: [...reached, ...extra] };
}