feat: first commit
This commit is contained in:
43
admin/src/routes.ts
Normal file
43
admin/src/routes.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { AuthParams } from '@/utils/authentication';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
export type IRoute = AuthParams & {
|
||||
name: string;
|
||||
key: string;
|
||||
// 当前页是否展示面包屑
|
||||
breadcrumb?: boolean;
|
||||
children?: IRoute[];
|
||||
// 当前路由是否渲染菜单项,为 true 的话不会在菜单中显示,但可通过路由地址访问。
|
||||
ignore?: boolean;
|
||||
icon?: string
|
||||
};
|
||||
|
||||
const generatorMenu = (data, parentId = 0) => {
|
||||
const menu = [];
|
||||
for (const item of data) {
|
||||
item.key = item.url;
|
||||
if (item.parentId === parentId) {
|
||||
const children = generatorMenu(data, item.id);
|
||||
if (children.length) {
|
||||
item.children = children;
|
||||
}
|
||||
menu.push(item);
|
||||
}
|
||||
}
|
||||
return menu;
|
||||
};
|
||||
|
||||
const useRoute = (menus): [IRoute[], string] => {
|
||||
const routes = generatorMenu(menus);
|
||||
const defaultRoute = useMemo(() => {
|
||||
const first = routes[0];
|
||||
if (first) {
|
||||
return first?.children?.[0]?.key || first.key;
|
||||
}
|
||||
return '';
|
||||
}, [routes]);
|
||||
return [routes, defaultRoute];
|
||||
|
||||
};
|
||||
|
||||
export default useRoute;
|
||||
Reference in New Issue
Block a user