All files / app/features/auth/verification VerificationSection.tsx

100% Statements 37/37
100% Branches 14/14
100% Functions 14/14
100% Lines 29/29

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 2072x 2x 2x 2x 2x             2x             144x             99x             183x   100x                         2x                   144x                       144x   144x                         144x 4x           140x 17x           123x                                           137x 137x 137x 137x                         144x                       144x                                           4x     4x                               1x     1x                            
import { alpha, Box, styled, Typography } from "@mui/material";
import Badge from "features/badges/Badge";
import { useBadges } from "features/badges/hooks";
import { useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import { ReactNode } from "react";
 
export type VerificationStatus = "verified" | "in_progress" | "code_sent" | "not_verified";
 
export type IconTint = "primary" | "primaryDark" | "secondary";
 
const TINT_COLOR: Record<IconTint, string> = {
  primary: "var(--mui-palette-primary-main)",
  primaryDark: "var(--mui-palette-primary-dark)",
  secondary: "var(--mui-palette-secondary-main)",
};
 
/** The section as a whole: heading block, then one block per flow state. */
export const SectionRoot = styled("section")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  gap: theme.spacing(2.5),
}));
 
/** A vertical run of related copy and controls inside a section. */
export const SectionBlock = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  gap: theme.spacing(1.75),
}));
 
/** Buttons plus any trailing hint text, wrapping on narrow screens. */
export const ActionRow = styled("div", { shouldForwardProp: (prop) => prop !== "align" })<{
  align?: "start" | "center";
}>(({ theme, align = "start" }) => ({
  display: "flex",
  flexWrap: "wrap",
  gap: theme.spacing(1.5),
  alignItems: "center",
  justifyContent: align === "center" ? "center" : "flex-start",
  // Once the row wraps there is no useful left edge to align to, so centre.
  [theme.breakpoints.down("sm")]: {
    justifyContent: "center",
    textAlign: "center",
  },
}));
 
const IconTile = styled("span")({
  width: 40,
  height: 40,
  flexShrink: 0,
  borderRadius: 10,
  display: "grid",
  placeItems: "center",
  "& .MuiSvgIcon-root": { fontSize: 24 },
});
 
const Pill = styled("span")(({ theme }) => ({
  marginInlineStart: "auto",
  display: "inline-flex",
  alignItems: "center",
  padding: theme.spacing(0.375, 1.25),
  borderRadius: 24,
  fontSize: "0.6875rem", // 11px, below the theme's smallest step
  fontWeight: 700,
  whiteSpace: "nowrap",
}));
 
function StatusPill({ status }: { status: VerificationStatus }) {
  const { t } = useTranslation(AUTH);
 
  const labels: Record<VerificationStatus, string> = {
    verified: t("verification_page.status_pill.verified"),
    in_progress: t("verification_page.status_pill.in_progress"),
    code_sent: t("verification_page.status_pill.code_sent"),
    not_verified: t("verification_page.status_pill.not_verified"),
  };
 
  return (
    <Pill
      sx={(theme) => {
        // alpha() needs a real colour rather than a CSS variable, so the tint is
        // computed off the light scheme; at 14% it reads as a wash in both. The
        // text colour does have to flip, or the dark green/orange goes muddy.
        if (status === "verified") {
          return {
            backgroundColor: alpha(theme.palette.success.main, 0.14),
            color: theme.palette.success.dark,
            ...theme.applyStyles("dark", { color: theme.palette.success.light }),
          };
        }
        if (status === "in_progress" || status === "code_sent") {
          return {
            backgroundColor: alpha(theme.palette.secondary.main, 0.14),
            color: theme.palette.secondary.dark,
            ...theme.applyStyles("dark", { color: theme.palette.secondary.light }),
          };
        }
        return {
          backgroundColor: "var(--mui-palette-grey-200)",
          color: "var(--mui-palette-text-secondary)",
        };
      }}
    >
      {labels[status]}
    </Pill>
  );
}
 
type VerificationSectionHeaderProps = {
  icon: ReactNode;
  iconTint: IconTint;
  title: string;
  description: string;
  status: VerificationStatus;
  // profile badge this verification earns, shown under the description
  badgeId?: string;
};
 
function EarnedBadge({ badgeId }: { badgeId: string }) {
  const { t } = useTranslation(AUTH);
  const { badges } = useBadges();
  const badge = badges?.[badgeId];
  if (!badge) return null;
 
  return (
    // the chip's own margins are for wrapping rows of badges on the profile
    <Box sx={{ display: "flex", alignItems: "center", flexWrap: "wrap", gap: 1, "& .MuiChip-root": { m: 0 } }}>
      <Typography variant="body2" sx={{ color: "var(--mui-palette-text-secondary)" }}>
        {t("verification_page.earned_badge_caption")}
      </Typography>
      <Badge badge={badge} />
    </Box>
  );
}
 
export function VerificationSectionHeader({
  icon,
  iconTint,
  title,
  description,
  status,
  badgeId,
}: VerificationSectionHeaderProps) {
  return (
    <Box sx={{ display: "flex", flexDirection: "column", gap: 1 }}>
      <Box sx={{ display: "flex", alignItems: "center", gap: 1.25 }}>
        <IconTile
          sx={(theme) => ({
            color: TINT_COLOR[iconTint],
            backgroundColor: alpha(
              iconTint === "secondary" ? theme.palette.secondary.main : theme.palette.primary.main,
              0.12,
            ),
          })}
        >
          {icon}
        </IconTile>
        <Typography variant="h2">{title}</Typography>
        <StatusPill status={status} />
      </Box>
      <Typography variant="body1" sx={{ color: "var(--mui-palette-text-secondary)", textWrap: "pretty" }}>
        {description}
      </Typography>
      {badgeId && <EarnedBadge badgeId={badgeId} />}
    </Box>
  );
}
 
/** The green "you're done" banner each section ends on. */
export function SuccessBanner({ children }: { children: ReactNode }) {
  return (
    <Box
      sx={(theme) => ({
        display: "flex",
        gap: 1.5,
        alignItems: "flex-start",
        padding: theme.spacing(1.75, 2),
        borderRadius: "8px",
        backgroundColor: alpha(theme.palette.success.main, 0.08),
        "& .MuiSvgIcon-root": { fontSize: 22, color: "var(--mui-palette-success-main)" },
      })}
    >
      {children}
    </Box>
  );
}
 
/** A neutral, grey-tinted note row (postcard in transit, data handling, ...). */
export function InfoBanner({ children }: { children: ReactNode }) {
  return (
    <Box
      sx={(theme) => ({
        display: "flex",
        gap: 1.5,
        alignItems: "flex-start",
        padding: theme.spacing(1.75, 2),
        borderRadius: "8px",
        backgroundColor: "var(--mui-palette-grey-50)",
        "& .MuiSvgIcon-root": { fontSize: 22, color: "var(--mui-palette-primary-main)" },
      })}
    >
      {children}
    </Box>
  );
}