'use client' // React Imports import { useRef, useState } from 'react' import type { MouseEvent } from 'react' // Next Imports import { useParams, useRouter } from 'next/navigation' // MUI Imports import { styled } from '@mui/material/styles' import Badge from '@mui/material/Badge' import Avatar from '@mui/material/Avatar' import Popper from '@mui/material/Popper' import Fade from '@mui/material/Fade' import Paper from '@mui/material/Paper' import ClickAwayListener from '@mui/material/ClickAwayListener' import MenuList from '@mui/material/MenuList' import Typography from '@mui/material/Typography' import Divider from '@mui/material/Divider' import MenuItem from '@mui/material/MenuItem' import Button from '@mui/material/Button' // Type Imports import type { Locale } from '@configs/i18n' // Hook Imports import { useSettings } from '@core/hooks/useSettings' // Util Imports import { getLocalizedUrl } from '@/utils/i18n' import { useAuthMutation } from '../../../services/mutations/auth' import { useAuth } from '../../../contexts/authContext' import { CircularProgress } from '@mui/material' // Styled component for badge content const BadgeContentSpan = styled('span')({ width: 8, height: 8, borderRadius: '50%', cursor: 'pointer', backgroundColor: 'var(--mui-palette-success-main)', boxShadow: '0 0 0 2px var(--mui-palette-background-paper)' }) const UserDropdown = () => { const { currentUser } = useAuth() // States const [open, setOpen] = useState(false) // Refs const anchorRef = useRef(null) const { logout } = useAuthMutation() // Hooks const router = useRouter() const { settings } = useSettings() const { lang: locale } = useParams() const handleDropdownOpen = () => { !open ? setOpen(true) : setOpen(false) } const handleDropdownClose = (event?: MouseEvent | (MouseEvent | TouchEvent), url?: string) => { if (url) { router.push(getLocalizedUrl(url, locale as Locale)) } if (anchorRef.current && anchorRef.current.contains(event?.target as HTMLElement)) { return } setOpen(false) } const handleUserLogout = async () => { try { // Sign out from the app // await signOut({ callbackUrl: process.env.NEXT_PUBLIC_APP_URL }) logout.mutate() router.push(getLocalizedUrl('/login', locale as Locale)) } catch (error) { console.error(error) // Show above error in a toast like following // toastService.error((err as Error).message) } } if (!currentUser) { return null } return ( <> } anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }} className='mis-2' > {({ TransitionProps, placement }) => ( handleDropdownClose(e as MouseEvent | TouchEvent)}>
{currentUser?.name} {currentUser?.email}
handleDropdownClose(e, '/pages/user-profile')}> My Profile handleDropdownClose(e, '/pages/account-settings')}> Settings handleDropdownClose(e, '/pages/pricing')}> Pricing handleDropdownClose(e, '/pages/faq')}> FAQ
)}
) } export default UserDropdown