All files / app/features/communities/events EventCard.tsx

100% Statements 41/41
100% Branches 14/14
100% Functions 10/10
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 233              9x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x 9x   9x 745x 184x                           184x             184x                   9x                   41x           184x                 184x           9x           9x           9x                 9x                     9x               170x         184x   184x 184x   184x 178x       184x       184x                           184x                                                                                                                                                    
import {
  Card,
  CardContent,
  CardMedia,
  Chip,
  styled,
  Typography,
} from "@mui/material";
import { eventImagePlaceholderUrl } from "appConstants";
import Divider from "components/Divider";
import FlagButton from "features/FlagButton";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import Link from "next/link";
import { Event } from "proto/events_pb";
import { useMemo } from "react";
import { routeToEvent } from "routes";
import { timestamp2Date } from "utils/date";
import dayjs from "utils/dayjs";
import stripMarkdown from "utils/stripMarkdown";
 
const StyledCard = styled(Card, {
  shouldForwardProp: (prop) => prop !== "isCancelled",
})<{ isCancelled?: boolean }>(({ theme, isCancelled }) => ({
  position: "relative",
  display: "grid",
  gridTemplateRows: "1fr auto",
  overflow: "hidden",
  "&:hover": {
    backgroundColor: theme.palette.grey[50],
  },
  ...(isCancelled && {
    opacity: 0.6,
    backgroundColor: theme.palette.grey[200],
  }),
}));
 
const Title = styled(Typography)(({ theme }) => ({
  display: "-webkit-box",
  WebkitBoxOrient: "vertical",
  WebkitLineClamp: 2,
  overflow: "hidden",
}));
 
const EventTime = styled(Typography)(({ theme }) => ({
  display: "-webkit-box",
  WebkitBoxOrient: "vertical",
  WebkitLineClamp: 2,
  overflow: "hidden",
  [theme.breakpoints.up("sm")]: {
    WebkitLineClamp: 1,
  },
}));
 
const Content = styled(Typography)({
  display: "-webkit-box",
  WebkitBoxOrient: "vertical",
  WebkitLineClamp: 3,
  overflow: "hidden",
  textOverflow: "ellipsis",
  wordBreak: "break-word",
  maxHeight: "4.5em",
});
 
const CancelledChip = styled(Chip)(({ theme }) => ({
  backgroundColor: theme.palette.error.main,
  color: theme.palette.common.white,
  fontWeight: "bold",
}));
 
const FlagButtonWrapper = styled("div")(({ theme }) => ({
  position: "absolute",
  top: theme.spacing(1),
  right: theme.spacing(1),
  "& svg": {
    fontSize: 16,
  },
}));
 
const StyledCommentsCount = styled(Typography)(({ theme }) => ({
  alignSelf: "flex-end",
  flexShrink: 0,
  color: theme.palette.primary.main,
}));
 
const ContentWrapper = styled("div")({
  display: "flex",
  flexDirection: "column",
  flexGrow: 1,
});
 
const ActivityStatsWrapper = styled("div")({
  display: "flex",
  justifyContent: "space-between",
  marginTop: "auto",
});
 
const StyledLink = styled(Link)({
  textDecoration: "none",
  color: "inherit",
  display: "flex",
  flexDirection: "column",
  height: "100%",
  overflow: "hidden",
});
 
const StyledCardContent = styled(CardContent)({
  display: "flex",
  flexDirection: "column",
  flexGrow: 1,
  height: 240,
  overflow: "hidden",
  "&:last-child": {
    paddingBottom: 16, // Override MUI's default last-child padding
  },
});
 
export const EVENT_CARD_TEST_ID = "event-card";
interface EventCardProps {
  event: Event.AsObject;
  className?: string;
  // Optional formatter to override attendees count text (used by dashboard to avoid loading COMMUNITIES)
  attendeesCountFormatter?: (count: number) => string;
}
 
export default function EventCard({
  event,
  className,
  attendeesCountFormatter,
}: EventCardProps) {
  const { t } = useTranslation([COMMUNITIES]);
 
  const startTime = dayjs(timestamp2Date(event.startTime!));
  const endTime = dayjs(timestamp2Date(event.endTime!));
 
  const strippedContent = useMemo(
    () => stripMarkdown(event.content),
    [event.content],
  );
 
  const formattedEventDates = `${startTime.format("llll")} - ${endTime.format(
    endTime.isSame(startTime, "day") ? "LT" : "llll",
  )}`;
 
  const eventImageSrc = event.photoUrl || eventImagePlaceholderUrl;
 
  return (
    <StyledCard
      className={className}
      isCancelled={event.isCancelled}
      data-testid={EVENT_CARD_TEST_ID}
    >
      <StyledLink href={routeToEvent(event.eventId, event.slug)}>
        <CardMedia
          component="div"
          sx={{
            position: "relative",
            padding: 1,
            backgroundColor: (theme) => theme.palette.grey[200],
            height: { xs: 80, sm: 100, md: 120 },
            backgroundImage: `url(${eventImageSrc})`,
            backgroundSize:
              eventImageSrc === eventImagePlaceholderUrl ? "contain" : "cover",
            backgroundRepeat: "no-repeat",
            backgroundPosition: "center",
          }}
        >
          {event.onlineInformation && (
            <Chip
              size="medium"
              label={t("communities:online")}
              sx={{ borderRadius: 1, fontWeight: "bold" }}
            />
          )}
          <FlagButtonWrapper>
            <FlagButton
              contentRef={`event/${event.eventId}`}
              authorUser={event.creatorUserId}
            />
          </FlagButtonWrapper>
        </CardMedia>
        <StyledCardContent>
          <EventTime
            variant="body2"
            color="textSecondary"
            gutterBottom
            title={formattedEventDates}
          >
            {formattedEventDates}
          </EventTime>
          <Title variant="h3" gutterBottom>
            {event.title}
          </Title>
          <Typography
            noWrap
            variant="body2"
            gutterBottom
            sx={{
              maxWidth: "25em",
            }}
          >
            {event.offlineInformation
              ? event.offlineInformation.address
              : t("communities:virtual_event_location_placeholder")}
          </Typography>
          {event.isCancelled && (
            <CancelledChip label={t("communities:cancelled")} />
          )}
          <Divider spacing={1} />
          <ContentWrapper>
            <Content>{strippedContent}</Content>
 
            <ActivityStatsWrapper>
              <Typography variant="body2" color="textSecondary">
                {attendeesCountFormatter
                  ? attendeesCountFormatter(event.goingCount + event.maybeCount)
                  : t("communities:attendees_count", {
                      count: event.goingCount + event.maybeCount,
                    })}
              </Typography>
              <StyledCommentsCount variant="body2">
                {t("communities:comments_count", {
                  count: event.thread?.numResponses,
                })}
              </StyledCommentsCount>
            </ActivityStatsWrapper>
          </ContentWrapper>
        </StyledCardContent>
      </StyledLink>
    </StyledCard>
  );
}