All files / app/features/connections/friends FriendTile.tsx

100% Statements 12/12
100% Branches 6/6
100% Functions 3/3
100% Lines 9/9

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 673x 3x 3x 3x 3x 3x                         30x             30x                                                                               30x  
import { Card, CircularProgress, styled, Typography } from "@mui/material";
import Alert from "components/Alert";
import NotificationBadge from "components/NotificationBadge";
import TextBody from "components/TextBody";
import React from "react";
import { theme } from "theme";
 
interface FriendTileProps {
  children: React.ReactNode;
  /** Shown as a badge beside the title. Nothing renders when it's 0 or absent. */
  count?: number;
  errorMessage: string | null;
  hasData: boolean;
  isLoading: boolean;
  noDataMessage: string;
  title: string;
}
 
const StyledContainer = styled("div")(({ theme }) => ({
  margin: theme.spacing(2),
  "& > *": {
    marginBottom: theme.spacing(2),
  },
}));
 
const StyledHeader = styled(Typography)(({ theme }) => ({
  fontWeight: theme.typography.fontWeightBold,
  marginBottom: theme.spacing(2),
  marginLeft: theme.spacing(1),
  // Sit the count inline after the title and vertically centred, instead of
  // overlapping its top-right corner the way an overlay badge does.
  "& .MuiBadge-root": {
    alignItems: "center",
  },
  "& .MuiBadge-badge": {
    position: "static",
    transform: "none",
    marginInlineStart: theme.spacing(1),
  },
}));
 
function FriendTile({ children, count, errorMessage, hasData, isLoading, noDataMessage, title }: FriendTileProps) {
  return (
    <Card>
      <StyledContainer>
        <StyledHeader variant="h2">
          {count ? <NotificationBadge count={count}>{title}</NotificationBadge> : title}
        </StyledHeader>
        {errorMessage ? (
          <Alert severity="error" sx={{ borderRadius: 0 }}>
            {errorMessage}
          </Alert>
        ) : null}
        {isLoading ? (
          <CircularProgress sx={{ display: "block", margin: `0 auto ${theme.spacing(1)}` }} />
        ) : hasData ? (
          children
        ) : (
          <TextBody sx={{ marginLeft: theme.spacing(1) }}>{noDataMessage}</TextBody>
        )}
      </StyledContainer>
    </Card>
  );
}
 
export default FriendTile;