All files / app/components/Avatar Avatar.tsx

100% Statements 14/14
88.88% Branches 16/18
100% Functions 6/6
100% Lines 12/12

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 111 112 11321x 21x   21x 21x   21x                                                     21x     590x             501x           590x                 67x                                                             1260x                       104x                      
import { Avatar as MuiAvatar, styled } from "@mui/material";
import Link from "next/link";
import { LiteUser } from "proto/api_pb";
import React from "react";
import { routeToUser } from "routes";
 
import { getProfileLinkA11yLabel } from "./constants";
 
type UserWithAvatarUrl = Pick<
  LiteUser.AsObject,
  "username" | "name" | "avatarUrl"
>;
type UserWithAvatarThumbnailUrl = Pick<
  LiteUser.AsObject,
  "username" | "name" | "avatarThumbnailUrl"
>;
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 StyledLink = styled(Link)(({ theme }) => ({
  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) {
  return (
    <StyledWrapper
      isDefaultSize={!className}
      grow={grow}
      className={className}
      {...otherProps}
    >
      {user ? (
        isProfileLink ? (
          <StyledLink
            href={routeToUser(user.username)}
            aria-label={getProfileLinkA11yLabel(user.name)}
            target={openInNewTab ? "_blank" : undefined}
          >
            <StyledMuiAvatar
              alt={user.name}
              src={
                !!highRes
                  ? (user as UserWithAvatarUrl).avatarUrl
                  : (user as UserWithAvatarThumbnailUrl).avatarThumbnailUrl
              }
            >
              {user.name.split(/\s+/).map((name) => name[0])}
            </StyledMuiAvatar>
          </StyledLink>
        ) : (
          <StyledMuiAvatar
            alt={user.name}
            src={
              !!highRes
                ? (user as UserWithAvatarUrl).avatarUrl
                : (user as UserWithAvatarThumbnailUrl).avatarThumbnailUrl
            }
          >
            {user.name.split(/\s+/).map((name) => name[0])}
          </StyledMuiAvatar>
        )
      ) : otherProps.children ? (
        <StyledMuiAvatar>{otherProps.children}</StyledMuiAvatar>
      ) : (
        <StyledMuiAvatar />
      )}
    </StyledWrapper>
  );
}