Files
Checkmate/client/src/Components/v2/design-elements/BasePage.tsx
T

209 lines
3.8 KiB
TypeScript
Raw Normal View History

2026-01-23 19:58:37 +00:00
import Stack from "@mui/material/Stack";
2026-01-26 20:27:11 +00:00
import Alert from "@mui/material/Alert";
import Link from "@mui/material/Link";
2026-01-23 19:58:37 +00:00
import {
ErrorFallback,
EmptyFallback,
EmptyMonitorFallback,
2026-01-26 20:27:11 +00:00
BaseFallback,
2026-01-23 19:58:37 +00:00
} from "@/Components/v2/design-elements/Fallback";
import { Breadcrumb } from "@/Components/v2/design-elements/Breadcrumb";
import CircularProgress from "@mui/material/CircularProgress";
import type { StackProps } from "@mui/material/Stack";
import { useTheme } from "@mui/material/styles";
2026-01-26 20:47:10 +00:00
import { Trans, useTranslation } from "react-i18next";
2026-01-26 20:27:11 +00:00
import { Link as RouterLink } from "react-router-dom";
2026-01-26 20:47:10 +00:00
import { Typography } from "@mui/material";
2026-01-26 20:27:11 +00:00
export const PageSpeedKeyPriorityFallback = () => {
return (
<BaseFallback>
<Alert
severity="warning"
sx={{
width: "100%",
maxWidth: 600,
}}
>
2026-01-26 20:47:10 +00:00
<Typography>
<Trans
i18nKey="common.alerts.pageSpeedApiKey.content"
components={{
settingsLink: (
<Link
component={RouterLink}
to="/settings"
color="inherit"
fontWeight="inherit"
/>
),
}}
/>
</Typography>
2026-01-26 20:27:11 +00:00
</Alert>
</BaseFallback>
);
};
2026-01-23 19:58:37 +00:00
interface BasePageProps extends StackProps {
loading?: boolean;
error?: boolean;
children: React.ReactNode;
breadcrumbOverride?: string[];
}
export const BasePage = ({
loading,
error,
children,
breadcrumbOverride,
...props
}: BasePageProps) => {
const theme = useTheme();
if (loading) {
return (
<Stack
alignItems="center"
justifyContent="center"
sx={{ height: "100%" }}
>
<CircularProgress
color="primary"
size={28}
/>
</Stack>
);
}
if (error) {
return (
<ErrorFallback
title="Something went wrong..."
subtitle="Please try again later"
/>
);
}
return (
<Stack
spacing={theme.spacing(10)}
{...props}
>
2026-02-02 21:51:06 +00:00
<Breadcrumb breadcrumbOverride={breadcrumbOverride} />
2026-01-23 19:58:37 +00:00
{children}
</Stack>
);
};
interface BasePageWithStatesProps extends StackProps {
loading: boolean;
error: any;
items: any[];
bullets: string[] | unknown;
page: string;
actionButtonText: string;
actionLink: string;
children: React.ReactNode;
}
export const BasePageWithStates = ({
loading,
error,
items,
page,
bullets,
actionButtonText,
actionLink,
children,
...props
}: BasePageWithStatesProps) => {
const showLoading = loading && (!items || items.length === 0);
2026-02-04 19:30:41 +00:00
if (!loading && isEmpty(items)) {
2026-01-23 19:58:37 +00:00
return (
<EmptyFallback
bullets={bullets}
title={page}
actionButtonText={actionButtonText}
actionLink={actionLink}
/>
);
}
return (
<BasePage
loading={showLoading}
error={error}
{...props}
>
{children}
</BasePage>
);
};
interface MonitorBasePageWithStatesProps extends StackProps {
loading: boolean;
error: any;
items: any[];
page: string;
actionLink?: string;
children: React.ReactNode;
2026-01-26 20:27:11 +00:00
priorityFallback?: React.ReactNode;
2026-01-23 19:58:37 +00:00
}
const isEmpty = (items: any[]) => {
if (!items) return true;
if (Array.isArray(items) && items.length === 0) return true;
return false;
};
export const MonitorBasePageWithStates = ({
loading,
error,
items,
page,
actionLink,
children,
2026-01-26 20:27:11 +00:00
priorityFallback,
2026-01-23 19:58:37 +00:00
...props
}: MonitorBasePageWithStatesProps) => {
const { t } = useTranslation();
const showLoading = loading && (!items || items.length === 0);
2026-01-26 20:27:11 +00:00
if (priorityFallback) {
return (
<BasePage
2026-01-26 20:47:10 +00:00
loading={loading}
error={error}
2026-01-26 20:27:11 +00:00
{...props}
>
{priorityFallback}
</BasePage>
);
}
2026-01-23 19:58:37 +00:00
if (!loading && isEmpty(items)) {
return (
<EmptyMonitorFallback
page={page}
title={t(`pages.${page}.fallback.title`)}
2026-01-23 12:33:40 -08:00
bullets={t(`pages.${page}.fallback.checks`, { returnObjects: true })}
actionButtonText={t(`pages.${page}.fallback.actionButton`)}
2026-01-23 19:58:37 +00:00
actionLink={actionLink || ""}
/>
);
}
return (
<BasePage
loading={showLoading}
error={error}
{...props}
>
{children}
</BasePage>
);
};