1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-19 17:52:45 +02:00
unleash.unleash/frontend/src/component/common/UserAvatar/UserAvatar.tsx
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

79 lines
2.0 KiB
TypeScript

import { Avatar, AvatarProps, styled, SxProps, Theme } from '@mui/material';
import { IUser } from 'interfaces/user';
import { FC } from 'react';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
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,
}));
interface IUserAvatarProps extends AvatarProps {
user?: IUser;
src?: string;
title?: string;
onMouseEnter?: (event: any) => void;
onMouseLeave?: () => void;
className?: string;
sx?: SxProps<Theme>;
}
export const UserAvatar: FC<IUserAvatarProps> = ({
user,
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?.includes(' ')) {
fallback = `${fallback.split(' ')[0][0]}${
fallback.split(' ')[1][0]
}`.toUpperCase();
} else if (fallback) {
fallback = fallback[0].toUpperCase();
}
}
return (
<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>
);
};