Skip to content

Commit

Permalink
chore: new user avatar tooltip (#9050)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois and thomasheartman authored Jan 3, 2025
1 parent 4a52247 commit 7eced29
Showing 1 changed file with 51 additions and 15 deletions.
66 changes: 51 additions & 15 deletions frontend/src/component/common/UserAvatar/UserAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {
Avatar,
type AvatarProps,
Box,
styled,
type SxProps,
type Theme,
} from '@mui/material';
import type { IUser } from 'interfaces/user';
import { forwardRef } from 'react';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { HtmlTooltip } from '../HtmlTooltip/HtmlTooltip';

const StyledAvatar = styled(Avatar)(({ theme }) => ({
width: theme.spacing(3.5),
height: theme.spacing(3.5),
Expand All @@ -19,6 +20,23 @@ const StyledAvatar = styled(Avatar)(({ theme }) => ({
fontWeight: theme.fontWeight.bold,
}));

const StyledTooltip = styled(Box)(({ theme }) => ({
display: 'flex',
gap: theme.spacing(2),
}));

const StyledTooltipAvatar = styled(StyledAvatar)(({ theme }) => ({
width: theme.spacing(5),
height: theme.spacing(5),
fontSize: theme.fontSizes.smallBody,
}));

const StyledTooltipContent = styled(Box)({
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
});

export interface IUserAvatarProps extends AvatarProps {
user?: Partial<
Pick<IUser, 'id' | 'name' | 'email' | 'username' | 'imageUrl'>
Expand All @@ -37,8 +55,8 @@ const tooltipContent = (
}

const [mainIdentifier, secondaryInfo] = [
user.email || user.username,
user.name,
user.email || user.username,
];

if (mainIdentifier) {
Expand All @@ -56,8 +74,10 @@ const TooltipSecondaryContent = styled('div')(({ theme }) => ({
color: theme.palette.text.secondary,
fontSize: theme.typography.body2.fontSize,
}));

const TooltipMainContent = styled('div')(({ theme }) => ({
fontSize: theme.typography.body1.fontSize,
overflowWrap: 'anywhere',
}));

export const UserAvatar = forwardRef<HTMLDivElement, IUserAvatarProps>(
Expand All @@ -84,29 +104,45 @@ export const UserAvatar = forwardRef<HTMLDivElement, IUserAvatarProps>(
alt={user?.name || user?.email || user?.username || 'Gravatar'}
src={src || user?.imageUrl}
>
<ConditionallyRender
condition={Boolean(fallback)}
show={fallback}
elseShow={children}
/>
{fallback ? fallback : children}
</StyledAvatar>
);

const tooltip = disableTooltip ? undefined : tooltipContent(user);
if (tooltip) {
const { main, secondary } = tooltip;

return (
<HtmlTooltip
arrow
describeChild
maxWidth={400}
title={
<>
<TooltipSecondaryContent>
{tooltip.secondary}
</TooltipSecondaryContent>
<TooltipMainContent>
{tooltip.main}
</TooltipMainContent>
</>
<StyledTooltip>
<StyledTooltipAvatar
src={src || user?.imageUrl}
alt={
user?.name ||
user?.email ||
user?.username ||
'Gravatar'
}
>
{fallback ? fallback : children}
</StyledTooltipAvatar>
<StyledTooltipContent>
{main && (
<TooltipMainContent>
{main}
</TooltipMainContent>
)}
{secondary && (
<TooltipSecondaryContent>
{secondary}
</TooltipSecondaryContent>
)}
</StyledTooltipContent>
</StyledTooltip>
}
>
{Avatar}
Expand Down

0 comments on commit 7eced29

Please sign in to comment.