-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
BasicLayout.tsx
194 lines (178 loc) · 5.21 KB
/
BasicLayout.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
* You can view component api by:
* https://github.com/ant-design/ant-design-pro-layout
*/
import React, { useEffect, useMemo, useRef } from 'react';
import { Link, history, useIntl } from 'umi';
import { getMatchMenu } from '@umijs/route-utils';
import { Result, Button } from 'antd';
import { GithubOutlined } from '@ant-design/icons';
import type { MenuDataItem, BasicLayoutProps as ProLayoutProps } from '@ant-design/pro-layout';
import ProLayout, { DefaultFooter } from '@ant-design/pro-layout';
import type { Dispatch } from 'redux';
import { connect } from 'dva';
import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import type { ConnectState } from '@/models/connect';
import { isProductionEnv } from '@/utils/utils';
import { setAuthority } from '@/utils/authority';
import type { DefaultSettings } from '../../config/defaultSettings';
import SwitchTabsLayout from './SwitchTabsLayout';
import logo from '../assets/logo.svg';
import styles from './BasicLayout.less';
const noMatch = (
<Result
status={403}
title='403'
subTitle='Sorry, you are not authorized to access this page.'
extra={
<Button type='primary'>
<Link to='/user/login'>Go Login</Link>
</Button>
}
/>
);
export interface BasicLayoutProps extends Omit<ProLayoutProps, 'children'> {
breadcrumbNameMap: Record<string, MenuDataItem>;
route: ProLayoutProps['route'] & {
authority: string[];
};
settings: DefaultSettings;
dispatch: Dispatch;
children: React.ReactElement;
}
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
breadcrumbNameMap: Record<string, MenuDataItem>;
};
/**
* use Authorized check all menu item
*/
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
menuList.map((item) => {
const localItem = {
...item,
children: item.children ? menuDataRender(item.children) : [],
};
return Authorized.check(item.authority, localItem, null) as MenuDataItem;
});
const defaultFooterDom = (
<DefaultFooter
copyright='2021 theprimone 出品'
links={[
{
key: 'Ant Design Pro',
title: 'Ant Design Pro',
href: 'https://pro.ant.design',
blankTarget: true,
},
{
key: 'github',
title: <GithubOutlined />,
href: 'https://github.com/theprimone/ant-design-pro-plus',
blankTarget: true,
},
{
key: 'Ant Design',
title: 'Ant Design',
href: 'https://ant.design',
blankTarget: true,
},
]}
/>
);
const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
const {
dispatch,
children,
location = {
pathname: '/',
},
settings,
} = props;
const menuDataRef = useRef<MenuDataItem[]>([]);
useEffect(() => {
if (dispatch) {
dispatch({
type: 'user/fetchCurrent',
});
}
}, []);
const handleMenuCollapse = (payload: boolean): void => {
if (dispatch) {
dispatch({
type: 'global/changeLayoutCollapsed',
payload,
});
}
};
if (isProductionEnv) {
setAuthority('admin');
}
// get children authority
const authorized = useMemo(
() =>
getMatchMenu(location.pathname || '/', menuDataRef.current).pop() || {
authority: undefined,
},
[location.pathname],
);
const { formatMessage } = useIntl();
return (
<ProLayout
className={settings.switchTabs?.mode && styles.customByPageTabs}
logo={logo}
formatMessage={formatMessage}
{...props}
{...settings}
onCollapse={(collapsed) => {
handleMenuCollapse(collapsed);
}}
onMenuHeaderClick={() => history.push('/')}
menuItemRender={(menuItemProps, defaultDom) => {
if (menuItemProps.isUrl || menuItemProps.children || !menuItemProps.path) {
return defaultDom;
}
return <Link to={menuItemProps.path}>{defaultDom}</Link>;
}}
breadcrumbRender={(routers = []) => [
{
path: '/',
breadcrumbName: '首页',
},
...routers,
]}
itemRender={(route, params, routes, paths) => {
const first = routes.indexOf(route) === 0;
return first ? (
<Link to={paths.join('/')}>{route.breadcrumbName}</Link>
) : (
<span>{route.breadcrumbName}</span>
);
}}
menuDataRender={menuDataRender}
rightContentRender={() => <RightContent />}
>
<Authorized authority={authorized!.authority} noMatch={noMatch}>
<SwitchTabsLayout
mode={settings?.switchTabs?.mode}
persistent={settings?.switchTabs?.persistent}
fixed={settings?.switchTabs?.fixed}
routes={props.route.routes!}
footerRender={() => {
if (settings.footerRender || settings.footerRender === undefined) {
return defaultFooterDom;
}
return null;
}}
>
{children}
</SwitchTabsLayout>
</Authorized>
</ProLayout>
);
};
export default connect(({ global, settings }: ConnectState) => ({
collapsed: global.collapsed,
settings,
}))(BasicLayout);