117 lines
4.5 KiB
TypeScript
117 lines
4.5 KiB
TypeScript
/**
|
||
* 侧边栏菜单合并 hook —— 把已注册的「iframe 类型」轻应用动态注入静态菜单。
|
||
*
|
||
* 静态结构来自 sidebar-menu.ts;每个启用的 iframe 应用按其 `mountLocation`(父菜单 id)
|
||
* 作为新子项注入对应分组(XK中任务 / PG中任务 / TY中任务 / 综合管理),点击进入
|
||
* iframe 宿主页 `/page/strategy/light-app/view/:appId`。
|
||
*
|
||
* 浅色 PageSidebar 与深色 PageSidebarDark 共用此 hook,保证两套皮肤同步。
|
||
*/
|
||
import { useMemo } from "react";
|
||
import { useLocation } from "react-router-dom";
|
||
import { Layout } from "lucide-react";
|
||
|
||
import { getStoredAuth } from "@/core/auth";
|
||
import { desensitize, makeDesensitizer } from "@/lib/desensitize";
|
||
import { useLightApps, type LightApp } from "@/strategy-components/api/light-apps";
|
||
import { useMenuOverrides } from "@/strategy-components/api/menu-overrides";
|
||
import { useSensitiveWords, wordsToMap } from "@/strategy-components/api/sensitive-words";
|
||
|
||
import { applyMenuOverrides } from "./menu-overrides";
|
||
import {
|
||
filterMenuItemsByRole,
|
||
getActiveThirdLevelItems,
|
||
sidebarMenuItems,
|
||
type MenuItem,
|
||
} from "./sidebar-menu";
|
||
|
||
function lightAppLeaf(app: LightApp): MenuItem {
|
||
return {
|
||
id: `light-app-${app.appId}`,
|
||
label: app.name,
|
||
icon: Layout,
|
||
path: `/page/strategy/light-app/view/${app.appId}`,
|
||
};
|
||
}
|
||
|
||
/** 在菜单树(不可变克隆)中,把 iframe 应用按 mountLocation 注入对应父节点末尾。 */
|
||
export function injectLightApps(items: MenuItem[], apps: LightApp[]): MenuItem[] {
|
||
if (apps.length === 0) return items;
|
||
|
||
const byMount = new Map<string, LightApp[]>();
|
||
for (const app of apps) {
|
||
if (!app.mountLocation) continue;
|
||
const list = byMount.get(app.mountLocation) ?? [];
|
||
list.push(app);
|
||
byMount.set(app.mountLocation, list);
|
||
}
|
||
if (byMount.size === 0) return items;
|
||
|
||
const walk = (nodes: MenuItem[]): MenuItem[] =>
|
||
nodes.map((node) => {
|
||
const children = node.children ? walk(node.children) : undefined;
|
||
const extra = byMount.get(node.id);
|
||
if (!extra || extra.length === 0) {
|
||
return children ? { ...node, children } : node;
|
||
}
|
||
return { ...node, children: [...(children ?? []), ...extra.map(lightAppLeaf)] };
|
||
});
|
||
|
||
return walk(items);
|
||
}
|
||
|
||
/** 递归对菜单标签做脱敏(仅改 `label`,path/id/icon 不动,保证高亮与路由不受影响)。 */
|
||
function desensitizeLabels(items: MenuItem[], fn: (text: string) => string): MenuItem[] {
|
||
return items.map((node) => ({
|
||
...node,
|
||
label: fn(node.label),
|
||
children: node.children ? desensitizeLabels(node.children, fn) : node.children,
|
||
}));
|
||
}
|
||
|
||
function useDesensitizeLabel() {
|
||
const { data: words = [] } = useSensitiveWords();
|
||
return useMemo(
|
||
() => (words.length > 0 ? makeDesensitizer(wordsToMap(words)) : desensitize),
|
||
[words],
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 返回合并了 iframe 轻应用、套用了后端「菜单覆盖层」(改名/停用/排序/层级)、
|
||
* 按 admin 角色过滤、并对标签做**展示层脱敏**后的菜单。
|
||
*
|
||
* 管线顺序:静态菜单 → 注入 iframe 轻应用 → 套覆盖层(停用项被剔除)→ 角色过滤 → 标签脱敏。
|
||
* 覆盖层作用在合并后的整棵树上,所以 iframe 轻应用节点同样可被改名/停用/移层级。
|
||
*
|
||
* 脱敏直接用 `/api/sensitive-words` 查询结果构造(后台非空时用其映射,否则回退到内置种子的
|
||
* 全局 {@link desensitize}),避免依赖 {@link replaceDesensitizeMap} 的 effect 回灌时序。
|
||
*/
|
||
export function useSidebarMenuItems(isAdmin: boolean): MenuItem[] {
|
||
const { data: iframeApps = [] } = useLightApps({ appType: "iframe", enabledOnly: true });
|
||
const { data: overrides = [] } = useMenuOverrides();
|
||
const desensitizeLabel = useDesensitizeLabel();
|
||
return useMemo(
|
||
() =>
|
||
desensitizeLabels(
|
||
filterMenuItemsByRole(
|
||
applyMenuOverrides(injectLightApps(sidebarMenuItems, iframeApps), overrides),
|
||
isAdmin,
|
||
),
|
||
desensitizeLabel,
|
||
),
|
||
[iframeApps, overrides, isAdmin, desensitizeLabel],
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 当前路由所在「激活二级项」的三级子项(浅色侧边栏右侧列用)。
|
||
* 供 PageRoutes(主内容左内边距随展开自适应)与浅色侧边栏共用同一份判定逻辑。
|
||
*/
|
||
export function useActiveThirdLevel(): MenuItem[] {
|
||
const isAdmin = getStoredAuth()?.system_role === "admin";
|
||
const items = useSidebarMenuItems(!!isAdmin);
|
||
const { pathname } = useLocation();
|
||
return useMemo(() => getActiveThirdLevelItems(items, pathname), [items, pathname]);
|
||
}
|