You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
import { ascend } from "ramda";
|
|
|
|
import type { AppRouteObject, RouteMeta } from "#/router";
|
|
|
|
/**
|
|
* return menu routes
|
|
*/
|
|
export const menuFilter = (items: AppRouteObject[]) => {
|
|
return items
|
|
.filter((item) => {
|
|
const show = item.meta?.key;
|
|
if (show && item.children) {
|
|
item.children = menuFilter(item.children);
|
|
}
|
|
return show;
|
|
})
|
|
.sort(ascend((item) => item.order || Number.POSITIVE_INFINITY));
|
|
};
|
|
|
|
/**
|
|
* 基于 src/router/routes/modules 文件结构动态生成路由
|
|
*/
|
|
export function getRoutesFromModules() {
|
|
const menuModules: AppRouteObject[] = [];
|
|
|
|
const modules = import.meta.glob("./routes/modules/**/*.tsx", {
|
|
eager: true,
|
|
});
|
|
for (const key in modules) {
|
|
const mod = (modules as any)[key].default || {};
|
|
const modList = Array.isArray(mod) ? [...mod] : [mod];
|
|
menuModules.push(...modList);
|
|
}
|
|
return menuModules;
|
|
}
|
|
|
|
/**
|
|
* return the routes will be used in sidebar menu
|
|
*/
|
|
export function getMenuRoutes(appRouteObjects: AppRouteObject[]) {
|
|
// return menuFilter(getMenuModules());
|
|
return menuFilter(appRouteObjects);
|
|
}
|
|
|
|
/**
|
|
* return flatten routes
|
|
*/
|
|
export function flattenMenuRoutes(routes: AppRouteObject[]) {
|
|
return routes.reduce<RouteMeta[]>((prev, item) => {
|
|
const { meta, children } = item;
|
|
if (meta) prev.push(meta);
|
|
if (children) prev.push(...flattenMenuRoutes(children));
|
|
return prev;
|
|
}, []);
|
|
}
|
|
|