All files / app/components/Avatar Avatar.tsx

100% Statements 12/12
100% Branches 16/16
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 11124x 24x   24x   24x                                                         24x     800x             24x           800x                 251x                 800x                 2125x                                                                  
import { Avatar as MuiAvatar, styled } from "@mui/material";
import ProfileLink from "components/ProfileLink/ProfileLink";
import { LiteUser } from "proto/api_pb";
import React from "react";
 
import { getProfileLinkA11yLabel } from "./constants";
 
type UserWithAvatarUrl = Pick<
  LiteUser.AsObject,
  "username" | "name" | "avatarUrl"
> & {
  userId?: number;
};
type UserWithAvatarThumbnailUrl = Pick<
  LiteUser.AsObject,
  "username" | "name" | "avatarThumbnailUrl"
> & { userId?: number };
interface AvatarPropsHighRes {
  children?: React.ReactNode;
  highRes?: true;
  user?: UserWithAvatarUrl;
  grow?: boolean;
  className?: string;
  isProfileLink?: boolean;
  style?: React.CSSProperties;
  openInNewTab?: boolean;
}
 
interface AvatarPropsLowRes
  extends Omit<AvatarPropsHighRes, "highRes" | "user"> {
  highRes?: false | undefined;
  user?: UserWithAvatarThumbnailUrl;
}
 
const StyledWrapper = styled("div")<{
  isDefaultSize: boolean;
  grow: boolean | undefined;
}>(({ isDefaultSize, grow }) => ({
  flexShrink: 0,
  position: "relative",
  ...(isDefaultSize && { height: "3rem", width: "3rem" }),
  ...(grow && { height: 0, paddingTop: "min(18rem, 100%)", width: "100%" }),
}));
 
const linkStyle: React.CSSProperties = {
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
};
 
const StyledMuiAvatar = styled(MuiAvatar)(() => ({
  height: "100%",
  position: "absolute",
  top: 0,
  width: "100%",
  maxWidth: "18rem",
  maxHeight: "18rem",
}));
 
export default function Avatar({
  user,
  highRes,
  grow,
  className,
  isProfileLink = true,
  openInNewTab = false,
  ...otherProps
}: AvatarPropsHighRes | AvatarPropsLowRes) {
  const avatarImg = user ? (
    <StyledMuiAvatar
      alt={user.name}
      src={
        !!highRes
          ? (user as UserWithAvatarUrl).avatarUrl
          : (user as UserWithAvatarThumbnailUrl).avatarThumbnailUrl
      }
    >
      {user.name.split(/\s+/).map((name) => name[0])}
    </StyledMuiAvatar>
  ) : null;
 
  return (
    <StyledWrapper
      isDefaultSize={!className}
      grow={grow}
      className={className}
      {...otherProps}
    >
      {user ? (
        isProfileLink ? (
          <ProfileLink
            userId={user.userId}
            username={user.username}
            aria-label={getProfileLinkA11yLabel(user.name)}
            openInNewTab={openInNewTab}
            style={linkStyle}
          >
            {avatarImg}
          </ProfileLink>
        ) : (
          avatarImg
        )
      ) : otherProps.children ? (
        <StyledMuiAvatar>{otherProps.children}</StyledMuiAvatar>
      ) : (
        <StyledMuiAvatar />
      )}
    </StyledWrapper>
  );
}