1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00
unleash.unleash/frontend/src/component/common/UserAvatar/UserAvatar.tsx
sjaanus d10c151dea Feat/groups refinement (#1190)
* Button for 0 groups

* Highlight name on exist

* Add hover to groups

* Change avatar size to 28px

* Add tooltip to project and fix error

* Fix tooltip

* Link to project, change to flex etc

* Reuse badges better

* Limit to max 50% width

* Refinements

* UI refinements

* Update

* Remove import

* Refinement fixes

* Refinement

* Refinement

* Refinement

* Star to star rounded
2022-08-03 18:57:48 +00:00

116 lines
2.9 KiB
TypeScript

import {
Avatar,
AvatarProps,
Badge,
styled,
SxProps,
Theme,
} from '@mui/material';
import { IUser } from 'interfaces/user';
import { FC } from 'react';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { StarRounded } from '@mui/icons-material';
const StyledAvatar = styled(Avatar)(({ theme }) => ({
width: theme.spacing(3.5),
height: theme.spacing(3.5),
margin: 'auto',
backgroundColor: theme.palette.secondary.light,
color: theme.palette.text.primary,
fontSize: theme.fontSizes.smallerBody,
fontWeight: theme.fontWeight.bold,
}));
const StyledStar = styled(StarRounded)(({ theme }) => ({
color: theme.palette.warning.main,
backgroundColor: theme.palette.background.paper,
borderRadius: theme.shape.borderRadiusExtraLarge,
fontSize: theme.fontSizes.smallBody,
marginLeft: theme.spacing(-1),
}));
interface IUserAvatarProps extends AvatarProps {
user?: IUser;
star?: boolean;
src?: string;
title?: string;
onMouseEnter?: (event: any) => void;
onMouseLeave?: () => void;
className?: string;
sx?: SxProps<Theme>;
}
export const UserAvatar: FC<IUserAvatarProps> = ({
user,
star,
src,
title,
onMouseEnter,
onMouseLeave,
className,
sx,
children,
...props
}) => {
if (!title && !onMouseEnter && user) {
title = `${user?.name || user?.email || user?.username} (id: ${
user?.id
})`;
}
if (!src && user) {
src = user?.imageUrl;
}
let fallback;
if (!children && user) {
fallback = user?.name || user?.email || user?.username;
if (fallback && fallback.includes(' ')) {
fallback = `${fallback.split(' ')[0][0]}${
fallback.split(' ')[1][0]
}`.toUpperCase();
} else if (fallback) {
fallback = fallback[0].toUpperCase();
}
}
const avatar = (
<StyledAvatar
className={className}
sx={sx}
{...props}
data-loading
alt="Gravatar"
src={src}
title={title}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
<ConditionallyRender
condition={Boolean(fallback)}
show={fallback}
elseShow={children}
/>
</StyledAvatar>
);
return (
<ConditionallyRender
condition={Boolean(star)}
show={
<Badge
overlap="circular"
anchorOrigin={{
vertical: 'top',
horizontal: 'left',
}}
badgeContent={<StyledStar />}
>
{avatar}
</Badge>
}
elseShow={avatar}
/>
);
};