All files / app/features/donations DonationDriveBlock.tsx

96.55% Statements 28/29
85.71% Branches 12/14
100% Functions 4/4
96.15% Lines 25/26

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 1083x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x               17x                       17x             17x               17x 17x 17x 17x   17x   17x       17x     17x               17x                                                                          
import { useFeatureValue } from "@growthbook/growthbook-react";
import VolunteerActivismIcon from "@mui/icons-material/VolunteerActivism";
import { Alert, alpha, styled } from "@mui/material";
import useAccountInfo from "features/auth/useAccountInfo";
import { useTranslation } from "i18n";
import { localizeDateOnly } from "i18n/datetimes";
import { GLOBAL } from "i18n/namespaces";
import { localizeUSD } from "i18n/numbers";
import { Temporal } from "temporal-polyfill";
import { theme } from "theme";
import { UTC_TIMEZONE } from "utils/date";
 
import DonationProgressBar from "./DonationProgressBar";
import useDonationStats from "./useDonationStats";
 
export interface DonationDriveBlockProps {
  onClose?: () => void;
  action?: React.ReactNode;
  alwaysShow?: boolean;
}
 
const OuterWrapper = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  width: "100%",
  gap: theme.spacing(2),
  [theme.breakpoints.down("md")]: {
    flexDirection: "column",
    alignItems: "stretch",
    gap: theme.spacing(1.5),
  },
}));
 
const ContentWrapper = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  flex: 1,
  gap: theme.spacing(0.75),
}));
 
const Message = styled("span")(({ theme }) => ({
  fontSize: "0.875rem",
  color: "var(--mui-palette-text-secondary)",
  [theme.breakpoints.down("md")]: {
    textAlign: "center",
  },
}));
 
export default function DonationDriveBlock({ onClose, action, alwaysShow = false }: DonationDriveBlockProps) {
  const { t, i18n } = useTranslation(GLOBAL);
  const { data: accountInfo, isLoading } = useAccountInfo();
  const { data: donationStats } = useDonationStats();
  // epoch seconds, or 0 when the drive has no end
  const driveEnd = useFeatureValue("donation_drive_end", 0);
 
  Iif (!alwaysShow && (isLoading || !accountInfo?.shouldShowDonationBanner)) {
    return null;
  }
 
  const formattedGoal = donationStats ? localizeUSD(donationStats.goal, i18n.language) : "";
  // shown in UTC so it's the same date everywhere, matching when the drive ends
  const formattedEndDate =
    driveEnd > 0
      ? localizeDateOnly(
          Temporal.Instant.fromEpochMilliseconds(driveEnd * 1000).toZonedDateTimeISO(UTC_TIMEZONE),
          i18n.language,
          { includeYear: false },
        )
      : null;
  // languages without the end date sentence yet keep their translated sentence without it
  const hasEndDateMessage = !!i18n.getResource(i18n.language, GLOBAL, "donation_banner.message_with_end_date");
 
  return (
    <Alert
      icon={<VolunteerActivismIcon />}
      onClose={onClose}
      sx={{
        alignItems: "center",
        ".MuiAlert-message": { width: "100%", py: 0.5 },
        backgroundColor: alpha(theme.palette.secondary.main, 0.08),
        color: "var(--mui-palette-text-primary)",
        "& .MuiAlert-icon": {
          color: "var(--mui-palette-secondary-main)",
          alignSelf: "center",
        },
        [theme.breakpoints.down("md")]: {
          alignItems: "flex-start",
          "& .MuiAlert-icon": {
            paddingTop: "10px",
          },
        },
      }}
    >
      <OuterWrapper>
        <ContentWrapper>
          <DonationProgressBar />
          <Message>
            {formattedEndDate && hasEndDateMessage
              ? t("donation_banner.message_with_end_date", { goal_amount: formattedGoal, end_date: formattedEndDate })
              : t("donation_banner.message", { goal: formattedGoal })}
          </Message>
        </ContentWrapper>
        {action}
      </OuterWrapper>
    </Alert>
  );
}