Skip to content

Commit 97454a9

Browse files
committed
optimize: 优化关于menuUtil的代码
1 parent 0c051c3 commit 97454a9

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/features/menu/MenuProvider.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import type { FC, PropsWithChildren } from 'react';
33

44
import { routes } from '@/router';
55
import { selectActiveFirstLevelMenuKey, setActiveFirstLevelMenuKey } from '@/store/slice/tab';
6-
import { getActiveFirstLevelMenuKey } from '@/store/slice/tab/shared';
76

87
import { useLang } from '../lang';
98
import { useRoute } from '../router';
109

11-
import { filterRoutesToMenus } from './MenuUtil';
10+
import { filterRoutesToMenus, getActiveFirstLevelMenuKey } from './MenuUtil';
1211
import { MixMenuContext } from './menuContext';
1312

1413
const MenuProvider: FC<PropsWithChildren> = ({ children }) => {
@@ -47,7 +46,7 @@ const MenuProvider: FC<PropsWithChildren> = ({ children }) => {
4746
routeKey = getActiveFirstLevelMenuKey(route);
4847
}
4948

50-
dispatch(setActiveFirstLevelMenuKey(routeKey));
49+
dispatch(setActiveFirstLevelMenuKey(routeKey || ''));
5150
}
5251

5352
const mixMenuContext = useCreation(

src/features/menu/MenuUtil.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ import { $t } from '@/locales';
1111
export function filterRoutesToMenus(routes: RouteObject[]) {
1212
const menus: App.Global.Menu[] = [];
1313

14+
const cacheRoutes: string[] = [];
15+
1416
for (const route of routes) {
1517
// 如果节点存在 path(注意:这里假设空字符串或 undefined 均视为无 path)
16-
if (route.path && !route.handle?.hideInMenu) {
18+
if (route.handle?.keepAlive) {
19+
cacheRoutes.push(route.path as string);
20+
}
21+
if (route.path && !route.handle?.hideInMenu && route.id?.includes('base')) {
1722
// 如果存在 children,则递归处理
1823
const newNode = getGlobalMenuByBaseRoute(route);
1924

@@ -62,3 +67,20 @@ export function getGlobalMenuByBaseRoute(route: RouteObject): App.Global.Menu {
6267

6368
return menu;
6469
}
70+
71+
/**
72+
* Get active first level menu key
73+
*
74+
* @param route
75+
*/
76+
export function getActiveFirstLevelMenuKey(route: App.Global.TabRoute) {
77+
const { activeMenu, hideInMenu } = route.handle;
78+
79+
const name = route.pathname;
80+
81+
const routeName = (hideInMenu ? activeMenu : name) || name;
82+
83+
const [firstLevelRouteName] = routeName;
84+
85+
return firstLevelRouteName;
86+
}

src/pages/(blank)/login/code-login/config.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,3 @@ export const config = {
22
i18nKey: 'route.login_code-login',
33
title: 'login_code-login'
44
};
5-
6-
export const loader = undefined;
7-
8-
export const action = undefined;
9-
10-
export const shouldRevalidate = undefined;

0 commit comments

Comments
 (0)