All files / app/features/dashboard EventListRow.tsx

100% Statements 38/38
85.71% Branches 24/28
100% Functions 3/3
100% Lines 36/36

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  3x 3x 3x 3x 3x   3x 3x 3x   12x           3x                               3x                                   12x                       3x               3x         3x             3x                   3x                   3x                       3x           3x                       3x                 24x                               12x       12x   12x 12x 12x   12x         12x 12x 12x 12x 12x 12x       12x   12x                                                                              
import { ChevronRight, Group, Place, Schedule } from "@mui/icons-material";
import { Skeleton, styled } from "@mui/material";
import { useTranslation } from "i18n";
import { localizeMonthName, localizeTimeOnly } from "i18n/datetimes";
import { DASHBOARD } from "i18n/namespaces";
import Link from "next/link";
import { Event } from "proto/events_pb";
import { routeToEvent } from "routes";
import { Temporal } from "temporal-polyfill";
import { timestampToZonedDateTime } from "utils/date";
 
export const EventListContainer = styled("div")({
  border: "1px solid var(--mui-palette-grey-300)",
  borderRadius: "8px",
  overflow: "hidden",
});
 
const RowLink = styled(Link)({
  display: "flex",
  alignItems: "center",
  gap: "14px",
  padding: "10px 14px",
  textDecoration: "none",
  color: "inherit",
  borderBottom: "1px solid var(--mui-palette-divider)",
  "&:last-child": {
    borderBottom: "none",
  },
  "&:hover": {
    backgroundColor: "var(--mui-palette-grey-50)",
  },
});
 
const DateChip = styled("div")({
  width: 40,
  flexShrink: 0,
  border: "1px solid var(--mui-palette-grey-300)",
  borderRadius: "5px",
  padding: "4px 0",
  textAlign: "center",
  backgroundColor: "var(--mui-palette-background-paper)",
  "&[data-today]": {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "center",
    minHeight: 38,
    overflow: "hidden",
  },
});
 
const DateMonth = styled("div")<{ labelFontSize?: number }>(({ labelFontSize }) => ({
  fontSize: labelFontSize ? `${labelFontSize}px` : "9px",
  textTransform: "uppercase",
  letterSpacing: "0.08em",
  color: "var(--mui-palette-secondary-main)",
  fontWeight: 700,
  lineHeight: 1.2,
  "[data-today] &": {
    color: "var(--mui-palette-primary-main)",
  },
}));
 
const DateDay = styled("div")({
  fontSize: "16px",
  fontWeight: 700,
  lineHeight: 1,
  marginTop: "2px",
  color: "var(--mui-palette-text-primary)",
});
 
const ContentWrapper = styled("div")({
  flex: 1,
  minWidth: 0,
});
 
const TitleRow = styled("div")({
  display: "flex",
  alignItems: "center",
  gap: "8px",
  minWidth: 0,
});
 
const RowTitle = styled("div")({
  fontSize: "14px",
  fontWeight: 600,
  overflow: "hidden",
  textOverflow: "ellipsis",
  whiteSpace: "nowrap",
  flex: 1,
  minWidth: 0,
});
 
const MetaLine = styled("div")({
  display: "flex",
  alignItems: "center",
  gap: "10px",
  marginTop: "3px",
  fontSize: "12px",
  color: "var(--mui-palette-text-secondary)",
  overflow: "hidden",
});
 
const MetaItem = styled("div")({
  display: "flex",
  alignItems: "center",
  gap: "3px",
  overflow: "hidden",
  flexShrink: 0,
  "&:last-child": {
    flexShrink: 1,
    overflow: "hidden",
  },
});
 
const MetaText = styled("span")({
  overflow: "hidden",
  textOverflow: "ellipsis",
  whiteSpace: "nowrap",
});
 
const AttendeeTag = styled("span")({
  display: "inline-flex",
  alignItems: "center",
  gap: "3px",
  color: "var(--mui-palette-text-secondary)",
  fontSize: "11px",
  fontWeight: 600,
  padding: "2px 8px",
  flexShrink: 0,
  whiteSpace: "nowrap",
});
 
const SkeletonRow = styled("div")({
  display: "flex",
  alignItems: "center",
  gap: "14px",
  padding: "10px 14px",
  borderBottom: "1px solid var(--mui-palette-divider)",
  "&:last-child": { borderBottom: "none" },
});
 
export function EventListRowSkeleton() {
  return (
    <SkeletonRow>
      <Skeleton variant="rectangular" width={40} height={52} sx={{ borderRadius: "5px", flexShrink: 0 }} />
      <div style={{ flex: 1, minWidth: 0 }}>
        <Skeleton width="60%" height={20} />
        <Skeleton width="80%" height={16} sx={{ marginTop: "4px" }} />
      </div>
    </SkeletonRow>
  );
}
 
interface EventListRowProps {
  event: Event.AsObject;
}
 
export default function EventListRow({ event }: EventListRowProps) {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation([DASHBOARD]);
 
  const now = Temporal.Now.zonedDateTimeISO();
  const startDate = timestampToZonedDateTime(event.startTime!, event.timezone);
  const endDate = event.endTime ? timestampToZonedDateTime(event.endTime, event.timezone) : null;
  const isNow =
    endDate !== null &&
    Temporal.ZonedDateTime.compare(startDate, now) <= 0 &&
    Temporal.ZonedDateTime.compare(endDate, now) >= 0;
 
  // Define "today" as: starting on the current day in the current timezone.
  const isToday = !isNow && startDate.withTimeZone(now.timeZoneId).toPlainDate().equals(now.toPlainDate());
  const todayLabel = t("dashboard:today_label");
  const nowLabel = t("dashboard:now_label");
  const chipLabel = isNow ? nowLabel : todayLabel;
  const chipFontSize = chipLabel.length <= 5 ? 9 : chipLabel.length <= 7 ? 8 : 7;
  const month = localizeMonthName(startDate.month, locale, {
    abbreviate: true,
    capitalize: true,
  });
  const day = startDate.day;
 
  const timeStr = localizeTimeOnly(startDate, locale);
 
  return (
    <RowLink href={routeToEvent(event.eventId, event.slug)}>
      <DateChip data-today={isToday || isNow || undefined}>
        <DateMonth labelFontSize={isToday || isNow ? chipFontSize : undefined}>
          {isNow ? nowLabel : isToday ? todayLabel : month}
        </DateMonth>
        {!isToday && !isNow && <DateDay>{day}</DateDay>}
      </DateChip>
      <ContentWrapper>
        <TitleRow>
          <RowTitle>{event.title}</RowTitle>
          <AttendeeTag>
            <Group sx={{ fontSize: "11px" }} />
            {event.goingCount}
          </AttendeeTag>
          <ChevronRight
            sx={{
              fontSize: "16px",
              color: "var(--mui-palette-text-secondary)",
              flexShrink: 0,
            }}
          />
        </TitleRow>
        <MetaLine>
          <MetaItem>
            <Schedule sx={{ fontSize: "12px" }} />
            <MetaText>{timeStr}</MetaText>
          </MetaItem>
          <MetaItem>
            <Place sx={{ fontSize: "12px" }} />
            <MetaText>{event.location?.address}</MetaText>
          </MetaItem>
        </MetaLine>
      </ContentWrapper>
    </RowLink>
  );
}