/******************************************************************************** * Copyright (c) 2019 TypeFox and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ import { FunctionComponent, useContext, useRef, useState } from 'react'; import { Avatar, Box, IconButton, Link, Menu, MenuItem, Typography } from '@mui/material'; import { Link as RouteLink } from 'react-router'; import SettingsIcon from '@mui/icons-material/Settings'; import AdminPanelSettingsIcon from '@mui/icons-material/AdminPanelSettings'; import LogoutIcon from '@mui/icons-material/Logout'; import { UserSettingsRoutes } from './user-settings-routes'; import { AdminDashboardRoutes } from '../admin-dashboard/admin-dashboard-routes'; import { MainContext } from '../../context'; import { LogoutForm } from './logout'; // Radius, font and min-height come from the MuiMenuItem theme override. const menuItemSx = { py: '0.5rem', px: '0.625rem', gap: '0.625rem', color: 'text.primary', display: 'flex', alignItems: 'center' } as const; const iconSx = { fontSize: '1.0625rem', color: 'text.disabled', flexShrink: 0 }; export const UserAvatar: FunctionComponent = () => { const [open, setOpen] = useState(false); const context = useContext(MainContext); const anchorRef = useRef(null); const logoutFormRef = useRef(null); const user = context.user; if (!user) return null; const initials = user.loginName.slice(0, 2).toUpperCase(); return ( <> setOpen(true)} sx={{ p: '0.3125rem' }}> {initials} setOpen(false)}> {/* User header */} {initials} Logged in as setOpen(false)}> {user.loginName} setOpen(false)} sx={{ ...menuItemSx, textDecoration: 'none' }}> Settings {user.role === 'admin' && ( setOpen(false)} sx={{ ...menuItemSx, textDecoration: 'none' }}> Admin Dashboard )} logoutFormRef.current?.submit()} sx={menuItemSx}> Log out ); };