-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathnav_panel.tsx
50 lines (45 loc) · 1.32 KB
/
nav_panel.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useCallback, useMemo } from 'react';
import { EuiSideNav, EuiPageSideBar, htmlIdGenerator, OuiSideNav } from '@elastic/eui';
import { generatePath, Link, matchPath, useLocation } from 'react-router-dom';
import { ROUTES } from '../../common/router';
export function NavPanel() {
const location = useLocation();
const items = useMemo(
() => [
{
name: 'Machine Learning',
id: htmlIdGenerator('machine-learning')(),
items: ROUTES.filter((item) => !!item.label && item.nav).map((item) => {
const href = generatePath(item.path);
return {
id: href,
name: item.label,
href,
isSelected:
matchPath(location.pathname, { path: item.path, exact: item.exact }) !== null,
};
}),
},
],
[location.pathname]
);
const renderItem = useCallback((item) => {
if (!item.href) {
return item.children;
}
const { href, ...restProps } = item;
return <Link to={href!} {...restProps} />;
}, []);
if (items[0].items.every((item) => !item.isSelected)) {
return null;
}
return (
<EuiPageSideBar>
<EuiSideNav items={items} renderItem={renderItem} />
</EuiPageSideBar>
);
}