76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { Home, Code, Calendar, Settings, Users, Folder, ShieldCheck } from "lucide-react";
|
|
import type { ComponentType } from "react";
|
|
|
|
export type ActivePage = "home" | "scripts" | "schedules" | "system";
|
|
export type SubPage = "users" | "projects" | "roles";
|
|
|
|
export type NavigationItem = {
|
|
/** 菜单显示名 */
|
|
label: string;
|
|
/** 渲染在菜单前的图标组件(lucide-react 或等价) */
|
|
icon: ComponentType;
|
|
/** 入口页 — 点击后由 guardedNavigate 处理 */
|
|
page: ActivePage;
|
|
/** 子页段,仅 children 项使用 */
|
|
sub?: SubPage;
|
|
/** 命中高亮的 pathname;不填则按 pathForPage(page) 派生 */
|
|
activePath?: string;
|
|
/** 当前端可访问的权限码;未配置时视为所有人可访问 */
|
|
permission?: string;
|
|
/** 子菜单条目 */
|
|
children?: NavigationItem[];
|
|
};
|
|
|
|
export function pathForPage(page: ActivePage): string {
|
|
if (page === "home") return "/workbench";
|
|
return `/${page}`;
|
|
}
|
|
|
|
function normalizePath(pathname: string): string {
|
|
const trimmed = pathname.replace(/\/+$/, "");
|
|
return trimmed || "/";
|
|
}
|
|
|
|
/** 根据当前 pathname 解析顶栏标题:二级菜单显示子项名称,一级菜单显示自身名称。 */
|
|
export function pageTitleFromPath(pathname: string): string {
|
|
const path = normalizePath(pathname);
|
|
|
|
for (const item of navigation) {
|
|
if (item.children) {
|
|
for (const child of item.children) {
|
|
const childPath =
|
|
child.activePath ?? `${pathForPage(child.page)}/${child.sub}`;
|
|
if (path === childPath) {
|
|
return child.label;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const item of navigation) {
|
|
const itemPath = item.activePath ?? pathForPage(item.page);
|
|
if (path === itemPath) {
|
|
return item.label;
|
|
}
|
|
}
|
|
|
|
return "工作台";
|
|
}
|
|
|
|
export const navigation: NavigationItem[] = [
|
|
{ label: "工作台", icon: Home, page: "home", permission: "dashboard:view" },
|
|
{ label: "构建脚本", icon: Code, page: "scripts", permission: "script:view" },
|
|
{ label: "调度配置", icon: Calendar, page: "schedules", permission: "schedule:view" },
|
|
{
|
|
label: "系统管理",
|
|
icon: Settings,
|
|
page: "system",
|
|
permission: "system:view",
|
|
children: [
|
|
{ label: "用户管理", icon: Users, page: "system", sub: "users", permission: "system:user:view" },
|
|
{ label: "项目管理", icon: Folder, page: "system", sub: "projects", permission: "system:project:view" },
|
|
{ label: "角色管理", icon: ShieldCheck, page: "system", sub: "roles", permission: "system:role:view" },
|
|
],
|
|
},
|
|
];
|