All files / app/components/Avatar Avatar.tsx

100% Statements 16/16
100% Branches 9/9
100% Functions 7/7
100% Lines 13/13

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 8921x 21x   21x 21x   21x   21x     547x             460x           483x                 64x                                   374x                                         1160x         58x                      
import { Avatar as MuiAvatar, Skeleton, 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";
 
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",
}));
 
const StyledSkeleton = styled(Skeleton)(() => ({
  height: "100%",
  position: "absolute",
  top: 0,
  width: "100%",
  maxWidth: "18rem",
  maxHeight: "18rem",
}));
 
export interface AvatarProps {
  children?: React.ReactNode;
  user?: LiteUser.AsObject;
  grow?: boolean;
  className?: string;
  isProfileLink?: boolean;
  style?: React.CSSProperties;
}
 
export default function Avatar({
  user,
  grow,
  className,
  isProfileLink = true,
  ...otherProps
}: AvatarProps) {
  return (
    <StyledWrapper
      isDefaultSize={!className}
      grow={grow}
      className={className}
      {...otherProps}
    >
      {user ? (
        isProfileLink ? (
          <StyledLink
            href={routeToUser(user.username)}
            aria-label={getProfileLinkA11yLabel(user.name)}
          >
            <StyledMuiAvatar alt={user.name} src={user.avatarUrl}>
              {user.name.split(/\s+/).map((name) => name[0])}
            </StyledMuiAvatar>
          </StyledLink>
        ) : (
          <StyledMuiAvatar alt={user.name} src={user.avatarUrl}>
            {user.name.split(/\s+/).map((name) => name[0])}
          </StyledMuiAvatar>
        )
      ) : otherProps.children ? (
        <StyledMuiAvatar>{otherProps.children}</StyledMuiAvatar>
      ) : (
        <StyledSkeleton variant="circular" />
      )}
    </StyledWrapper>
  );
}