Files
archie/ui/src/lib/nav-tree.ts
T
sam 32642af4a9 feat(ui): give organisation, instance and account clear homes
The Access area mixed organisation, instance and personal settings under
one name. Split them: /org is the organisation's own area (members,
identities, connections, models, schedules, playbooks), /settings is
instance administration only, and personal security stays in the account
menu. Setup checklist steps now link to the page that fixes them.
2026-10-17 09:01:41 +11:00

125 lines
3.3 KiB
TypeScript

export interface NavEntry {
path: string;
label: string;
description: string;
section?: string;
dividerBefore?: string;
}
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;
};
}
/** One route as the router declares it, children included. */
export interface NavRouteNode extends NavRoute {
meta?: {
label?: string;
description?: string;
section?: string;
navPath?: string;
};
children?: NavRouteNode[];
}
/**
* flattenRoutes resolves every route's address, parent before child. A nav
* spec names the address the browser visits, so a relative child path is only
* useful once joined to its parent; a child with an absolute path is taken as
* written.
*/
export function flattenRoutes(
list: NavRouteNode[],
parent = "",
): NavRouteNode[] {
return list.flatMap((route) => {
const joined = route.path.startsWith("/")
? route.path
: `${parent}/${route.path}`.replace(/\/+/g, "/");
const path = joined.length > 1 ? joined.replace(/\/$/, "") : joined;
return [
{ ...route, path },
...flattenRoutes(route.children ?? [], path),
];
});
}
export type NavSpec =
| { kind: "link"; path: string; label?: 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,
};
};
const visible = (path: string) => !hidden.includes(path);
const tree: NavNode[] = [];
for (const spec of specs) {
if (spec.kind === "link") {
if (visible(spec.path)) {
const entry = entryFor(spec.path);
tree.push({
kind: "link",
entry: spec.label ? { ...entry, label: spec.label } : entry,
});
}
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] };
}