All files / app/components/ErrorFallback ErrorFallback.tsx

68.18% Statements 15/22
0% Branches 0/4
0% Functions 0/5
83.33% Lines 15/18

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 801x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x                 1x                               1x         1x               4x                                                        
import { styled, Typography } from "@mui/material";
import Button from "components/Button";
import HtmlMeta from "components/HtmlMeta";
import { CouchersIcon } from "components/Icons";
import ReportButton from "components/Navigation/ReportButton";
import { useTranslation } from "i18n";
import { GLOBAL } from "i18n/namespaces";
import Link from "next/link";
import { useRouter } from "next/router";
import { baseRoute } from "routes";
 
// An error boundary replaces everything it wraps, including the navigation and
// the usual page container, so this screen has to lay itself out.
const StyledRoot = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  // See the --vh workaround in pages/_app.tsx for why this isn't 100vh.
  minHeight: "calc(var(--vh, 1vh) * 100)",
  padding: theme.spacing(4, 2),
}));
 
const StyledPanel = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  alignItems: "center",
  textAlign: "center",
  gap: theme.spacing(2),
  width: "100%",
  maxWidth: "32rem",
  padding: theme.spacing(5, 3),
  backgroundColor: "var(--mui-palette-background-paper)",
  // In dark mode `paper` and `default` are the same colour, so the border is
  // what separates the panel from the page behind it.
  border: "1px solid var(--mui-palette-divider)",
  borderRadius: theme.shape.borderRadius * 2,
}));
 
const StyledMark = styled(CouchersIcon)({
  fontSize: "3rem",
  color: "var(--mui-palette-secondary-main)",
});
 
const StyledActions = styled("div")(({ theme }) => ({
  display: "flex",
  flexWrap: "wrap",
  justifyContent: "center",
  gap: theme.spacing(1),
  marginTop: theme.spacing(1),
}));
 
export default function ErrorFallback({ isFatal }: { isFatal?: boolean }) {
  const { t } = useTranslation(GLOBAL);
  const router = useRouter();
 
  const handleRefresh = () => router.reload();
 
  return (
    <StyledRoot>
      <HtmlMeta title={t("error.fallback.title")} />
      <StyledPanel>
        <StyledMark />
        <Typography variant="h1">{t("error.fallback.title")}</Typography>
        <Typography variant="body1" sx={{ color: "text.secondary" }}>
          {isFatal ? t("error.fatal_message") : t("error.fallback.subtitle")}
        </Typography>
        {!isFatal && <ReportButton isResponsive={false} />}
        <StyledActions>
          {!isFatal && (
            <Button variant="outlined" component={Link} href={baseRoute}>
              {t("error.fallback.home_page_link_label")}
            </Button>
          )}
          <Button onClick={handleRefresh}>{t("error.fallback.refresh_page_button_label")}</Button>
        </StyledActions>
      </StyledPanel>
    </StyledRoot>
  );
}