import PropTypes from 'prop-types' import { createElement } from 'react' import { Flex, Typography, Button, Space, Dropdown, Divider } from 'antd' import { UserOutlined } from '@ant-design/icons' import { useContext } from 'react' import { useNavigate } from 'react-router-dom' import LogoutIcon from '../../Icons/LogoutIcon' import { User } from '../../../database/models/User' import { AuthContext } from '../context/AuthContext' const { Text } = Typography const ICON_ACTION_NAMES = ['info', 'edit'] const UserProfilePopover = ({ onClose }) => { const { userProfile, profileImageUrl, logout } = useContext(AuthContext) const navigate = useNavigate() const modelActions = User.actions || [] const iconActions = modelActions.filter((a) => ICON_ACTION_NAMES.includes(a.name)) const dropdownActions = modelActions.filter( (a) => !ICON_ACTION_NAMES.includes(a.name) ) const objectData = { ...userProfile, _user: userProfile } const runAction = (action) => { if (action.name === 'logout') { logout() } else if (action.url && userProfile?._id) { const url = action.url(userProfile._id) navigate(url) } onClose?.() } const isActionDisabled = (action) => { if (action.disabled && typeof action.disabled === 'function') { return action.disabled(objectData) } return false } const username = userProfile?.username const fullName = [userProfile?.firstName, userProfile?.lastName] .filter(Boolean) .join(' ') const email = userProfile?.email const dropdownItems = [ ...dropdownActions.map((action) => ({ key: action.name, label: action.label, icon: action.icon ? createElement(action.icon) : undefined, disabled: isActionDisabled(action), onClick: () => !isActionDisabled(action) && runAction(action) })), { type: 'divider' }, { key: 'logout', label: 'Logout', icon: createElement(LogoutIcon), onClick: () => runAction({ name: 'logout' }) } ] const actionButton = dropdownItems.length > 1 ? ( ) : null return ( {profileImageUrl ? ( ) : ( )} {fullName && ( {fullName} )} {email && ( @{username} {email} )} {!username && !fullName && !email && ( Unknown user )} {iconActions.map((action) => (