import { useState, useEffect, type ReactNode, type MouseEvent, type CSSProperties } from 'react';
import { IconButton, Avatar, Menu as MuiMenu, Box, MenuItem } from '@mui/material';
import PersonIcon from '@mui/icons-material/Person';
import Brightness5Icon from '@mui/icons-material/Brightness5';
import SettingsIcon from '@mui/icons-material/Settings';
import HelpOutlineIcon from '@mui/icons-material/HelpOutline';
import LogoutIcon from '@mui/icons-material/Logout';
import RdsAvatar from '../../rds-avatar/rds-avatar';
import '../styles/rds-profile-menu.scss';
export interface ProfileMenuItem {
label: string;
onClick?: () => void;
sx?: object;
icon?: ReactNode;
}
export type ProfileMenuVariant = 'rich' | 'compact';
export interface ProfileMenuProps {
name: string;
email: string;
/** Compact: initials avatar + name. Rich: RdsAvatar with optional designation. */
variant?: ProfileMenuVariant;
/** Initials for compact variant (defaults to first letters of name) */
shortName?: string;
designation?: string;
menuItems?: ProfileMenuItem[];
}
const getInitials = (name: string): string =>
name
.split(' ')
.filter(Boolean)
.map((part) => part.charAt(0))
.join('')
.toUpperCase();
const DEFAULT_MENU_ITEMS: ProfileMenuItem[] = [
{ label: 'My Profile', icon: },
{ label: 'Theme', icon: },
{ label: 'Setting', icon: },
{ label: 'Help', icon: },
{
label: 'Logout',
icon: ,
sx: { borderRadius: '0 0 var(--rds-border-radius-lg, 8px) var(--rds-border-radius-lg, 8px)' },
},
];
export const ProfileMenu = ({
name,
email,
variant = 'rich',
shortName,
designation = 'Developer',
menuItems,
}: ProfileMenuProps) => {
const [anchorEl, setAnchorEl] = useState(null);
const [isSmallScreen, setIsSmallScreen] = useState(() => {
if (typeof window === 'undefined') {
return false;
}
return window.innerWidth <= 834;
});
const open = Boolean(anchorEl);
const initials = shortName ?? getInitials(name);
const items = menuItems && menuItems.length > 0 ? menuItems : DEFAULT_MENU_ITEMS;
useEffect(() => {
const checkScreenSize = () => {
setIsSmallScreen(window.innerWidth <= 834);
};
window.addEventListener('resize', checkScreenSize);
return () => window.removeEventListener('resize', checkScreenSize);
}, []);
const handleClick = (event: MouseEvent) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
return (
<>
{variant === 'rich' ? (
<>
{!isSmallScreen && (
)}
>
) : (
<>
{initials}
{name}
>
)}
{initials}
{name}
{email}
{items.map((item, idx) => (
))}
>
);
};
export default ProfileMenu;
ProfileMenu.displayName = 'ProfileMenu';