-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathRoot.tsx
420 lines (394 loc) · 13.2 KB
/
Root.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import React, { PropsWithChildren, useContext, useState } from 'react';
import {
Sidebar,
SidebarDivider,
SidebarGroup,
SidebarItem,
SidebarPage,
SidebarScrollWrapper,
SidebarSpace,
} from '@backstage/core-components';
import { configApiRef, useApi } from '@backstage/core-plugin-api';
import { MyGroupsSidebarItem } from '@backstage/plugin-org';
import { usePermission } from '@backstage/plugin-permission-react';
import { SidebarSearchModal } from '@backstage/plugin-search';
import { Settings as SidebarSettings } from '@backstage/plugin-user-settings';
import { policyEntityReadPermission } from '@backstage-community/plugin-rbac-common';
import { AdminIcon } from '@internal/plugin-dynamic-plugins-info';
import AccountCircleOutlinedIcon from '@mui/icons-material/AccountCircleOutlined';
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import ExpandMore from '@mui/icons-material/ExpandMore';
import MuiMenuIcon from '@mui/icons-material/Menu';
import SearchIcon from '@mui/icons-material/Search';
import Box from '@mui/material/Box';
import Collapse from '@mui/material/Collapse';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import { SxProps } from '@mui/material/styles';
import { makeStyles } from 'tss-react/mui';
import DynamicRootContext, {
ResolvedMenuItem,
} from '../DynamicRoot/DynamicRootContext';
import { ApplicationHeaders } from './ApplicationHeaders';
import { MenuIcon } from './MenuIcon';
import { SidebarLogo } from './SidebarLogo';
const useStyles = makeStyles()({
/**
* This is a workaround to remove the fix height of the Page component
* to support the application headers (and the global header plugin)
* without having multiple scrollbars.
*
* This solves also the duplicate scrollbar issues in tech docs:
* https://issues.redhat.com/browse/RHIDP-4637 (Scrollbar for docs behaves weirdly if there are over a page of headings)
*
* Which was also reported and tried to fix upstream:
* https://github.com/backstage/backstage/issues/13717
* https://github.com/backstage/backstage/pull/14138
* https://github.com/backstage/backstage/issues/19427
* https://github.com/backstage/backstage/issues/22745
*
* See also
* https://github.com/backstage/backstage/blob/v1.35.0/packages/core-components/src/layout/Page/Page.tsx#L31-L34
*
* The following rules are based on the current DOM structure
*
* ```
* <body>
* <div id="root">
* // snackbars and toasts
* <div className="pageWithoutFixHeight">
* <nav /> // Optional nav(s) if a header with position: above-sidebar is configured
* <div> // Backstage SidebarPage component
* <nav /> // Optional nav(s) if a header with position: above-main-content is configured
* <nav aria-label="sidebar nav" /> // Sidebar content
* <main /> // Backstage Page component
* </div>
* </div>
* </div>
* // some modals and other overlays
* </body>
* ```
*/
pageWithoutFixHeight: {
// Use 100vh for the complete viewport (similar to how Backstage does it)
// and makes the page content part scrollable below...
// But instead of using 100vh on the content below,
// we use it here so that it includes the header.
'> div': {
height: '100vh',
display: 'flex',
flexDirection: 'column',
},
// But we unset the Backstage default 100vh value here and use flex box
// to grow to the full height of the parent container.
'> div > main': {
height: 'unset',
flexGrow: 1,
},
// This solves the same issue for techdocs, which was reported as
// https://issues.redhat.com/browse/RHIDP-4637
'.techdocs-reader-page > main': {
height: 'unset',
},
},
sidebarItem: {
textDecorationLine: 'none',
},
});
// Backstage does not expose the props object, pulling it from the component argument
type SidebarItemProps = Parameters<typeof SidebarItem>[0];
const SideBarItemWrapper = (props: SidebarItemProps) => {
const {
classes: { sidebarItem },
} = useStyles();
return (
<SidebarItem
{...props}
className={`${sidebarItem} ${props.className ?? ''}`}
/>
);
};
const renderIcon = (iconName: string) => () => <MenuIcon icon={iconName} />;
const renderExpandIcon = (expand: boolean) => {
return expand ? (
<ExpandMore
fontSize="small"
style={{
display: 'flex',
marginLeft: 8,
}}
/>
) : (
<ChevronRightIcon
fontSize="small"
style={{
display: 'flex',
marginLeft: 8,
}}
/>
);
};
const getMenuItem = (menuItem: ResolvedMenuItem, isNestedMenuItem = false) => {
const menuItemStyle = {
paddingLeft: isNestedMenuItem ? '2rem' : '',
};
return menuItem.name === 'default.my-group' ? (
<Box key={menuItem.name} sx={{ '& a': menuItemStyle }}>
<MyGroupsSidebarItem
key={menuItem.name}
icon={renderIcon(menuItem.icon ?? '')}
singularTitle={menuItem.title}
pluralTitle={`${menuItem.title}s`}
/>
</Box>
) : (
<SideBarItemWrapper
key={menuItem.name}
icon={renderIcon(menuItem.icon ?? '')}
to={menuItem.to ?? ''}
text={menuItem.title}
style={menuItemStyle}
/>
);
};
interface ExpandableMenuListProps {
menuItems: ResolvedMenuItem[];
isOpen: boolean;
renderItem: (item: ResolvedMenuItem) => JSX.Element;
sx?: SxProps;
}
const ExpandableMenuList: React.FC<ExpandableMenuListProps> = ({
menuItems,
isOpen,
renderItem,
sx = {},
}) => {
if (!menuItems || menuItems.length === 0) return null;
return (
<Collapse in={isOpen} timeout="auto" unmountOnExit>
<List disablePadding sx={sx}>
{menuItems.map(item => renderItem(item))}
</List>
</Collapse>
);
};
export const Root = ({ children }: PropsWithChildren<{}>) => {
const {
classes: { pageWithoutFixHeight },
} = useStyles();
const { dynamicRoutes, menuItems } = useContext(DynamicRootContext);
const configApi = useApi(configApiRef);
const showLogo = configApi.getOptionalBoolean('app.sidebar.logo') ?? true;
const showSearch = configApi.getOptionalBoolean('app.sidebar.search') ?? true;
const showSettings =
configApi.getOptionalBoolean('app.sidebar.settings') ?? true;
const showAdministration =
configApi.getOptionalBoolean('app.sidebar.administration') ?? true;
const [openItems, setOpenItems] = useState<{ [key: string]: boolean }>({});
const { loading: loadingPermission, allowed: canDisplayRBACMenuItem } =
usePermission({
permission: policyEntityReadPermission,
resourceRef: policyEntityReadPermission.resourceType,
});
const handleClick = (itemName: string) => {
setOpenItems(prevOpenItems => ({
...prevOpenItems,
[itemName]: !prevOpenItems[itemName],
}));
};
const renderExpandableNestedMenuItems = (
menuItem: ResolvedMenuItem,
isSubMenuOpen: boolean,
) => {
return (
<ExpandableMenuList
menuItems={menuItem.children ?? []}
isOpen={isSubMenuOpen}
sx={{
paddingLeft: '4.25rem',
fontSize: 12,
'& span.MuiTypography-subtitle2': {
fontSize: 12,
whiteSpace: 'normal',
wordBreak: 'break-word',
overflowWrap: 'break-word',
},
'& div': { width: 36, boxShadow: '-1px 0 0 0 #3c3f42' },
"& div[class*='BackstageSidebarItem-secondaryAction']": { width: 20 },
a: {
width: 'auto',
'@media (min-width: 600px)': { width: 160 },
},
}}
renderItem={child => (
<SideBarItemWrapper
key={child.title}
icon={() => null}
text={child.title}
to={child.to ?? ''}
/>
)}
/>
);
};
const renderExpandableMenuItems = (
menuItem: ResolvedMenuItem,
isOpen: boolean,
) => {
return (
<ExpandableMenuList
menuItems={menuItem.children ?? []}
isOpen={isOpen}
renderItem={child => {
const isNestedMenuOpen = openItems[child.name] || false;
return (
<ListItem
key={child.name}
disableGutters
disablePadding
sx={{
display: 'block',
'& .MuiButton-label': { paddingLeft: '2rem' },
"& span[class*='-subtitle2']": {
width: 78,
fontSize: 12,
whiteSpace: 'normal',
wordBreak: 'break-word',
overflowWrap: 'break-word',
},
"& div[class*='BackstageSidebarItem-secondaryAction']": {
width:
child.children && child.children.length === 0 ? 18 : 48,
},
a: {
width: 'auto',
'@media (min-width: 600px)': { width: 224 },
},
}}
>
{child.children && child.children.length === 0 ? (
getMenuItem(child, true)
) : (
<>
<SideBarItemWrapper
icon={renderIcon(child.icon ?? '')}
text={child.title}
onClick={() => handleClick(child.name)}
>
{child.children!.length > 0 &&
renderExpandIcon(isNestedMenuOpen)}
</SideBarItemWrapper>
{renderExpandableNestedMenuItems(child, isNestedMenuOpen)}
</>
)}
</ListItem>
);
}}
/>
);
};
const renderMenuItems = (
isDefaultMenuSection: boolean,
isBottomMenuSection: boolean,
) => {
let menuItemArray = isDefaultMenuSection
? menuItems.filter(mi => mi.name.startsWith('default.'))
: menuItems.filter(mi => !mi.name.startsWith('default.'));
menuItemArray = isBottomMenuSection
? menuItemArray.filter(mi => mi.name === 'admin')
: menuItemArray.filter(mi => mi.name !== 'admin');
if (isBottomMenuSection && !canDisplayRBACMenuItem && !loadingPermission) {
menuItemArray[0].children = menuItemArray[0].children?.filter(
mi => mi.name !== 'rbac',
);
}
return (
<>
{menuItemArray.map(menuItem => {
const isOpen = openItems[menuItem.name] || false;
return (
<React.Fragment key={menuItem.name}>
{menuItem.children!.length === 0 && getMenuItem(menuItem)}
{menuItem.children!.length > 0 && (
<SideBarItemWrapper
key={menuItem.name}
icon={renderIcon(menuItem.icon ?? '')}
text={menuItem.title}
onClick={() => handleClick(menuItem.name)}
>
{menuItem.children!.length > 0 && renderExpandIcon(isOpen)}
</SideBarItemWrapper>
)}
{menuItem.children!.length > 0 &&
renderExpandableMenuItems(menuItem, isOpen)}
</React.Fragment>
);
})}
</>
);
};
return (
<div className={pageWithoutFixHeight}>
<ApplicationHeaders position="above-sidebar" />
<SidebarPage>
<ApplicationHeaders position="above-main-content" />
<Sidebar>
{showLogo && <SidebarLogo />}
{showSearch ? (
<>
<SidebarGroup label="Search" icon={<SearchIcon />} to="/search">
<SidebarSearchModal />
</SidebarGroup>
<SidebarDivider />
</>
) : (
<Box sx={{ height: '1.2rem' }} />
)}
<SidebarGroup label="Menu" icon={<MuiMenuIcon />}>
{/* Global nav, not org-specific */}
{renderMenuItems(true, false)}
{/* End global nav */}
<SidebarDivider />
<SidebarScrollWrapper>
{renderMenuItems(false, false)}
{dynamicRoutes.map(({ scope, menuItem, path }) => {
if (menuItem && 'Component' in menuItem) {
return (
<menuItem.Component
{...(menuItem.config?.props || {})}
key={`${scope}/${path}`}
to={path}
/>
);
}
return null;
})}
</SidebarScrollWrapper>
</SidebarGroup>
<SidebarSpace />
{showAdministration && (
<>
<SidebarDivider />
<SidebarGroup label="Administration" icon={<AdminIcon />}>
{renderMenuItems(false, true)}
</SidebarGroup>
</>
)}
{showSettings && (
<>
<SidebarDivider />
<SidebarGroup
label="Settings"
to="/settings"
icon={<AccountCircleOutlinedIcon />}
>
<SidebarSettings icon={AccountCircleOutlinedIcon} />
</SidebarGroup>
</>
)}
</Sidebar>
{children}
</SidebarPage>
</div>
);
};