-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAccountActionButton.tsx
More file actions
230 lines (216 loc) · 7.18 KB
/
Copy pathAccountActionButton.tsx
File metadata and controls
230 lines (216 loc) · 7.18 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
import { ExitToApp, ManageAccounts } from '@mui/icons-material';
import AccountCircle from '@mui/icons-material/AccountCircle';
import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined';
import SupervisedUserCircleIcon from '@mui/icons-material/SupervisedUserCircle';
import SwitchAccountOutlinedIcon from '@mui/icons-material/SwitchAccountOutlined';
import { Chip, Divider } from '@mui/material';
import Badge from '@mui/material/Badge';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import IconButton from '@mui/material/IconButton';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import Tooltip from '@mui/material/Tooltip';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';
import ImpersonateButton from 'components/common/ImpersonateButton';
import StyledDialog from 'components/common/StyledDialog';
import UOLoader from 'components/common/UOLoader';
import { SettingsContext } from 'context/SettingsContextProvider';
import { UserContext } from 'context/UserContextProvider';
import { SettingsId } from 'generated/sdk';
import { getUniqueArrayBy } from 'utils/helperFunctions';
import ProfileInfo from './ProfileInfo';
import RoleSelection from './RoleSelection';
const AccountActionButton = () => {
const navigate = useNavigate();
const [isLoggingOut, setIsLoggingOut] = useState(false);
const [show, setShow] = useState(false);
const { user } = useContext(UserContext);
const settingsContext = useContext(SettingsContext);
const { roles, currentRole, handleLogout, impersonatingUserId } =
useContext(UserContext);
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [searchParams, setSearchParams] = useSearchParams();
const hasMultipleRoles = getUniqueArrayBy(roles, 'id').length > 1;
const humanReadableActiveRole = useMemo(
() =>
roles.find(({ shortCode }) => shortCode.toUpperCase() === currentRole)
?.title ?? 'Unknown',
[roles, currentRole]
);
useEffect(() => {
if (searchParams.get('selectRoles')) {
setShow(hasMultipleRoles);
setSearchParams((searchParams) => {
searchParams.delete('selectRoles');
return searchParams;
});
}
}, [hasMultipleRoles, searchParams, setSearchParams]);
const isUserImpersonated = typeof impersonatingUserId === 'number';
const externalProfileLink = settingsContext.settingsMap.get(
SettingsId.PROFILE_PAGE_LINK
)?.settingsValue;
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const handleModalClose = () => setShow(false);
const handleOnLogout = () => {
setIsLoggingOut(true);
handleLogout().finally(() => {
setIsLoggingOut(false);
});
};
const handleManageAccountClick = () => {
handleClose();
if (externalProfileLink) {
window.open(externalProfileLink, '_blank', 'noopener,noreferrer');
} else {
navigate(`/ProfilePage/${user.id}`);
}
};
return (
<>
<StyledDialog
aria-labelledby="simple-modal-title"
aria-describedby="simple-modal-description"
open={show}
onClose={handleModalClose}
maxWidth="md"
fullWidth
title="Assigned Roles"
>
<DialogContent dividers>
<RoleSelection onClose={handleModalClose} />
</DialogContent>
<DialogActions>
<Button variant="text" onClick={handleModalClose}>
Close
</Button>
</DialogActions>
</StyledDialog>
<>
<Badge
badgeContent={
<Tooltip title="User impersonated (if you want to un-impersonate click on account icon and use Un-impersonate button)">
<InfoOutlinedIcon fontSize="small" />
</Tooltip>
}
sx={(theme) => ({
infoIcon: {
'& .MuiBadge-badge': {
top: theme.spacing(0.5),
right: theme.spacing(0.5),
},
},
})}
invisible={!impersonatingUserId}
>
<IconButton
color="inherit"
aria-controls="simple-menu"
aria-haspopup="true"
aria-label="Account"
onClick={handleClick}
data-cy="profile-page-btn"
sx={{
fontSize: '1rem',
}}
>
<AccountCircle />
</IconButton>
</Badge>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
>
<ProfileInfo />
<Divider style={{ marginBottom: '7px' }} />
<MenuItem
onClick={handleManageAccountClick}
disabled={isLoggingOut}
data-cy="manage-account-button"
>
<Box paddingRight={1} paddingTop={1}>
<ManageAccounts />
</Box>
Manage account
</MenuItem>
{hasMultipleRoles && (
<MenuItem
onClick={() => {
setShow(true);
handleClose();
}}
data-cy="change-roles-button"
>
<Box paddingRight={1} paddingTop={1}>
<SupervisedUserCircleIcon />
</Box>
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
flex: 1,
}}
>
<Box>Roles</Box>
<Chip
label={humanReadableActiveRole}
color="primary"
size="small"
sx={{
fontSize: '10px',
height: 'inherit',
lineHeight: 2,
}}
/>
</Box>
</MenuItem>
)}
{isUserImpersonated && (
<MenuItem data-cy="un-impersonate">
<ImpersonateButton
userId={impersonatingUserId}
data-cy="un-impersonate"
variant="text"
startIcon={<SwitchAccountOutlinedIcon />}
sx={{
textTransform: 'none',
fontSize: '1rem',
color: 'inherit',
'&:hover': {
backgroundColor: 'transparent ',
},
}}
>
Un-impersonate
</ImpersonateButton>
</MenuItem>
)}
<MenuItem
data-cy="logout"
onClick={handleOnLogout}
disabled={isLoggingOut}
>
<Box paddingRight={1} paddingTop={1}>
{isLoggingOut ? <UOLoader size={24} /> : <ExitToApp />}
</Box>
Logout
</MenuItem>
</Menu>
</>
</>
);
};
export default AccountActionButton;