Skip to content

Commit af4c27f

Browse files
author
agile.zhou
committed
fix side meun display issue
1 parent 74084ad commit af4c27f

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/AgileConfig.Server.UI/react-ui-antd/src/layouts/BasicLayout.tsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
Settings,
1010
} from '@ant-design/pro-layout';
1111
import ProLayout from '@ant-design/pro-layout';
12-
import React, { useEffect, useMemo, useRef } from 'react';
12+
import React, { useEffect, useMemo, useRef, useCallback } from 'react';
1313
import { Dispatch, getIntl, getLocale } from 'umi';
1414
import { Link, useIntl, connect, history } from 'umi';
1515
import { Result, Button } from 'antd';
16-
import { getCategories, hasFunction } from '@/utils/authority';
16+
import { getCategories } from '@/utils/authority';
1717
import Authorized from '@/utils/Authorized';
1818
import RightContent from '@/components/GlobalHeader/RightContent';
1919
import type { ConnectState } from '@/models/connect';
@@ -38,6 +38,7 @@ export type BasicLayoutProps = {
3838
route: ProLayoutProps['route'] & {
3939
authority: string[];
4040
};
41+
categories?: string[];
4142
settings: Settings;
4243
dispatch: Dispatch;
4344
} & ProLayoutProps;
@@ -46,26 +47,6 @@ export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
4647
};
4748
/** Use Authorized check all menu item */
4849

49-
// Filter menu by categories stored from login (e.g., Application, Configuration, Node, Client, User, Role, Service, System)
50-
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => {
51-
const cats = getCategories();
52-
return menuList
53-
.filter(m => {
54-
// category filter
55-
const category = (m as any).category;
56-
if (category && !cats.includes(category)) return false;
57-
58-
return true;
59-
})
60-
.map((item) => {
61-
const localItem = {
62-
...item,
63-
children: item.children ? menuDataRender(item.children) : undefined,
64-
};
65-
return Authorized.check(item.authority, localItem, null) as MenuDataItem;
66-
});
67-
};
68-
6950
const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
7051
const {
7152
dispatch,
@@ -76,6 +57,38 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
7657
},
7758
} = props;
7859

60+
// keep categories in props to force re-render when user changes; fall back to persisted storage to avoid empty menu during bootstrap
61+
const categories = useMemo<string[]>(() => {
62+
if (props.categories && props.categories.length) {
63+
return props.categories;
64+
}
65+
return getCategories();
66+
}, [props.categories]);
67+
68+
// Filter menu by categories stored from login (e.g., Application, Configuration, Node, Client, User, Role, Service, System)
69+
const menuDataRender = useCallback(
70+
(menuList: MenuDataItem[]): MenuDataItem[] => {
71+
const cats = categories || [];
72+
console.log('menuDataRender categories=', cats);
73+
return menuList
74+
.filter((m) => {
75+
// category filter
76+
const category = (m as any).category;
77+
if (category && !cats.includes(category)) return false;
78+
79+
return true;
80+
})
81+
.map((item) => {
82+
const localItem = {
83+
...item,
84+
children: item.children ? menuDataRender(item.children) : undefined,
85+
};
86+
return Authorized.check(item.authority, localItem, null) as MenuDataItem;
87+
});
88+
},
89+
[categories],
90+
);
91+
7992
const menuDataRef = useRef<MenuDataItem[]>([]);
8093

8194
useEffect(() => {
@@ -166,7 +179,8 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
166179
);
167180
};
168181

169-
export default connect(({ global, settings }: ConnectState) => ({
182+
export default connect(({ global, settings, user }: ConnectState) => ({
170183
collapsed: global.collapsed,
171184
settings,
185+
categories: user.currentUser?.currentCategories,
172186
}))(BasicLayout);

src/AgileConfig.Server.UI/react-ui-antd/src/models/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type CurrentUser = {
1717
}[];
1818
userid?: string;
1919
unreadCount?: number;
20+
currentCategories?: string[];
2021
};
2122

2223
export type UserModelState = {

src/AgileConfig.Server.UI/react-ui-antd/src/utils/authority.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function setFunctions(authority: string | string[] ): void {
6060

6161
// categories (business domains) control which left-menu entries are visible
6262
export function getCategories(str?: string): string[] {
63+
console.log('getCategories called');
6364
const catString =
6465
typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-categories') : str;
6566
let cats;
@@ -77,6 +78,7 @@ export function getCategories(str?: string): string[] {
7778
}
7879

7980
export function setCategories(categories: string | string[]): void {
81+
console.log('setCategories called with', categories);
8082
const arr = typeof categories === 'string' ? [categories] : categories;
8183
const safeArr = arr || [];
8284
localStorage.setItem('antd-pro-categories', JSON.stringify(safeArr));

0 commit comments

Comments
 (0)