Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI] EVEREST-1190 Display logged in user #1067

Merged
merged 9 commits into from
Feb 5, 2025
26 changes: 26 additions & 0 deletions ui/apps/everest/src/components/app-bar/user-icon/UserIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { useContext, useState } from 'react';
import {
Divider,
FormControlLabel,
FormGroup,
IconButton,
Menu,
MenuItem,
Switch,
Typography,
} from '@mui/material';
import PersonOutlineIcon from '@mui/icons-material/PersonOutline';
import { ColorModeContext } from '@percona/design';
import { AuthContext } from 'contexts/auth';
import { jwtDecode } from 'jwt-decode';

interface UserToken {
preferred_username?: string;
name?: string;
email?: string;
sub: string;
}

const AppBarUserIcon = () => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
Expand All @@ -23,6 +33,16 @@ const AppBarUserIcon = () => {
const handleClose = () => {
setAnchorEl(null);
};
const token = localStorage.getItem('everestToken');
const decoded = jwtDecode(token!) as UserToken;

const preferredUsername = decoded.preferred_username;
const name = decoded.name;
const email = decoded.email;
const sub =
decoded.sub?.substring(0, decoded.sub.indexOf(':')) || decoded.sub;

const userToShow = preferredUsername || name || email || sub;

return (
<>
Expand All @@ -44,6 +64,12 @@ const AppBarUserIcon = () => {
open={Boolean(anchorEl)}
onClose={handleClose}
>
<MenuItem sx={{ cursor: 'text', userSelect: 'text' }}>
<Typography variant="helperText" color="text.secondary">
{userToShow}
</Typography>
</MenuItem>
<Divider />
<MenuItem>
<FormGroup>
<FormControlLabel
Expand Down
4 changes: 4 additions & 0 deletions ui/apps/everest/src/components/drawer/Drawer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { DrawerContextProvider } from 'contexts/drawer/drawer.context';
const queryClient = new QueryClient();
vi.mock('hooks/api/version/useVersion');

vi.mock('jwt-decode', () => ({
jwtDecode: vi.fn(() => ({ sub: '12345', name: 'Test User' })),
}));

const BarAndDrawer = () => (
<QueryClientProvider client={queryClient}>
<DrawerContextProvider>
Expand Down
Loading