Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat update nav panel #297

Open
wants to merge 2 commits into
base: feature/model-registry
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const ROUTES: RouteConfig[] = [
path: routerPaths.registerModel,
label: 'Register Model',
Component: RegisterModelForm,
nav: true,
nav: false,
},
{
path: routerPaths.modelList,
Expand Down
29 changes: 29 additions & 0 deletions public/components/__tests__/nav_panel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';

import { NavPanel } from '../nav_panel';
import { render, screen } from '../../../test/test_utils';

describe('<NavPanel />', () => {
it('should render "Overview" with selected status', () => {
render(<NavPanel />, { route: '/overview' });

expect(screen.getByText('Overview')).toBeInTheDocument();
expect(screen.getByText('Overview').closest('a[class*=isSelected]')).toBeInTheDocument();
});
it('should render "Model Registry" with selected status', () => {
render(<NavPanel />, { route: '/model-registry/model-list' });

expect(screen.getByText('Model Registry')).toBeInTheDocument();
expect(screen.getByText('Model Registry').closest('a[class*=isSelected]')).toBeInTheDocument();
});
it('should render nothing if nav item not exists', () => {
const { container } = render(<NavPanel />, { route: '/model-registry/model/123' });

expect(container).toBeEmptyDOMElement();
});
});
4 changes: 1 addition & 3 deletions public/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export const MlCommonsPluginApp = ({
<I18nProvider>
<>
<EuiPage>
<EuiPageSideBar>
<NavPanel />
</EuiPageSideBar>
<NavPanel />
<EuiPageBody component="main">
<Switch>
{ROUTES.map(({ path, Component, exact }) => (
Expand Down
48 changes: 33 additions & 15 deletions public/components/nav_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,47 @@
*/

import React, { useCallback, useMemo } from 'react';
import { EuiSideNav } from '@elastic/eui';
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(
() =>
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,
};
}),
() => [
{
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(
({ href, ...restProps }) => <Link to={href!} {...restProps} />,
[]
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>
);
return <EuiSideNav items={items} renderItem={renderItem} />;
}
Loading