All files / app/components UserSummary.tsx

100% Statements 18/18
95.23% Branches 20/21
100% Functions 2/2
100% Lines 15/15

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 15017x 17x 17x 17x 17x 17x   17x 17x   43x                                                                                   17x                       17x                 444x     444x                                     444x         444x                                                                                                  
import { ListItemAvatar, ListItemText, Typography } from "@material-ui/core";
import { Skeleton } from "@material-ui/lab";
import classNames from "classnames";
import Avatar from "components/Avatar";
import { OpenInNewIcon } from "components/Icons";
import StyledLink from "components/StyledLink";
import { User } from "proto/api_pb";
import { routeToUser } from "routes";
import makeStyles from "utils/makeStyles";
 
export const useStyles = makeStyles((theme) => ({
  avatar: {
    marginInlineEnd: theme.spacing(2),
  },
  avatarBig: {
    height: "4.5rem",
    width: "4.5rem",
  },
  avatarSmall: {
    height: "3rem",
    width: "3rem",
  },
  root: {
    display: "flex",
    padding: 0,
    width: "100%",
    alignItems: "center",
  },
  titleSkeleton: {
    maxWidth: 300,
  },
  title: {
    marginTop: 0,
  },
  link: {
    display: "flex",
    alignItems: "center",
  },
  linkIcon: {
    display: "block",
    marginInlineStart: theme.spacing(0.5),
    height: "1.25rem",
    width: "1.25rem",
  },
  titleAndBarContainer: {
    display: "grid",
    gridGap: theme.spacing(0.5),
    margin: 0,
    minHeight: theme.spacing(9),
  },
}));
 
export const USER_TITLE_SKELETON_TEST_ID = "user-title-skeleton";
 
export interface UserSummaryProps {
  avatarIsLink?: boolean;
  children?: React.ReactNode;
  smallAvatar?: boolean;
  nameOnly?: boolean;
  headlineComponent?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
  user?: User.AsObject;
  titleIsLink?: boolean;
}
 
export default function UserSummary({
  avatarIsLink = true,
  children,
  smallAvatar = false,
  nameOnly = false,
  headlineComponent = "h2",
  user,
  titleIsLink = false,
}: UserSummaryProps) {
  const classes = useStyles();
 
  const title = (
    <Typography
      component={headlineComponent}
      variant="h2"
      className={classes.title}
      noWrap={nameOnly}
    >
      {!user ? (
        <Skeleton
          className={classes.titleSkeleton}
          data-testid={USER_TITLE_SKELETON_TEST_ID}
        />
      ) : nameOnly ? (
        user.name
      ) : (
        `${user.name}, ${user.age}`
      )}
    </Typography>
  );
 
  const avatarClassNames = classNames(
    classes.avatar,
    smallAvatar ? classes.avatarSmall : classes.avatarBig
  );
 
  return (
    <div className={classes.root}>
      <ListItemAvatar>
        {!user ? (
          <Skeleton variant="circle" className={avatarClassNames} />
        ) : (
          <Avatar
            user={user}
            className={avatarClassNames}
            isProfileLink={avatarIsLink}
          />
        )}
      </ListItemAvatar>
      <ListItemText
        className={classes.titleAndBarContainer}
        disableTypography
        primary={
          titleIsLink && user ? (
            <StyledLink
              href={routeToUser(user.username)}
              target="_blank"
              rel="noopener noreferrer"
              className={classes.link}
            >
              {title}
              <OpenInNewIcon className={classes.linkIcon} />
            </StyledLink>
          ) : (
            title
          )
        }
        secondary={
          <>
            {!nameOnly && (
              <Typography
                color="textSecondary"
                variant="body1"
                noWrap={nameOnly}
              >
                {!user ? <Skeleton /> : user.city}
              </Typography>
            )}
            {children}
          </>
        }
      />
    </div>
  );
}