-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
Header.tsx
370 lines (341 loc) · 13.1 KB
/
Header.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
import { useState, VFC } from 'react';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useTheme } from '@mui/material/styles';
import { Link } from 'react-router-dom';
import {
AppBar,
Container,
IconButton,
Tooltip,
styled,
Theme,
Box,
} from '@mui/material';
import MenuIcon from '@mui/icons-material/Menu';
import SettingsIcon from '@mui/icons-material/Settings';
import UserProfile from 'component/user/UserProfile';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import MenuBookIcon from '@mui/icons-material/MenuBook';
import { ReactComponent as UnleashLogo } from 'assets/img/logoDarkWithText.svg';
import { ReactComponent as UnleashLogoWhite } from 'assets/img/logoWithWhiteText.svg';
import { ReactComponent as CelebatoryUnleashLogo } from 'assets/img/unleashHoliday.svg';
import { ReactComponent as CelebatoryUnleashLogoWhite } from 'assets/img/unleashHolidayDark.svg';
import { DrawerMenu } from './DrawerMenu/DrawerMenu';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { flexRow, focusable } from 'themes/themeStyles';
import { NavigationMenu } from './NavigationMenu/NavigationMenu';
import { getRoutes, getCondensedRoutes } from 'component/menu/routes';
import {
DarkModeOutlined,
KeyboardArrowDown,
LightModeOutlined,
} from '@mui/icons-material';
import { filterByConfig, mapRouteLink } from 'component/common/util';
import { useId } from 'hooks/useId';
import { ThemeMode } from 'component/common/ThemeMode/ThemeMode';
import { useThemeMode } from 'hooks/useThemeMode';
import { Notifications } from 'component/common/Notifications/Notifications';
import { useAdminRoutes } from 'component/admin/useAdminRoutes';
import InviteLinkButton from './InviteLink/InviteLinkButton/InviteLinkButton';
import { useUiFlag } from 'hooks/useUiFlag';
import { Badge } from '../../common/Badge/Badge';
const StyledHeader = styled(AppBar)(({ theme }) => ({
backgroundColor: theme.palette.background.paper,
padding: theme.spacing(1),
boxShadow: 'none',
position: 'relative',
zIndex: 300,
}));
const StyledSpaciousHeader = styled(AppBar)(({ theme }) => ({
backgroundColor: theme.palette.background.paper,
padding: theme.spacing(1),
boxShadow: 'none',
position: 'relative',
zIndex: 300,
maxWidth: '1512px',
[theme.breakpoints.down('lg')]: {
maxWidth: '1280px',
paddingLeft: theme.spacing(1),
paddingRight: theme.spacing(1),
},
[theme.breakpoints.down(1024)]: {
marginLeft: 0,
marginRight: 0,
},
[theme.breakpoints.down('sm')]: {
minWidth: '100%',
},
margin: '0 auto',
}));
const SpaciousStyledContainer = styled(Box)(() => ({
display: 'flex',
alignItems: 'center',
width: '100%',
'&&&': { padding: 0 },
}));
const StyledContainer = styled(Container)(() => ({
display: 'flex',
alignItems: 'center',
maxWidth: 1280,
'&&&': { padding: 0 },
}));
const StyledUserContainer = styled('div')({
marginLeft: 'auto',
display: 'flex',
alignItems: 'center',
});
const StyledNav = styled('nav')({
display: 'flex',
alignItems: 'center',
flexGrow: 1,
});
const StyledUnleashLogoWhite = styled(UnleashLogoWhite)({ width: '150px' });
const StyledUnleashLogo = styled(UnleashLogo)({ width: '150px' });
const StyledCelebatoryLogo = styled(CelebatoryUnleashLogo)({ width: '150px' });
const StyledLinks = styled('div')(({ theme }) => ({
display: 'flex',
justifyContent: 'center',
marginLeft: theme.spacing(3),
'& a': {
textDecoration: 'none',
color: 'inherit',
marginRight: theme.spacing(3),
display: 'flex',
alignItems: 'center',
},
}));
const StyledAdvancedNavButton = styled('button')(({ theme }) => ({
...focusable(theme),
border: 'none',
background: 'transparent',
height: '100%',
display: 'flex',
fontSize: theme.fontSizes.bodySize,
fontFamily: theme.typography.fontFamily,
alignItems: 'center',
color: 'inherit',
cursor: 'pointer',
}));
const StyledSpan = styled('span')({
height: '16px',
paddingBottom: 16,
});
const StyledBadge = styled(Badge)({
height: '16px',
maxHeight: '16px',
padding: 0.25,
marginBottom: 0,
});
const styledIconProps = (theme: Theme) => ({
color: theme.palette.neutral.main,
});
const StyledLink = styled(Link)(({ theme }) => focusable(theme));
const StyledLinkWithBetaBagde = ({
title,
to,
}: { title: string; to: string }) => (
<StyledLink to={to} sx={{ margin: 0 }}>
<div>
<span>{title}</span>{' '}
<StyledSpan>
<StyledBadge color='success'>Beta</StyledBadge>
</StyledSpan>
</div>
</StyledLink>
);
const StyledIconButton = styled(IconButton)<{
component?: 'a' | 'button';
href?: string;
target?: string;
}>(({ theme }) => ({
borderRadius: 100,
'&:focus-visible': {
outlineStyle: 'solid',
outlineWidth: 2,
outlineColor: theme.palette.primary.main,
borderRadius: '100%',
},
}));
const Header: VFC = () => {
const { onSetThemeMode, themeMode } = useThemeMode();
const theme = useTheme();
const adminId = useId();
const configId = useId();
const [adminRef, setAdminRef] = useState<HTMLButtonElement | null>(null);
const [configRef, setConfigRef] = useState<HTMLButtonElement | null>(null);
const disableNotifications = useUiFlag('disableNotifications');
const { uiConfig, isOss } = useUiConfig();
const smallScreen = useMediaQuery(theme.breakpoints.down('md'));
const [openDrawer, setOpenDrawer] = useState(false);
const toggleDrawer = () => setOpenDrawer((prev) => !prev);
const onAdminClose = () => setAdminRef(null);
const onConfigureClose = () => setConfigRef(null);
const increaseUnleashWidth = useUiFlag('increaseUnleashWidth');
const celebatoryUnleash = useUiFlag('celebrateUnleash');
const insightsDashboard = useUiFlag('executiveDashboard');
const routes = getRoutes();
const adminRoutes = useAdminRoutes();
const filteredMainRoutes = {
mainNavRoutes: getCondensedRoutes(routes.mainNavRoutes)
.filter(filterByConfig(uiConfig))
.map(mapRouteLink),
mobileRoutes: getCondensedRoutes(routes.mobileRoutes)
.filter(filterByConfig(uiConfig))
.map(mapRouteLink),
adminRoutes,
};
const HeaderComponent = increaseUnleashWidth
? StyledSpaciousHeader
: StyledHeader;
const ContainerComponent = increaseUnleashWidth
? SpaciousStyledContainer
: StyledContainer;
if (smallScreen) {
return (
<HeaderComponent position='static'>
<ContainerComponent>
<Tooltip title='Menu' arrow>
<IconButton
sx={{
color: (theme) => theme.palette.text.primary,
}}
onClick={toggleDrawer}
aria-controls='header-drawer'
aria-expanded={openDrawer}
size='large'
>
<MenuIcon />
</IconButton>
</Tooltip>
<DrawerMenu
links={uiConfig.links}
open={openDrawer}
toggleDrawer={toggleDrawer}
routes={filteredMainRoutes}
/>
<StyledUserContainer>
<UserProfile />
</StyledUserContainer>
</ContainerComponent>
</HeaderComponent>
);
}
return (
<HeaderComponent position='static'>
<ContainerComponent>
<StyledLink to='/' sx={flexRow} aria-label='Home'>
<ThemeMode
darkmode={
<ConditionallyRender
condition={celebatoryUnleash}
show={<CelebatoryUnleashLogoWhite />}
elseShow={
<StyledUnleashLogoWhite aria-label='Unleash logo' />
}
/>
}
lightmode={
<ConditionallyRender
condition={celebatoryUnleash}
show={<StyledCelebatoryLogo />}
elseShow={
<StyledUnleashLogo aria-label='Unleash logo' />
}
/>
}
/>
</StyledLink>
<StyledNav>
<StyledLinks>
<StyledLink to='/projects'>Projects</StyledLink>
<StyledLink to={'/search'}>Search</StyledLink>
<StyledLink to='/playground'>Playground</StyledLink>
<ConditionallyRender
condition={insightsDashboard}
show={
<StyledLinkWithBetaBagde
to={'/insights'}
title={'Insights'}
/>
}
/>
<StyledAdvancedNavButton
onClick={(e) => setConfigRef(e.currentTarget)}
aria-controls={configRef ? configId : undefined}
aria-expanded={Boolean(configRef)}
>
Configure
<KeyboardArrowDown sx={styledIconProps} />
</StyledAdvancedNavButton>
<NavigationMenu
id={configId}
options={filteredMainRoutes.mainNavRoutes}
anchorEl={configRef}
handleClose={onConfigureClose}
style={{ top: 10 }}
/>
</StyledLinks>
<StyledUserContainer>
<InviteLinkButton />
<Tooltip
title={
themeMode === 'dark'
? 'Switch to light theme'
: 'Switch to dark theme'
}
arrow
>
<StyledIconButton
onClick={onSetThemeMode}
size='large'
>
<ConditionallyRender
condition={themeMode === 'dark'}
show={<DarkModeOutlined />}
elseShow={<LightModeOutlined />}
/>
</StyledIconButton>
</Tooltip>
<ConditionallyRender
condition={!isOss() && !disableNotifications}
show={<Notifications />}
/>
<Tooltip title='Documentation' arrow>
<StyledIconButton
component='a'
href='https://docs.getunleash.io/'
target='_blank'
rel='noopener noreferrer'
size='large'
>
<MenuBookIcon />
</StyledIconButton>
</Tooltip>
<Tooltip title='Settings' arrow>
<StyledIconButton
onClick={(e) => setAdminRef(e.currentTarget)}
aria-controls={adminRef ? adminId : undefined}
aria-expanded={Boolean(adminRef)}
size='large'
>
<SettingsIcon />
</StyledIconButton>
</Tooltip>
<NavigationMenu
id={adminId}
options={filteredMainRoutes.adminRoutes}
anchorEl={adminRef}
handleClose={onAdminClose}
style={{
top: 5,
left: -100,
}}
/>{' '}
<UserProfile />
</StyledUserContainer>
</StyledNav>
</ContainerComponent>
</HeaderComponent>
);
};
export default Header;