1 | import { m } from 'framer-motion';
|
---|
2 | // @mui
|
---|
3 | import { alpha } from '@mui/material/styles';
|
---|
4 | import Box from '@mui/material/Box';
|
---|
5 | import Stack from '@mui/material/Stack';
|
---|
6 | import Avatar from '@mui/material/Avatar';
|
---|
7 | import Divider from '@mui/material/Divider';
|
---|
8 | import MenuItem from '@mui/material/MenuItem';
|
---|
9 | import IconButton from '@mui/material/IconButton';
|
---|
10 | import Typography from '@mui/material/Typography';
|
---|
11 | // routes
|
---|
12 | import { useRouter } from 'src/routes/hooks';
|
---|
13 | // auth
|
---|
14 | import { useAuthContext } from 'src/auth/hooks';
|
---|
15 | // components
|
---|
16 | import { varHover } from 'src/components/animate';
|
---|
17 | import CustomPopover, { usePopover } from 'src/components/custom-popover';
|
---|
18 |
|
---|
19 | // ----------------------------------------------------------------------
|
---|
20 |
|
---|
21 | const OPTIONS = [
|
---|
22 | {
|
---|
23 | label: 'Home',
|
---|
24 | linkTo: '/',
|
---|
25 | },
|
---|
26 | {
|
---|
27 | label: 'Profile',
|
---|
28 | linkTo: '/#1',
|
---|
29 | },
|
---|
30 | {
|
---|
31 | label: 'Settings',
|
---|
32 | linkTo: '/#2',
|
---|
33 | },
|
---|
34 | ];
|
---|
35 |
|
---|
36 | // ----------------------------------------------------------------------
|
---|
37 |
|
---|
38 | export default function AccountPopover() {
|
---|
39 | const router = useRouter();
|
---|
40 |
|
---|
41 | const { logout, user } = useAuthContext();
|
---|
42 |
|
---|
43 | const popover = usePopover();
|
---|
44 |
|
---|
45 | const handleLogout = async () => {
|
---|
46 | try {
|
---|
47 | await logout();
|
---|
48 | popover.onClose();
|
---|
49 | router.replace('/');
|
---|
50 | } catch (error) {
|
---|
51 | console.error(error);
|
---|
52 | }
|
---|
53 | };
|
---|
54 |
|
---|
55 | const handleClickItem = (path: string) => {
|
---|
56 | popover.onClose();
|
---|
57 | router.push(path);
|
---|
58 | };
|
---|
59 |
|
---|
60 | return (
|
---|
61 | <>
|
---|
62 | <IconButton
|
---|
63 | component={m.button}
|
---|
64 | whileTap="tap"
|
---|
65 | whileHover="hover"
|
---|
66 | variants={varHover(1.05)}
|
---|
67 | onClick={popover.onOpen}
|
---|
68 | sx={{
|
---|
69 | width: 40,
|
---|
70 | height: 40,
|
---|
71 | background: (theme) => alpha(theme.palette.grey[500], 0.08),
|
---|
72 | ...(popover.open && {
|
---|
73 | background: (theme) =>
|
---|
74 | `linear-gradient(135deg, ${theme.palette.primary.light} 0%, ${theme.palette.primary.main} 100%)`,
|
---|
75 | }),
|
---|
76 | }}
|
---|
77 | >
|
---|
78 | <Avatar
|
---|
79 | src={user?.photoURL}
|
---|
80 | alt={user?.displayName}
|
---|
81 | sx={{
|
---|
82 | width: 36,
|
---|
83 | height: 36,
|
---|
84 | border: (theme) => `solid 2px ${theme.palette.background.default}`,
|
---|
85 | }}
|
---|
86 | >
|
---|
87 | {user?.displayName.charAt(0).toUpperCase()}
|
---|
88 | </Avatar>
|
---|
89 | </IconButton>
|
---|
90 |
|
---|
91 | <CustomPopover open={popover.open} onClose={popover.onClose} sx={{ width: 200, p: 0 }}>
|
---|
92 | <Box sx={{ p: 2, pb: 1.5 }}>
|
---|
93 | <Typography variant="subtitle2" noWrap>
|
---|
94 | {user?.displayName}
|
---|
95 | </Typography>
|
---|
96 |
|
---|
97 | <Typography variant="body2" sx={{ color: 'text.secondary' }} noWrap>
|
---|
98 | {user?.email}
|
---|
99 | </Typography>
|
---|
100 | </Box>
|
---|
101 |
|
---|
102 | <Divider sx={{ borderStyle: 'dashed' }} />
|
---|
103 |
|
---|
104 | <Stack sx={{ p: 1 }}>
|
---|
105 | {OPTIONS.map((option) => (
|
---|
106 | <MenuItem key={option.label} onClick={() => handleClickItem(option.linkTo)}>
|
---|
107 | {option.label}
|
---|
108 | </MenuItem>
|
---|
109 | ))}
|
---|
110 | </Stack>
|
---|
111 |
|
---|
112 | <Divider sx={{ borderStyle: 'dashed' }} />
|
---|
113 |
|
---|
114 | <MenuItem
|
---|
115 | onClick={handleLogout}
|
---|
116 | sx={{ m: 1, fontWeight: 'fontWeightBold', color: 'error.main' }}
|
---|
117 | >
|
---|
118 | Logout
|
---|
119 | </MenuItem>
|
---|
120 | </CustomPopover>
|
---|
121 | </>
|
---|
122 | );
|
---|
123 | }
|
---|