143 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-02-18 01:44:48 -05:00
// ** React Imports
2024-02-18 06:59:55 -05:00
import { useState, SyntheticEvent, Fragment, useEffect } from 'react'
2024-02-18 01:44:48 -05:00
// ** Next Import
import { useRouter } from 'next/router'
// ** MUI Imports
import Box from '@mui/material/Box'
import Menu from '@mui/material/Menu'
import Badge from '@mui/material/Badge'
import Avatar from '@mui/material/Avatar'
import Divider from '@mui/material/Divider'
import MenuItem from '@mui/material/MenuItem'
import { styled } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
// ** Icons Imports
import CogOutline from 'mdi-material-ui/CogOutline'
import CurrencyUsd from 'mdi-material-ui/CurrencyUsd'
import EmailOutline from 'mdi-material-ui/EmailOutline'
import LogoutVariant from 'mdi-material-ui/LogoutVariant'
import AccountOutline from 'mdi-material-ui/AccountOutline'
import MessageOutline from 'mdi-material-ui/MessageOutline'
import HelpCircleOutline from 'mdi-material-ui/HelpCircleOutline'
import { useUser } from '@auth0/nextjs-auth0/client'
// ** Styled Components
const BadgeContentSpan = styled('span')(({ theme }) => ({
width: 8,
height: 8,
borderRadius: '50%',
backgroundColor: theme.palette.success.main,
boxShadow: `0 0 0 2px ${theme.palette.background.paper}`
}))
const UserDropdown = () => {
const { user, isLoading } = useUser();
2024-02-18 06:59:55 -05:00
const [appUser, setAppUser] = useState(null);
useEffect(() => {
getData()
}, []);
const getData = async () => {
var userResponse = await fetch('/api/me');
var user = await userResponse.json();
setAppUser(user);
}
2024-02-18 01:44:48 -05:00
// ** States
const [anchorEl, setAnchorEl] = useState<Element | null>(null)
// ** Hooks
const router = useRouter()
const handleDropdownOpen = (event: SyntheticEvent) => {
setAnchorEl(event.currentTarget)
}
const handleDropdownClose = (url?: string) => {
if (url) {
router.push(url)
}
setAnchorEl(null)
}
const styles = {
py: 2,
px: 4,
width: '100%',
display: 'flex',
alignItems: 'center',
color: 'text.primary',
textDecoration: 'none',
'& svg': {
fontSize: '1.375rem',
color: 'text.secondary'
}
}
return (
2024-02-18 06:59:55 -05:00
(!isLoading && user && appUser) ? (
2024-02-18 01:44:48 -05:00
<Fragment>
<Badge
overlap='circular'
onClick={handleDropdownOpen}
sx={{ ml: 2, cursor: 'pointer' }}
badgeContent={<BadgeContentSpan />}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
>
<Avatar
alt={user.nickname}
onClick={handleDropdownOpen}
sx={{ width: 40, height: 40 }}
src={user.picture}
/>
</Badge>
<Menu
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={() => handleDropdownClose()}
sx={{ '& .MuiMenu-paper': { width: 230, marginTop: 4 } }}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
transformOrigin={{ vertical: 'top', horizontal: 'right' }}
>
<Box sx={{ pt: 2, pb: 3, px: 4 }}>
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<Badge
overlap='circular'
badgeContent={<BadgeContentSpan />}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
>
2024-02-18 06:59:55 -05:00
<Avatar alt={appUser["displayName"]} src={user.picture} sx={{ width: '2.5rem', height: '2.5rem' }} />
2024-02-18 01:44:48 -05:00
</Badge>
<Box sx={{ display: 'flex', marginLeft: 3, alignItems: 'flex-start', flexDirection: 'column' }}>
2024-02-18 06:59:55 -05:00
<Typography sx={{ fontWeight: 600 }}>{appUser["displayName"]}</Typography>
2024-02-18 01:44:48 -05:00
<Typography variant='body2' sx={{ fontSize: '0.8rem', color: 'text.disabled' }}>
Welcome back!
</Typography>
</Box>
</Box>
</Box>
<Divider sx={{ mt: 0, mb: 1 }} />
2024-02-18 06:59:55 -05:00
<MenuItem sx={{ p: 0 }} onClick={() => router.push("/dashboard/settings")}>
2024-02-18 01:44:48 -05:00
<Box sx={styles}>
<CogOutline sx={{ marginRight: 2 }} />
Settings
</Box>
</MenuItem>
<Divider />
<MenuItem sx={{ py: 2 }} onClick={() => handleDropdownClose('/api/auth/logout')}>
<LogoutVariant sx={{ marginRight: 2, fontSize: '1.375rem', color: 'text.secondary' }} />
Logout
</MenuItem>
</Menu>
</Fragment>
): null
)
}
export default UserDropdown