Files
co-lite/solvway-admin-portal/app/lib/aclSidebar.ts
T
ngqileandClaude Opus 5.5 a8bfcf5f6d feat(portal): Translations and Languages pages
Ported from babyboo and trimmed to back-office labels: list with search
and the Missing Chinese filter, edit panel with placeholder checks and an
unsaved-changes guard, export, import with preview and summary, and the
Languages page with guarded switches. A blank translation is no longer
treated as an old placeholder mismatch, so Save blocks a broken new one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-02 13:17:54 +08:00

122 lines
5.0 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[];
}
// ponytail: fixed map; a new module lands in the trailing unlabelled section until added here.
// `label` is an i18n key; the first section has none (Dashboard stands alone).
export const SIDEBAR_SECTIONS: { label?: string; modules: string[] }[] = [
{ modules: ["BO_DASHBOARD"] },
{ label: "SidebarSectionEvents", modules: ["BO_EVENTS", "BO_EVENT_LEADS"] },
{ label: "SidebarSectionLeads", modules: ["BO_LEADS", "BO_WHATSAPP", "BO_INTERESTS"] },
{ label: "SidebarSectionAccess", modules: ["BO_USERS", "BO_ROLES", "BO_LOGIN_SECURITY"] },
{ label: "SidebarSectionSystem", modules: ["BO_COMPANIES", "BO_EMAILS", "BO_AUDIT_LOG", "BO_MESSAGES", "BO_LOCALES"] },
];
// Icon overrides by module name, so a shared seed icon can differ in the sidebar.
export const SIDEBAR_ICONS: Record<string, string> = { BO_LOGIN_SECURITY: "LockKeyhole" };
/** Splits links into the SIDEBAR_SECTIONS order; unknown links trail in an unlabelled section; empty sections drop. */
export function groupSidebar(links: SidebarLink[]): { label?: string; links: SidebarLink[] }[] {
const known = SIDEBAR_SECTIONS.flatMap((s) => s.modules);
const withIcon = (l: SidebarLink) => ({ ...l, iconName: SIDEBAR_ICONS[l.name] ?? l.iconName });
const sections = [
...SIDEBAR_SECTIONS.map(({ label, modules }) => ({
label,
links: modules.flatMap((m) => links.filter((l) => l.name === m)).map(withIcon),
})),
{ label: undefined, links: links.filter((l) => !known.includes(l.name)).map(withIcon) },
];
return sections.filter((s) => s.links.length > 0);
}
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;
}