Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
99 lines
3.8 KiB
TypeScript
99 lines
3.8 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[];
|
|
/** Extra routes on which this link counts as the current page; set by merged sidebar entries. */
|
|
activeUrls?: string[];
|
|
/** Active only on exactly this path, not its sub-paths (the /inventory link, merge-inventory-page). */
|
|
exact?: boolean;
|
|
}
|
|
|
|
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, ...(link.activeUrls ?? [])]
|
|
.filter((url) => {
|
|
// A group's url is "" -- never a page, else `startsWith("/")` matches every path.
|
|
if (!url) return false;
|
|
if (url === "/" || link.exact) 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;
|
|
}
|