Files
co-lite/solvway-admin-portal/app/lib/aclSidebar.ts
T
ngqileandClaude Opus 5.5 1071ddaa2a feat(admin-portal): cut babyboo portal down to Co-Lite login, users and roles
Removes every other screen, API route, component, test and the dependencies only
they used. Cookie colite_admin, Co-Lite branding, BO_USERS/BO_ROLES modules only.
The user form no longer loads branches, events or country scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:24:30 +08:00

95 lines
3.5 KiB
TypeScript

import type { AdminModule } from "@/app/lib/types";
// Maps the aclMenu tree (spec: "Access Is Gated By Role And Module, As UX
// Only") into sidebar nav entries, replacing the static
// src/components/app/nav-config.ts approach. Task 6 wires this into the
// sidebar component/shell.
export interface SidebarLink {
name: string;
url: string;
iconName?: string;
children: SidebarLink[];
}
const byId = (nodes: AdminModule[] | undefined) => [...(nodes ?? [])].sort((a, b) => (a.id ?? 0) - (b.id ?? 0));
/**
* Builds sidebar entries from the aclMenu tree.
*
* - `R` (root) is never a link itself; only its subtree is walked.
* - `M` (module) nodes become links when they carry a `url`.
* - An `M` node WITHOUT a `url` is a grouping node: if it has at least one
* linkable descendant, it becomes a collapsible group link (`url: ""`)
* whose children are nested underneath it (NOT hoisted to the parent
* level). If it has no linkable descendants, it renders nothing (no empty
* group).
* - `A` (action) nodes are NEVER links, and neither is anything nested under
* them — they exist only so `hasModule` can gate buttons/actions, not to
* appear in navigation. A module absent from the tree entirely is simply
* absent from the result.
*/
export function sidebarFromAclMenu(root: AdminModule | null | undefined): SidebarLink[] {
if (!root) return [];
function build(node: AdminModule): SidebarLink[] {
if (node.type === "A") return [];
// The tree's order is module id order (module-id-category-order); the portal keeps no order of its own.
const children = byId(node.children).flatMap(build);
if (node.type === "M" && node.url) {
return [{ name: node.name, url: node.url, iconName: node.iconName, children }];
}
if (node.type === "M" && !node.url) {
// Grouping node: only render it if it has linkable descendants.
// Otherwise it would show as an empty, useless collapsible group.
if (children.length === 0) return [];
return [{ name: node.name, url: "", iconName: node.iconName, children }];
}
// Root: not a link, but its linkable descendants still surface at this level.
return children;
}
return byId(root.children).flatMap(build);
}
/** The url of `link` (its own or a merged activeUrl) that matches `pathname`, longest first; undefined if none. */
export function matchedUrl(link: SidebarLink, pathname: string): string | undefined {
return [link.url]
.filter((url) => {
// A group's url is "" -- never a page, else `startsWith("/")` matches every path.
if (!url) return false;
if (url === "/") return pathname === url;
return pathname === url || pathname.startsWith(`${url}/`);
})
.sort((a, b) => b.length - a.length)[0];
}
/**
* The single current page: ancestors ending in the one leaf matching `pathname` most specifically
* (longest url wins, first in tree order on a tie). Empty when nothing matches. Sidebar and
* Breadcrumbs both read this, so they cannot disagree.
*/
export function currentTrail(links: SidebarLink[], pathname: string): SidebarLink[] {
let best: SidebarLink[] = [];
let bestLen = -1;
function walk(nodes: SidebarLink[], ancestors: SidebarLink[]) {
for (const node of nodes) {
if (node.children.length > 0) {
walk(node.children, [...ancestors, node]);
} else {
const len = matchedUrl(node, pathname)?.length ?? -1;
if (len > bestLen) {
bestLen = len;
best = [...ancestors, node];
}
}
}
}
walk(links, []);
return best;
}