Skip to content

Commit

Permalink
code improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Yi Cai <[email protected]>
  • Loading branch information
ciiay committed Jan 14, 2025
1 parent 057118c commit ad1eab1
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packages/app/src/components/Root/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { PropsWithChildren, useContext, useState } from 'react';

import {
ErrorBoundary,
Sidebar,
SidebarDivider,
SidebarGroup,
Expand Down Expand Up @@ -29,8 +30,8 @@ import { makeStyles } from 'tss-react/mui';
import { policyEntityReadPermission } from '@janus-idp/backstage-plugin-rbac-common';

import DynamicRootContext, {
MountPoints,
ResolvedMenuItem,
ScalprumMountPoint,
} from '../DynamicRoot/DynamicRootContext';
import { MenuIcon } from './MenuIcon';
import { SidebarLogo } from './SidebarLogo';
Expand Down Expand Up @@ -102,24 +103,31 @@ const getMenuItem = (menuItem: ResolvedMenuItem, isNestedMenuItem = false) => {
);
};

const ApplicationHeader = ({ mountPoints }: { mountPoints: MountPoints }) =>
mountPoints['application/header']?.map(({ Component, config }) => (
<Component
key={`app-header-${config?.layout?.position}`}
{...config?.props}
/>
const ApplicationHeader = ({
headerMountPoints,
}: {
headerMountPoints: ScalprumMountPoint[];
}) =>
headerMountPoints?.map(({ Component, config }) => (
<ErrorBoundary
key={`app-header-error-boundary-${config?.layout?.position}`}
>
<Component
key={`app-header-${config?.layout?.position}`}
{...config?.props}
/>
</ErrorBoundary>
));

export const Root = ({ children }: PropsWithChildren<{}>) => {
const { dynamicRoutes, menuItems, mountPoints } =
useContext(DynamicRootContext);
const [openItems, setOpenItems] = useState<{ [key: string]: boolean }>({});
const headerMountPoints = mountPoints['application/header'] ?? [];

const headerPositions = mountPoints['application/header']?.map(
({ config }) => {
return config?.layout?.position;
},
);
const headerPositions = headerMountPoints.map(({ config }) => {
return config?.layout?.position;
});

const { loading: loadingPermission, allowed: canDisplayRBACMenuItem } =
usePermission({
Expand Down Expand Up @@ -266,11 +274,11 @@ export const Root = ({ children }: PropsWithChildren<{}>) => {
return (
<>
{headerPositions.includes('above-sidebar') && (
<ApplicationHeader mountPoints={mountPoints} />
<ApplicationHeader headerMountPoints={headerMountPoints} />
)}
<SidebarPage>
{headerPositions.includes('above-main-content') && (
<ApplicationHeader mountPoints={mountPoints} />
<ApplicationHeader headerMountPoints={headerMountPoints} />
)}
<Sidebar>
<SidebarLogo />
Expand Down

0 comments on commit ad1eab1

Please sign in to comment.