All files / app/components UserSummary.tsx

100% Statements 37/37
82.92% Branches 34/41
100% Functions 13/13
100% Lines 35/35

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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 23321x 21x 21x 21x 21x 21x     21x 21x   21x       336x     21x 21x   21x                 21x 2737x 679x                   138x       21x 707x 343x             21x 1029x 336x             23x                         673x                   679x 679x   679x 5x     679x 5x     679x     679x                                                                                                                                                                               321x                       679x                                                              
import { Box, ListItemAvatar, ListItemText, Skeleton, Tooltip, Typography } from "@mui/material";
import { styled } from "@mui/system";
import Avatar from "components/Avatar";
import EllipsisMenu, { EllipsisMenuItem } from "components/EllipsisMenu";
import { PinIcon } from "components/Icons";
import ProfileLink from "components/ProfileLink/ProfileLink";
import { LiteUser } from "proto/api_pb";
import { BlockedUser } from "proto/blocking_pb";
import React, { useState } from "react";
import useIsScreenSizeOrSmaller from "utils/useIsScreenSizeOrSmaller";
 
import StrongVerificationBadge from "./StrongVerificationBadge";
 
// It could be BlockedUser.AsObject or LiteUser.AsObject and only LiteUser has hasStrongVerification
function isLiteUser(user: LiteUser.AsObject | BlockedUser.AsObject): user is LiteUser.AsObject {
  return "hasStrongVerification" in user;
}
 
const AVATAR_SIZE_SMALL = "3rem";
const AVATAR_SIZE_LARGE = "4.5rem";
 
const StyledWrapper = styled("div")({
  display: "flex",
  padding: 0,
  width: "100%",
  minWidth: 0,
  alignItems: "center",
  wordBreak: "break-word",
});
 
const StyledListItemText = styled(ListItemText, {
  shouldForwardProp: (prop) => prop !== "isSmallAvatar",
})<{ isSmallAvatar: boolean }>(({ theme, isSmallAvatar }) => ({
  display: "grid",
  gridTemplateColumns: "minmax(0, 1fr)",
  gap: theme.spacing(0.25),
  margin: 0,
  minHeight: isSmallAvatar ? theme.spacing(6) : theme.spacing(9),
  [theme.breakpoints.down("md")]: { minHeight: theme.spacing(6) },
}));
 
// The row is centred, but on mobile the ellipsis sits at the top right of the item.
const StyledMenuWrapper = styled("div")(({ theme }) => ({
  [theme.breakpoints.down("md")]: { alignSelf: "flex-start" },
}));
 
const StyledSkeleton = styled(Skeleton, {
  shouldForwardProp: (prop) => prop !== "isSmallAvatar",
})<{ isSmallAvatar: boolean }>(({ theme, isSmallAvatar }) => ({
  marginInlineEnd: theme.spacing(2),
  height: isSmallAvatar ? AVATAR_SIZE_SMALL : AVATAR_SIZE_LARGE,
  width: isSmallAvatar ? AVATAR_SIZE_SMALL : AVATAR_SIZE_LARGE,
  [theme.breakpoints.down("md")]: { height: AVATAR_SIZE_SMALL, width: AVATAR_SIZE_SMALL },
}));
 
const StyledAvatar = styled(Avatar, {
  shouldForwardProp: (prop) => prop !== "isSmallAvatar",
})<{ isSmallAvatar: boolean }>(({ theme, isSmallAvatar }) => ({
  marginInlineEnd: theme.spacing(2),
  height: isSmallAvatar ? AVATAR_SIZE_SMALL : AVATAR_SIZE_LARGE,
  width: isSmallAvatar ? AVATAR_SIZE_SMALL : AVATAR_SIZE_LARGE,
  [theme.breakpoints.down("md")]: { height: AVATAR_SIZE_SMALL, width: AVATAR_SIZE_SMALL },
}));
 
export const USER_TITLE_SKELETON_TEST_ID = "user-title-skeleton";
 
interface UserSummaryProps {
  children?: React.ReactNode;
  smallAvatar?: boolean;
  nameOnly?: boolean;
  headlineComponent?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
  user?: LiteUser.AsObject | BlockedUser.AsObject;
  titleIsLink?: boolean;
  isProfileLink?: boolean;
  menuItems?: EllipsisMenuItem[];
}
 
export default function UserSummary({
  children,
  smallAvatar = false,
  nameOnly = false,
  headlineComponent = "h2",
  user,
  titleIsLink = false,
  isProfileLink = true,
  menuItems,
}: UserSummaryProps) {
  const isMobile = useIsScreenSizeOrSmaller("mobile");
  const [menuAnchorEl, setMenuAnchorEl] = useState<HTMLButtonElement | null>(null);
 
  const handleMenuOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
    setMenuAnchorEl(event.currentTarget);
  };
 
  const handleMenuClose = () => {
    setMenuAnchorEl(null);
  };
 
  const nameValue = user?.name ?? "";
 
  const cityValue =
    user && "city" in user && typeof user.city === "string"
      ? user.city.length > 120
        ? user.city.slice(0, 120) + "..."
        : user.city
      : "";
 
  const title = (
    <Tooltip title={user?.name} arrow placement="top">
      <Typography component={headlineComponent} variant="h2" noWrap={nameOnly} sx={{ marginTop: "auto", minWidth: 0 }}>
        {!user ? (
          <Skeleton data-testid={USER_TITLE_SKELETON_TEST_ID} sx={{ maxWidth: 300 }} />
        ) : (
          <Box
            sx={{
              display: "flex",
              alignItems: "center",
              gap: 0.5,
              minWidth: 0,
            }}
          >
            <Box
              component="span"
              sx={{
                minWidth: 0,
                overflow: "hidden",
                textOverflow: "ellipsis",
                whiteSpace: "nowrap",
              }}
            >
              {nameValue}
            </Box>
            {isLiteUser(user) && user.hasStrongVerification && (
              <Box component="span" sx={{ flexShrink: 0 }}>
                <StrongVerificationBadge />
              </Box>
            )}
          </Box>
        )}
      </Typography>
    </Tooltip>
  );
 
  return (
    <StyledWrapper>
      <ListItemAvatar>
        {!user ? (
          <StyledSkeleton variant="circular" isSmallAvatar={smallAvatar} />
        ) : (
          <StyledAvatar user={user} isProfileLink={isProfileLink} isSmallAvatar={smallAvatar} />
        )}
      </ListItemAvatar>
      <StyledListItemText
        disableTypography
        isSmallAvatar={smallAvatar}
        primary={
          titleIsLink && user ? (
            <ProfileLink
              userId={"userId" in user ? user.userId : undefined}
              username={user.username}
              openInNewTab={!isMobile}
              showOpenIcon={!isMobile}
              style={{
                display: "flex",
                alignItems: "center",
                justifyContent: "flex-start",
                minWidth: 0,
              }}
            >
              {title}
            </ProfileLink>
          ) : (
            title
          )
        }
        secondary={
          <>
            {!nameOnly && (
              <Tooltip title={(user as LiteUser.AsObject)?.city} arrow placement="top">
                <Box
                  sx={{
                    display: "flex",
                    alignItems: "center",
                    gap: 0.5,
                    minWidth: 0,
                  }}
                >
                  {user && cityValue && (
                    <PinIcon
                      sx={(theme) => ({
                        flexShrink: 0,
                        color: "var(--mui-palette-text-secondary)",
                        fontSize: "1.25rem",
                        [theme.breakpoints.down("md")]: { fontSize: "1rem" },
                      })}
                    />
                  )}
                  <Typography
                    color="textSecondary"
                    variant="body1"
                    noWrap
                    sx={(theme) => ({
                      minWidth: 0,
                      fontSize: "1rem",
                      [theme.breakpoints.down("md")]: { fontSize: "0.875rem" },
                    })}
                  >
                    {!user ? <Skeleton /> : cityValue}
                  </Typography>
                </Box>
              </Tooltip>
            )}
            {children}
          </>
        }
      />
 
      {menuItems?.length ? (
        <StyledMenuWrapper>
          <EllipsisMenu
            idName={`${user?.username}-summary-menu`}
            isMenuOpen={!!menuAnchorEl}
            menuAnchorEl={menuAnchorEl}
            onMenuOpen={handleMenuOpen}
            onMenuClose={handleMenuClose}
            items={menuItems}
          />
        </StyledMenuWrapper>
      ) : null}
    </StyledWrapper>
  );
}