File size: 2,944 Bytes
1e92f2d | 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 | import * as React from 'react';
import RouterLink from 'next/link';
import { useRouter } from 'next/navigation';
import Box from '@mui/material/Box';
import Divider from '@mui/material/Divider';
import ListItemIcon from '@mui/material/ListItemIcon';
import MenuItem from '@mui/material/MenuItem';
import MenuList from '@mui/material/MenuList';
import Popover from '@mui/material/Popover';
import Typography from '@mui/material/Typography';
import { GearSixIcon } from '@phosphor-icons/react/dist/ssr/GearSix';
import { SignOutIcon } from '@phosphor-icons/react/dist/ssr/SignOut';
import { UserIcon } from '@phosphor-icons/react/dist/ssr/User';
import { paths } from '@/paths';
import { authClient } from '@/lib/auth/client';
import { logger } from '@/lib/default-logger';
import { useUser } from '@/hooks/use-user';
export interface UserPopoverProps {
anchorEl: Element | null;
onClose: () => void;
open: boolean;
}
export function UserPopover({ anchorEl, onClose, open }: UserPopoverProps): React.JSX.Element {
const { checkSession } = useUser();
const router = useRouter();
const handleSignOut = React.useCallback(async (): Promise<void> => {
try {
const { error } = await authClient.signOut();
if (error) {
logger.error('Sign out error', error);
return;
}
// Refresh the auth state
await checkSession?.();
// UserProvider, for this case, will not refresh the router and we need to do it manually
router.refresh();
// After refresh, AuthGuard will handle the redirect
} catch (error) {
logger.error('Sign out error', error);
}
}, [checkSession, router]);
return (
<Popover
anchorEl={anchorEl}
anchorOrigin={{ horizontal: 'left', vertical: 'bottom' }}
onClose={onClose}
open={open}
slotProps={{ paper: { sx: { width: '240px' } } }}
>
<Box sx={{ p: '16px 20px ' }}>
<Typography variant="subtitle1">Sofia Rivers</Typography>
<Typography color="text.secondary" variant="body2">
sofia.rivers@devias.io
</Typography>
</Box>
<Divider />
<MenuList disablePadding sx={{ p: '8px', '& .MuiMenuItem-root': { borderRadius: 1 } }}>
<MenuItem component={RouterLink} href={paths.dashboard.settings} onClick={onClose}>
<ListItemIcon>
<GearSixIcon fontSize="var(--icon-fontSize-md)" />
</ListItemIcon>
Settings
</MenuItem>
<MenuItem component={RouterLink} href={paths.dashboard.account} onClick={onClose}>
<ListItemIcon>
<UserIcon fontSize="var(--icon-fontSize-md)" />
</ListItemIcon>
Profile
</MenuItem>
<MenuItem onClick={handleSignOut}>
<ListItemIcon>
<SignOutIcon fontSize="var(--icon-fontSize-md)" />
</ListItemIcon>
Sign out
</MenuItem>
</MenuList>
</Popover>
);
}
|