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

frontend: Refactor prepareSidebarItems into a hook #2717

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions frontend/src/components/Sidebar/NavigationTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getCluster, getClusterPrefixedPath } from '../../lib/util';
import { useTypedSelector } from '../../redux/reducers/reducers';
import Tabs from '../common/Tabs';
import { SidebarItemProps } from '../Sidebar';
import prepareRoutes from './prepareRoutes';
import { useSidebarItems } from './useSidebarItems';

function searchNameInSubList(sublist: SidebarItemProps['subList'], name: string): boolean {
if (!sublist) {
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function NavigationTabs() {
}

let defaultIndex = null;
const listItems = prepareRoutes(t, sidebar.selected.sidebar || '');
const listItems = useSidebarItems(sidebar.selected.sidebar ?? undefined);
let navigationItem = listItems.find(item => item.name === sidebar.selected.item);
if (!navigationItem) {
const parent = findParentOfSubList(listItems, sidebar.selected.item);
Expand Down
23 changes: 3 additions & 20 deletions frontend/src/components/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import helpers from '../../helpers';
import { useCluster } from '../../lib/k8s';
import { createRouteURL } from '../../lib/router';
import { useTypedSelector } from '../../redux/reducers/reducers';
import { ActionButton } from '../common';
import CreateButton from '../common/Resource/CreateButton';
import NavigationTabs from './NavigationTabs';
import prepareRoutes from './prepareRoutes';
import SidebarItem from './SidebarItem';
import {
DefaultSidebars,
setSidebarSelected,
setWhetherSidebarOpen,
SidebarEntry,
} from './sidebarSlice';
import { useSidebarItems } from './useSidebarItems';
import VersionButton from './VersionButton';

export const drawerWidth = 240;
Expand Down Expand Up @@ -154,8 +153,6 @@
});

export default function Sidebar() {
const { t, i18n } = useTranslation(['glossary', 'translation']);

const sidebar = useTypedSelector(state => state.sidebar);
const {
isOpen,
Expand All @@ -165,24 +162,10 @@
isTemporary: isTemporaryDrawer,
} = useSidebarInfo();
const isNarrowOnly = isNarrow && !canExpand;
const arePluginsLoaded = useTypedSelector(state => state.plugins.loaded);
const namespaces = useTypedSelector(state => state.filter.namespaces);
const dispatch = useDispatch();
const cluster = useCluster();
const items = React.useMemo(() => {
// If the sidebar is null, then it means it should not be visible.
if (sidebar.selected.sidebar === null) {
return [];
}
return prepareRoutes(t, sidebar.selected.sidebar || '');
}, [
cluster,
sidebar.selected,
sidebar.entries,
sidebar.filters,
i18n.language,
arePluginsLoaded,
]);

const items = useSidebarItems(sidebar?.selected?.sidebar ?? undefined);

const search = namespaces.size !== 0 ? `?namespace=${[...namespaces].join('+')}` : '';

Expand All @@ -200,7 +183,7 @@
}

return (
<PureSidebar

Check failure on line 186 in frontend/src/components/Sidebar/Sidebar.tsx

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-22.04)

src/storybook.test.tsx > Storybook Tests > Sidebar/Sidebar > SelectedItemWithSidebarOmitted

TypeError: Cannot read properties of undefined (reading 'map') ❯ src/components/Sidebar/Sidebar.tsx:186:5 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom.development.js:19617:20 ❯ updateSimpleMemoComponent node_modules/react-dom/cjs/react-dom.development.js:19454:10 ❯ updateMemoComponent node_modules/react-dom/cjs/react-dom.development.js:19303:14 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21712:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22 ❯ flushActQueue node_modules/react/cjs/react.development.js:2667:24 ❯ recursivelyFlushAsyncActWork node_modules/react/cjs/react.development.js:2633:9 ❯ node_modules/react/cjs/react.development.js:2545:15
items={items}
open={isOpen}
openUserSelected={isUserOpened}
Expand Down
320 changes: 0 additions & 320 deletions frontend/src/components/Sidebar/prepareRoutes.ts

This file was deleted.

Loading
Loading