Skip to content

Commit

Permalink
hide header items in sidebar
Browse files Browse the repository at this point in the history
Signed-off-by: Yi Cai <[email protected]>
  • Loading branch information
ciiay committed Jan 29, 2025
1 parent 45d64a2 commit f54e4ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/app/src/components/Root/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import { makeStyles } from 'tss-react/mui';

import { policyEntityReadPermission } from '@janus-idp/backstage-plugin-rbac-common';

import DynamicRootContext, {
ResolvedMenuItem,
} from '../DynamicRoot/DynamicRootContext';
import { useDisplayedSidebarItems } from '../../utils/dynamicUI/useDisplayedSidebarItems';
import { ResolvedMenuItem } from '../DynamicRoot/DynamicRootContext';
import { ApplicationHeaders } from './ApplicationHeaders';
import { MenuIcon } from './MenuIcon';
import { SidebarLogo } from './SidebarLogo';
Expand Down Expand Up @@ -103,7 +102,11 @@ const getMenuItem = (menuItem: ResolvedMenuItem, isNestedMenuItem = false) => {
};

export const Root = ({ children }: PropsWithChildren<{}>) => {
const { dynamicRoutes, menuItems } = useContext(DynamicRootContext);
const {
showSearchBar,
displayedDynamicRoutes: dynamicRoutes,
displayedMenuItems: menuItems,
} = useDisplayedSidebarItems();
const [openItems, setOpenItems] = useState<{ [key: string]: boolean }>({});

const { loading: loadingPermission, allowed: canDisplayRBACMenuItem } =
Expand Down Expand Up @@ -255,9 +258,11 @@ export const Root = ({ children }: PropsWithChildren<{}>) => {
<ApplicationHeaders position="above-main-content" />
<Sidebar>
<SidebarLogo />
<SidebarGroup label="Search" icon={<SearchIcon />} to="/search">
<SidebarSearchModal />
</SidebarGroup>
{showSearchBar && (
<SidebarGroup label="Search" icon={<SearchIcon />} to="/search">
<SidebarSearchModal />
</SidebarGroup>
)}
<SidebarDivider />
<SidebarGroup label="Menu" icon={<MuiMenuIcon />}>
{/* Global nav, not org-specific */}
Expand Down
29 changes: 29 additions & 0 deletions packages/app/src/utils/dynamicUI/useDisplayedSidebarItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useContext } from 'react';

import DynamicRootContext from '../../components/DynamicRoot/DynamicRootContext';

export const useDisplayedSidebarItems = () => {
const { dynamicRoutes, menuItems, mountPoints } =
useContext(DynamicRootContext);
const headerComponents = mountPoints['application/header/component'] ?? [];

const headerPaths = headerComponents
.map(({ config }) => config?.props?.to)
.filter(Boolean);

const displayedDynamicRoutes = dynamicRoutes?.filter(
({ path }) => !headerPaths.includes(path),
);

const displayedMenuItems = menuItems.filter(
item => !headerPaths.includes(item.to),
);

return {
showSearchBar: !headerComponents.some(
({ config }) => (config as any)?.type === 'search',
),
displayedDynamicRoutes,
displayedMenuItems,
};
};

0 comments on commit f54e4ea

Please sign in to comment.