Skip to content

Commit 0e8d233

Browse files
πŸ’₯ feat: @umijs/route-utils replace getAuthorityFromRouter (#7319)
* πŸ’₯ feat: @umijs/route-utils replace getAuthorityFromRouter * fix test * remove unuse code
1 parent 9046670 commit 0e8d233

File tree

5 files changed

+19
-127
lines changed

5 files changed

+19
-127
lines changed

Diff for: β€Žpackage.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757
"@ant-design/pro-descriptions": "^1.0.16",
5858
"@ant-design/pro-layout": "^6.4.16",
5959
"@ant-design/pro-table": "^2.7.2",
60+
"@umijs/route-utils": "^1.0.32",
6061
"antd": "^4.6.3",
6162
"classnames": "^2.2.6",
6263
"lodash": "^4.17.11",
6364
"moment": "^2.25.3",
6465
"omit.js": "^2.0.2",
65-
"path-to-regexp": "2.4.0",
6666
"qs": "^6.9.0",
6767
"react": "^16.8.6",
6868
"react-dom": "^16.8.6",

Diff for: β€Žsrc/layouts/BasicLayout.tsx

+17-9
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import ProLayout, {
99
Settings,
1010
DefaultFooter,
1111
} from '@ant-design/pro-layout';
12-
import React, { useEffect } from 'react';
12+
import React, { useEffect, useMemo, useRef } from 'react';
1313
import { Link, useIntl, connect, Dispatch, history } from 'umi';
1414
import { GithubOutlined } from '@ant-design/icons';
1515
import { Result, Button } from 'antd';
1616
import Authorized from '@/utils/Authorized';
1717
import RightContent from '@/components/GlobalHeader/RightContent';
1818
import { ConnectState } from '@/models/connect';
19-
import { getAuthorityFromRouter } from '@/utils/utils';
19+
import { getMatchMenu } from '@umijs/route-utils';
2020
import logo from '../assets/logo.svg';
2121

2222
const noMatch = (
@@ -94,9 +94,8 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
9494
pathname: '/',
9595
},
9696
} = props;
97-
/**
98-
* constructor
99-
*/
97+
98+
const menuDataRef = useRef<MenuDataItem[]>([]);
10099

101100
useEffect(() => {
102101
if (dispatch) {
@@ -116,11 +115,16 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
116115
payload,
117116
});
118117
}
119-
}; // get children authority
120-
121-
const authorized = getAuthorityFromRouter(props.route.routes, location.pathname || '/') || {
122-
authority: undefined,
123118
};
119+
// get children authority
120+
const authorized = useMemo(
121+
() =>
122+
getMatchMenu(location.pathname || '/', menuDataRef.current).pop() || {
123+
authority: undefined,
124+
},
125+
[location.pathname],
126+
);
127+
124128
const { formatMessage } = useIntl();
125129

126130
return (
@@ -153,6 +157,10 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
153157
footerRender={() => defaultFooterDom}
154158
menuDataRender={menuDataRender}
155159
rightContentRender={() => <RightContent />}
160+
postMenuData={(menuData) => {
161+
menuDataRef.current = menuData || [];
162+
return menuData || [];
163+
}}
156164
{...props}
157165
{...settings}
158166
>

Diff for: β€Žsrc/pages/Authorized.tsx

-36
This file was deleted.

Diff for: β€Žsrc/utils/utils.test.ts

+1-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isUrl, getRouteAuthority } from './utils';
1+
import { isUrl } from './utils';
22

33
describe('isUrl tests', (): void => {
44
it('should return false for invalid and corner case inputs', (): void => {
@@ -35,42 +35,3 @@ describe('isUrl tests', (): void => {
3535
expect(isUrl('https://www.example.com/test/123?foo=bar')).toBeTruthy();
3636
});
3737
});
38-
39-
describe('getRouteAuthority tests', () => {
40-
it('should return authority for each route', (): void => {
41-
const routes = [
42-
{ path: '/user', name: 'user', authority: ['user'], exact: true },
43-
{ path: '/admin', name: 'admin', authority: ['admin'], exact: true },
44-
];
45-
expect(getRouteAuthority('/user', routes)).toEqual(['user']);
46-
expect(getRouteAuthority('/admin', routes)).toEqual(['admin']);
47-
});
48-
49-
it('should return inherited authority for unconfigured route', (): void => {
50-
const routes = [
51-
{ path: '/nested', authority: ['admin', 'user'], exact: true },
52-
{ path: '/nested/user', name: 'user', exact: true },
53-
];
54-
expect(getRouteAuthority('/nested/user', routes)).toEqual(['admin', 'user']);
55-
});
56-
57-
it('should return authority for configured route', (): void => {
58-
const routes = [
59-
{ path: '/nested', authority: ['admin', 'user'], exact: true },
60-
{ path: '/nested/user', name: 'user', authority: ['user'], exact: true },
61-
{ path: '/nested/admin', name: 'admin', authority: ['admin'], exact: true },
62-
];
63-
expect(getRouteAuthority('/nested/user', routes)).toEqual(['user']);
64-
expect(getRouteAuthority('/nested/admin', routes)).toEqual(['admin']);
65-
});
66-
67-
it('should return authority for substring route', (): void => {
68-
const routes = [
69-
{ path: '/nested', authority: ['user', 'users'], exact: true },
70-
{ path: '/nested/users', name: 'users', authority: ['users'], exact: true },
71-
{ path: '/nested/user', name: 'user', authority: ['user'], exact: true },
72-
];
73-
expect(getRouteAuthority('/nested/user', routes)).toEqual(['user']);
74-
expect(getRouteAuthority('/nested/users', routes)).toEqual(['users']);
75-
});
76-
});

Diff for: β€Žsrc/utils/utils.ts

-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { parse } from 'querystring';
2-
import pathRegexp from 'path-to-regexp';
3-
import { Route } from '@/models/connect';
42

53
/* eslint no-useless-escape:0 import/prefer-default-export:0 */
64
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
@@ -24,42 +22,3 @@ export const isAntDesignProOrDev = (): boolean => {
2422
};
2523

2624
export const getPageQuery = () => parse(window.location.href.split('?')[1]);
27-
28-
/**
29-
* props.route.routes
30-
* @param router [{}]
31-
* @param pathname string
32-
*/
33-
export const getAuthorityFromRouter = <T extends Route>(
34-
router: T[] = [],
35-
pathname: string,
36-
): T | undefined => {
37-
const authority = router.find(
38-
({ routes, path = '/', target = '_self' }) =>
39-
(path && target !== '_blank' && pathRegexp(path).exec(pathname)) ||
40-
(routes && getAuthorityFromRouter(routes, pathname)),
41-
);
42-
if (authority) return authority;
43-
return undefined;
44-
};
45-
46-
export const getRouteAuthority = (path: string, routeData: Route[]) => {
47-
let authorities: string[] | string | undefined;
48-
routeData.forEach((route) => {
49-
// match prefix
50-
if (pathRegexp(`${route.path}/(.*)`).test(`${path}/`)) {
51-
if (route.authority) {
52-
authorities = route.authority;
53-
}
54-
// exact match
55-
if (route.path === path) {
56-
authorities = route.authority || authorities;
57-
}
58-
// get children authority recursively
59-
if (route.routes) {
60-
authorities = getRouteAuthority(path, route.routes) || authorities;
61-
}
62-
}
63-
});
64-
return authorities;
65-
};

0 commit comments

Comments
Β (0)