All files / app/components/Avatar Avatar.tsx

100% Statements 15/15
100% Branches 7/7
100% Functions 4/4
100% Lines 14/14

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 10621x 21x 21x 21x 21x   21x 21x   21x   99x                                                                                   21x             1314x   1314x                                           3008x                   70x                      
import { Avatar as MuiAvatar } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
import { Skeleton } from "@material-ui/lab";
import classNames from "classnames";
import Link from "next/link";
import { User } from "proto/api_pb";
import React from "react";
import { routeToUser } from "routes";
 
import { getProfileLinkA11yLabel } from "./constants";
 
const useStyles = makeStyles((theme) => ({
  avatar: {
    height: "100%",
    position: "absolute",
    top: 0,
    width: "100%",
    maxWidth: "18rem",
    maxHeight: "18rem",
  },
 
  link: {
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
  },
 
  defaultSize: {
    height: "3rem",
    width: "3rem",
  },
 
  grow: {
    height: 0,
    paddingTop: "min(18rem, 100%)",
    width: "100%",
  },
 
  root: {
    flexShrink: 0,
    position: "relative",
  },
}));
 
export interface AvatarProps {
  children?: React.ReactNode;
  user?: User.AsObject;
  grow?: boolean;
  className?: string;
  isProfileLink?: boolean;
  style?: React.CSSProperties;
}
 
export default function Avatar({
  user,
  grow,
  className,
  isProfileLink = true,
  ...otherProps
}: AvatarProps) {
  const classes = useStyles();
 
  return (
    <div
      className={classNames(
        className,
        { [classes.defaultSize]: !className },
        classes.root,
        { [classes.grow]: grow }
      )}
      {...otherProps}
    >
      {user ? (
        isProfileLink ? (
          <Link href={routeToUser(user.username)}>
            <a
              className={classes.link}
              aria-label={getProfileLinkA11yLabel(user.name)}
            >
              <MuiAvatar
                className={classes.avatar}
                alt={user.name}
                src={user.avatarUrl}
              >
                {user.name.split(/\s+/).map((name) => name[0])}
              </MuiAvatar>
            </a>
          </Link>
        ) : (
          <MuiAvatar
            className={classes.avatar}
            alt={user.name}
            src={user.avatarUrl}
          >
            {user.name.split(/\s+/).map((name) => name[0])}
          </MuiAvatar>
        )
      ) : otherProps.children ? (
        <MuiAvatar className={classes.avatar}>{otherProps.children}</MuiAvatar>
      ) : (
        <Skeleton variant="circle" className={classes.avatar} />
      )}
    </div>
  );
}