-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.tsx
321 lines (310 loc) · 9.03 KB
/
index.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright 2022 Nightingale Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import FcMenu, { IMenuProps } from '@fc-components/menu';
import React, { FC, useState, useEffect } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { Menu, Button } from 'antd';
import Icon, { AimOutlined, AlertOutlined, LineChartOutlined, CodeOutlined } from '@ant-design/icons';
import _ from 'lodash';
import { useTranslation } from 'react-i18next';
import querystring from 'query-string';
import { dynamicPackages, Entry } from '@/utils';
import { getMenuPerm } from '@/services/common';
import { RootState as AccountRootState, accountStoreState } from '@/store/accountInterface';
import IconFont from '../IconFont';
import './menu.less';
const { SubMenu } = Menu;
const Packages = dynamicPackages();
let lazyMenu = Packages.reduce((result: any, module: Entry) => {
const menu = module.menu;
return menu ? (result = result.concat(menu)) : result;
}, []);
const getDefaultOpenKey = (menus: any, pathname) => {
const currentSubMenu = _.find(menus, (subMenus: any) => {
return _.some(
subMenus.children.filter((i) => !!i),
(menu) => {
return pathname.indexOf(menu.key) !== -1;
},
);
});
if (currentSubMenu) {
return currentSubMenu.key;
}
};
const defaultSelectedKey = (menus: any, pathname) => {
let key;
_.forEach(menus, (subMenus: any) => {
_.forEach(subMenus.children, (menu: any) => {
if (menu && pathname.indexOf(menu.key) !== -1) {
key = menu.key;
}
});
});
return key;
};
const SideMenu: FC = () => {
const { t, i18n } = useTranslation();
const [defaultSelectedKeys, setDefaultSelectedKeys] = useState<string[]>();
const menuList = [
{
key: 'targets',
icon: <AimOutlined />,
label: t('监控对象'),
children: [
{
key: '/targets',
label: t('对象列表'),
},
],
},
{
key: 'monitor',
icon: <LineChartOutlined />,
label: t('监控看图'),
children: [
{
key: '/metric/explorer',
label: t('即时查询'),
},
{
key: '/object/explorer',
label: t('快捷视图'),
},
{
key: '/dashboards',
label: t('监控大盘'),
},
],
},
{
key: 'alarm',
icon: <AlertOutlined />,
label: t('告警管理'),
children: [
{
key: '/alert-rules',
label: t('告警规则'),
},
{
key: '/recording-rules',
label: t('记录规则'),
},
{
key: '/alert-mutes',
label: t('屏蔽规则'),
},
{
key: '/alert-subscribes',
label: t('订阅规则'),
},
{
key: '/alert-cur-events',
label: t('活跃告警'),
},
{
key: '/alert-his-events',
label: t('历史告警'),
},
],
},
{
key: 'job',
icon: <CodeOutlined />,
label: t('告警自愈'),
children: [
{
key: '/job-tpls',
label: t('自愈脚本'),
},
{
key: '/job-tasks',
label: t('执行历史'),
},
],
},
{
key: 'manage',
// icon: <UserOutlined />,
icon: <IconFont type='icon-renyuanzuzhi-weixuanzhongyangshi' />,
label: t('人员组织'),
children: [
{
key: '/users',
label: t('用户管理'),
},
{
key: '/user-groups',
label: t('团队管理'),
},
{
key: '/busi-groups',
label: t('业务组管理'),
},
],
},
{
key: 'help',
// icon: <Icon component={SystemInfoSvg as any} />,
icon: <IconFont type='icon-xitongxinxi-weixuanzhongyangshi' />,
label: t('系统信息'),
children: [
{
key: '/help/version',
label: t('系统版本'),
},
// {
// key: '/help/contact',
// label:t('联系我们'),
// },
{
key: '/help/migrate',
label: t('管理员迁移'),
},
{
key: '/help/servers',
label: t('告警引擎'),
},
...[
import.meta.env.VITE_IS_DS_SETTING
? {
key: '/help/source',
label: t('数据源管理'),
}
: undefined,
],
],
},
];
const [menus, setMenus] = useState(menuList);
useEffect(() => {
setDefaultSelectedKeys([]);
for (const item of menuList) {
if (item && item.key.startsWith('/') && window.location.pathname.includes(item.key)) {
setDefaultSelectedKeys([item?.key]);
break;
} else if (item?.children && item.children.length > 0) {
for (const i of item.children) {
if (i && window.location.pathname.includes(i.key!)) {
setDefaultSelectedKeys([item?.key, i.key!]);
break;
}
}
}
}
}, []);
const history = useHistory();
const location = useLocation();
const { pathname } = location;
let { profile } = useSelector<AccountRootState, accountStoreState>((state) => state.account);
const [collapsed, setCollapsed] = useState<'0' | '1' | '2' | string | null>(localStorage.getItem('menuCollapsed'));
// full => '0'
// semi => '1'
// skim => '2'
const [selectedKeys, setSelectedKeys] = useState<string[]>([defaultSelectedKey(menus, pathname)]);
const [openKeys, setOpenKeys] = useState<string[]>(collapsed ? [] : [getDefaultOpenKey(menus, pathname)]);
const switchCollapsed = () => {
if (!isNaN(Number(collapsed))) {
const newColl = (Number(collapsed) === 2 ? -1 : Number(collapsed)) + 1 + '';
setCollapsed(newColl);
localStorage.setItem('menuCollapsed', newColl);
} else {
setCollapsed('1');
localStorage.setItem('menuCollapsed', '1');
}
setTimeout(() => {
window.dispatchEvent(new Event('resize'));
}, 500);
};
const handleClick = (key) => {
if (location.pathname === key) return;
setSelectedKeys([key as string]);
// 写两个key as string 感觉有点傻
if (key === 'changeLanguage') {
let language = i18n.language == 'en' ? 'zh' : 'en';
i18n.changeLanguage(language);
localStorage.setItem('language', language);
}
if ((key as string).startsWith('/')) {
history.push(key as string);
}
};
const hideSideMenu = () => {
if (
location.pathname === '/login' ||
location.pathname.startsWith('/chart/') ||
location.pathname.startsWith('/dashboards/share/') ||
location.pathname === '/callback' ||
location.pathname.indexOf('/polaris/screen') === 0
) {
return true;
}
// 大盘全屏模式下也需要隐藏左侧菜单
if (location.pathname.indexOf('/dashboard') === 0) {
const query = querystring.parse(location.search);
if (query?.viewMode === 'fullscreen') {
return true;
}
return false;
}
return false;
};
useEffect(() => {
setSelectedKeys([defaultSelectedKey(menus, pathname)]);
if (!collapsed) {
setOpenKeys(_.union([...openKeys, getDefaultOpenKey(menus, pathname)]));
}
}, [pathname, collapsed]);
useEffect(() => {
if (profile.roles.length > 0) {
if (profile.roles.indexOf('Admin') === -1) {
getMenuPerm().then((res) => {
const { dat } = res;
const newMenus = [...menuList];
newMenus.forEach((menu) => {
menu.children = menu.children.filter((item) => item && dat.includes(item.key));
});
setMenus(newMenus);
});
} else {
setMenus(menuList);
}
}
}, [profile.roles]);
return hideSideMenu() ? null : (
<div
style={{
display: 'flex',
flexDirection: 'column',
padding: '10px 0 10px 10px',
}}
>
{collapsed !== '2' && (
<div className={`home ${collapsed === '1' ? 'collapse' : ''}`}>
<div className='name' onClick={() => history.push('/metric/explorer')} key='overview'>
<img src={collapsed === '1' ? '/image/logo.svg' : '/image/logo-l.svg'} alt='' className='logo' />
</div>
</div>
)}
{defaultSelectedKeys && (
<FcMenu items={menus} onClick={handleClick} collapsed={collapsed} switchCollapsed={switchCollapsed} defaultSelectedKeys={defaultSelectedKeys} activeMode='click' />
)}
</div>
);
};
export default SideMenu;