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

100% Statements 37/37
100% Branches 7/7
100% Functions 9/9
100% Lines 31/31

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                9x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x   9x 673x 166x                           166x             166x                   9x                   40x           166x                 166x           9x           9x           9x                 9x                     9x           165x       166x   166x                       166x 164x       166x                                                                                                                                                                
import { Group } from "@mui/icons-material";
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 { localizeDateTimeRange, timestampToPlainDateTime } from "utils/date";
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: "var(--mui-palette-grey-50)",
  },
  ...(isCancelled && {
    opacity: 0.6,
    backgroundColor: "var(--mui-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;
}
 
export default function EventCard({ event, className }: EventCardProps) {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation([COMMUNITIES]);
 
  const dateTimeRangeText = localizeDateTimeRange(
    // TODO(#8064): Should use the event.timezone, but it's currently incorrect.
    timestampToPlainDateTime(event.startTime!),
    timestampToPlainDateTime(event.endTime!),
    {
      locale,
      includeDayOfWeek: true,
      abbreviate: true,
      capitalize: true,
    },
  );
 
  const strippedContent = useMemo(
    () => stripMarkdown(event.content),
    [event.content],
  );
 
  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: "var(--mui-palette-grey-200)",
            height: { xs: 80, sm: 100, md: 120 },
            backgroundImage: `url(${eventImageSrc})`,
            backgroundSize:
              eventImageSrc === eventImagePlaceholderUrl ? "contain" : "cover",
            backgroundRepeat: "no-repeat",
            backgroundPosition: "center",
          }}
        >
          <FlagButtonWrapper>
            <FlagButton
              contentRef={`event/${event.eventId}`}
              authorUser={event.creatorUserId}
            />
          </FlagButtonWrapper>
        </CardMedia>
        <StyledCardContent>
          <EventTime
            variant="body2"
            color="textSecondary"
            gutterBottom
            title={dateTimeRangeText}
          >
            {dateTimeRangeText}
          </EventTime>
          <Title variant="h3" gutterBottom>
            {event.title}
          </Title>
          <Typography
            noWrap
            variant="body2"
            gutterBottom
            sx={{
              maxWidth: "25em",
            }}
          >
            {event.location?.address}
          </Typography>
          {event.isCancelled && (
            <CancelledChip label={t("communities:cancelled")} />
          )}
          <Divider spacing={1} />
          <ContentWrapper>
            <Content>{strippedContent}</Content>
 
            <ActivityStatsWrapper>
              <Typography
                variant="body2"
                color="textSecondary"
                sx={{ display: "flex", alignItems: "center" }}
              >
                <Group fontSize="small" sx={{ marginRight: "0.25rem" }} />
                {event.goingCount}
              </Typography>
              <StyledCommentsCount variant="body2">
                {t("communities:comments_count", {
                  count: event.thread?.numResponses,
                })}
              </StyledCommentsCount>
            </ActivityStatsWrapper>
          </ContentWrapper>
        </StyledCardContent>
      </StyledLink>
    </StyledCard>
  );
}