forked from linode/manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserMenuPopover.tsx
More file actions
327 lines (304 loc) · 9.55 KB
/
UserMenuPopover.tsx
File metadata and controls
327 lines (304 loc) · 9.55 KB
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
import { useAccount, useProfile } from '@linode/queries';
import {
BetaChip,
Box,
Divider,
NewFeatureChip,
Stack,
Typography,
} from '@linode/ui';
import { styled } from '@mui/material';
import Grid from '@mui/material/Grid';
import Popover from '@mui/material/Popover';
import { useTheme } from '@mui/material/styles';
import React from 'react';
import { Link } from 'src/components/Link';
import { switchAccountSessionContext } from 'src/context/switchAccountSessionContext';
import { SwitchAccountButton } from 'src/features/Account/SwitchAccountButton';
import { useIsParentTokenExpired } from 'src/features/Account/SwitchAccounts/useIsParentTokenExpired';
import { useSwitchToParentAccount } from 'src/features/Account/SwitchAccounts/useSwitchToParentAccount';
import { useDelegationRole } from 'src/features/IAM/hooks/useDelegationRole';
import {
useIsIAMDelegationEnabled,
useIsIAMEnabled,
} from 'src/features/IAM/hooks/useIsIAMEnabled';
import { useFlags } from 'src/hooks/useFlags';
import { useRestrictedGlobalGrantCheck } from 'src/hooks/useRestrictedGlobalGrantCheck';
import {
sendSwitchAccountEvent,
sendSwitchToParentAccountEvent,
} from 'src/utilities/analytics/customEventAnalytics';
import { getStorage } from 'src/utilities/storage';
import { getCompanyNameOrEmail } from './utils';
interface UserMenuPopoverProps {
anchorEl: HTMLElement | null;
isDrawerOpen: boolean;
onClose: () => void;
onDrawerOpen: (open: boolean) => void;
}
interface MenuLink {
display: string;
hide?: boolean;
isBeta?: boolean;
isNew?: boolean;
to: string;
}
export const UserMenuPopover = (props: UserMenuPopoverProps) => {
const { anchorEl, isDrawerOpen, onClose, onDrawerOpen } = props;
const sessionContext = React.useContext(switchAccountSessionContext);
const { limitsEvolution, iamLimitedAvailabilityBadges } = useFlags();
const {
isProxyOrDelegateUserType,
isParentUserType,
isDelegateUserType,
isProxyUserType,
profile,
} = useDelegationRole();
const theme = useTheme();
const { handleSwitchToParentAccount, isSubmitting } =
useSwitchToParentAccount({
isDelegateUserType,
isProxyUserType,
onClose,
onTokenExpired: () => {
sessionContext.updateState({
isOpen: true,
});
},
});
const { data: account } = useAccount();
const { isIAMEnabled } = useIsIAMEnabled();
const isChildAccountAccessRestricted = useRestrictedGlobalGrantCheck({
globalGrantType: 'child_account_access',
});
const { isIAMDelegationEnabled } = useIsIAMDelegationEnabled();
const canSwitchBetweenParentOrProxyAccount = isIAMDelegationEnabled
? isParentUserType || isProxyOrDelegateUserType
: (isParentUserType && !isChildAccountAccessRestricted) ||
isProxyOrDelegateUserType;
const open = Boolean(anchorEl);
const id = open ? 'user-menu-popover' : undefined;
const profileLinks: MenuLink[] = [
{
display: 'Display',
to: '/profile/display',
},
{ display: 'Login & Authentication', to: '/profile/auth' },
{ display: 'SSH Keys', to: '/profile/keys' },
{ display: 'LISH Console Settings', to: '/profile/lish' },
{
display: 'API Tokens',
to: '/profile/tokens',
},
{ display: 'OAuth Apps', to: '/profile/clients' },
{
display: 'Preferences',
to: '/profile/preferences',
},
{
display: 'Referrals',
to: '/profile/referrals',
},
{ display: 'Log Out', to: '/logout' },
];
// Used for fetching parent profile and account data by making a request with the parent's token.
const proxyHeaders = isProxyOrDelegateUserType
? {
Authorization: getStorage(`authentication/parent_token/token`),
}
: undefined;
const companyNameOrEmail = getCompanyNameOrEmail({
company: account?.company,
profile,
});
const { data: parentProfile } = useProfile({ headers: proxyHeaders });
const userName =
(isProxyOrDelegateUserType ? parentProfile : profile)?.username ?? '';
const { isParentTokenExpired } = useIsParentTokenExpired({
isProxyOrDelegateUserType,
});
const accountLinks: MenuLink[] = React.useMemo(
() => [
{
display: 'Billing',
to: '/billing',
},
{
display: isIAMEnabled ? 'Identity & Access' : 'Users & Grants',
to: isIAMEnabled ? '/iam' : '/users',
isNew: isIAMEnabled && iamLimitedAvailabilityBadges,
},
{
display: 'Quotas',
hide: !limitsEvolution?.enabled,
to: '/quotas',
},
{
display: 'Login History',
to: '/login-history',
},
{
display: 'Service Transfers',
to: '/service-transfers',
},
{
display: 'Maintenance',
to: '/maintenance',
},
{
display: 'Account Settings',
to: '/account-settings',
},
],
[isIAMEnabled, limitsEvolution, iamLimitedAvailabilityBadges]
);
const renderLink = (link: MenuLink) => {
if (link.hide) {
return null;
}
return (
<Grid key={link.display} size={12}>
<Link
data-testid={`menu-item-${link.display}`}
onClick={onClose}
style={{
color: theme.tokens.alias.Content.Text.Link.Default,
font: theme.tokens.alias.Typography.Body.Semibold,
}}
to={link.to}
>
{link.display}
</Link>
</Grid>
);
};
const handleAccountSwitch = () => {
if (isParentTokenExpired) {
return sessionContext.updateState({
isOpen: true,
});
}
if (isDelegateUserType) {
sendSwitchToParentAccountEvent();
return handleSwitchToParentAccount();
}
onDrawerOpen(true);
};
return (
<Popover
anchorEl={anchorEl}
anchorOrigin={{
horizontal: 'right',
vertical: 'bottom',
}}
data-testid={id}
id={id}
marginThreshold={0}
onClose={onClose}
open={open}
slotProps={{
paper: {
sx: (theme) => ({
backgroundColor: theme.tokens.alias.Background.Normal,
paddingX: theme.tokens.spacing.S24,
paddingY: theme.tokens.spacing.S16,
maxWidth: 304,
}),
},
}}
// When the Switch Account drawer is open, hide the user menu popover so it's not covering the drawer.
sx={{ zIndex: isDrawerOpen ? 0 : 1 }}
>
<Stack
data-qa-user-menu
gap={(theme) => theme.tokens.spacing.S16}
minWidth={250}
>
<Stack
display="flex"
gap={(theme) => (companyNameOrEmail ? theme.tokens.spacing.S8 : 0)}
>
{canSwitchBetweenParentOrProxyAccount && companyNameOrEmail && (
<Typography
sx={(theme) => ({
color: theme.tokens.alias.Content.Text.Primary.Default,
font: theme.tokens.alias.Typography.Label.Semibold.S,
})}
>
Current account:
</Typography>
)}
<Typography
sx={(theme) => ({
color: theme.tokens.alias.Content.Text.Primary.Default,
font: theme.tokens.alias.Typography.Label.Bold.L,
overflowWrap: 'break-word',
})}
>
{canSwitchBetweenParentOrProxyAccount
? companyNameOrEmail || null
: userName}
</Typography>
{canSwitchBetweenParentOrProxyAccount && (
<SwitchAccountButton
buttonType="outlined"
data-testid="switch-account-button"
disabled={isSubmitting}
onClick={() => {
if (!isDelegateUserType) {
sendSwitchAccountEvent('User Menu');
}
handleAccountSwitch();
}}
/>
)}
</Stack>
<Box>
<Heading>My Profile</Heading>
<Divider />
<Grid columnSpacing={2} container rowSpacing={1}>
<Grid container direction="column" size={6} wrap="nowrap">
{profileLinks.slice(0, 4).map(renderLink)}
</Grid>
<Grid container direction="column" size={6} wrap="nowrap">
{profileLinks.slice(4).map(renderLink)}
</Grid>
</Grid>
</Box>
<Box>
<Heading>Administration</Heading>
<Divider />
<Stack
gap={(theme) => theme.tokens.spacing.S8}
mt={(theme) => theme.tokens.spacing.S8}
>
{accountLinks.map((menuLink) =>
menuLink.hide ? null : (
<Link
data-testid={`menu-item-${menuLink.display}`}
key={menuLink.display}
onClick={onClose}
style={{
color: theme.tokens.alias.Content.Text.Link.Default,
font: theme.tokens.alias.Typography.Body.Semibold,
}}
to={menuLink.to}
>
{menuLink.display}
{menuLink?.isBeta ? <BetaChip component="span" /> : null}
{menuLink?.isNew ? <NewFeatureChip component="span" /> : null}
</Link>
)
)}
</Stack>
</Box>
</Stack>
</Popover>
);
};
const Heading = styled(Typography)(({ theme }) => ({
color: theme.tokens.alias.Content.Text.Primary.Default,
font: theme.tokens.alias.Typography.Heading.Overline,
letterSpacing: theme.tokens.alias.Typography.Heading.OverlineLetterSpacing,
textTransform: theme.tokens.alias.Typography.Heading.OverlineTextCase,
}));