|
| 1 | +import * as Common from "@frontend/common"; |
| 2 | +import { CircularProgress, Stack, Theme } from "@mui/material"; |
| 3 | +import { ErrorBoundary, Suspense } from "@suspensive/react"; |
| 4 | +import { AxiosError, AxiosResponse } from "axios"; |
| 5 | +import * as React from "react"; |
| 6 | +import { useLocation, useParams } from "react-router-dom"; |
| 7 | +import * as R from "remeda"; |
| 8 | + |
| 9 | +import { useAppContext } from "../../contexts/app_context"; |
| 10 | + |
| 11 | +const initialPageStyle: (additionalStyle: React.CSSProperties) => (theme: Theme) => React.CSSProperties = |
| 12 | + (additionalStyle) => (theme) => ({ |
| 13 | + width: "100%", |
| 14 | + display: "flex", |
| 15 | + justifyContent: "flex-start", |
| 16 | + alignItems: "center", |
| 17 | + flexDirection: "column", |
| 18 | + |
| 19 | + marginTop: theme.spacing(4), |
| 20 | + |
| 21 | + ...(!R.isEmpty(additionalStyle) |
| 22 | + ? additionalStyle |
| 23 | + : { |
| 24 | + [theme.breakpoints.down("lg")]: { |
| 25 | + marginTop: theme.spacing(2), |
| 26 | + }, |
| 27 | + [theme.breakpoints.down("sm")]: { |
| 28 | + marginTop: theme.spacing(1), |
| 29 | + }, |
| 30 | + }), |
| 31 | + }); |
| 32 | + |
| 33 | +const initialSectionStyle: (additionalStyle: React.CSSProperties) => (theme: Theme) => React.CSSProperties = |
| 34 | + (additionalStyle) => (theme) => ({ |
| 35 | + width: "100%", |
| 36 | + maxWidth: "1200px", |
| 37 | + display: "flex", |
| 38 | + justifyContent: "flex-start", |
| 39 | + alignItems: "center", |
| 40 | + paddingRight: theme.spacing(16), |
| 41 | + paddingLeft: theme.spacing(16), |
| 42 | + |
| 43 | + "& .markdown-body": { width: "100%" }, |
| 44 | + ...(!R.isEmpty(additionalStyle) |
| 45 | + ? additionalStyle |
| 46 | + : { |
| 47 | + [theme.breakpoints.down("lg")]: { |
| 48 | + paddingRight: theme.spacing(4), |
| 49 | + paddingLeft: theme.spacing(4), |
| 50 | + }, |
| 51 | + [theme.breakpoints.down("sm")]: { |
| 52 | + paddingRight: theme.spacing(2), |
| 53 | + paddingLeft: theme.spacing(2), |
| 54 | + }, |
| 55 | + }), |
| 56 | + }); |
| 57 | + |
| 58 | +const LoginRequired: React.FC = () => <>401 Login Required</>; |
| 59 | +const PermissionDenied: React.FC = () => <>403 Permission Denied</>; |
| 60 | +const PageNotFound: React.FC = () => <>404 Not Found</>; |
| 61 | +const CenteredLoadingPage: React.FC = () => ( |
| 62 | + <Common.Components.CenteredPage> |
| 63 | + <CircularProgress /> |
| 64 | + </Common.Components.CenteredPage> |
| 65 | +); |
| 66 | + |
| 67 | +const throwPageNotFound: (message: string) => never = (message) => { |
| 68 | + const errorStr = `RouteRenderer: ${message}`; |
| 69 | + const axiosError = new AxiosError(errorStr, errorStr, undefined, undefined, { |
| 70 | + status: 404, |
| 71 | + } as AxiosResponse); |
| 72 | + throw new Common.BackendAPIs.BackendAPIClientError(axiosError); |
| 73 | +}; |
| 74 | + |
| 75 | +const RouteErrorFallback: React.FC<{ error: Error; reset: () => void }> = ({ error, reset }) => { |
| 76 | + if (error instanceof Common.BackendAPIs.BackendAPIClientError) { |
| 77 | + switch (error.status) { |
| 78 | + case 401: |
| 79 | + return <LoginRequired />; |
| 80 | + case 403: |
| 81 | + return <PermissionDenied />; |
| 82 | + case 404: |
| 83 | + return <PageNotFound />; |
| 84 | + default: |
| 85 | + return <Common.Components.ErrorFallback error={error} reset={reset} />; |
| 86 | + } |
| 87 | + } |
| 88 | + return <Common.Components.ErrorFallback error={error} reset={reset} />; |
| 89 | +}; |
| 90 | + |
| 91 | +export const PageRenderer: React.FC<{ id: string }> = ErrorBoundary.with( |
| 92 | + { fallback: RouteErrorFallback }, |
| 93 | + Suspense.with({ fallback: <CenteredLoadingPage /> }, ({ id }) => { |
| 94 | + const { setAppContext } = useAppContext(); |
| 95 | + const backendClient = Common.Hooks.BackendAPI.useBackendClient(); |
| 96 | + const { data } = Common.Hooks.BackendAPI.usePageQuery(backendClient, id); |
| 97 | + |
| 98 | + React.useEffect(() => { |
| 99 | + setAppContext((prev) => ({ |
| 100 | + ...prev, |
| 101 | + title: data.title, |
| 102 | + shouldShowTitleBanner: data.show_top_title_banner, |
| 103 | + shouldShowSponsorBanner: data.show_bottom_sponsor_banner, |
| 104 | + })); |
| 105 | + }, [data, setAppContext]); |
| 106 | + |
| 107 | + return ( |
| 108 | + <Stack sx={initialPageStyle(Common.Utils.parseCss(data.css))}> |
| 109 | + {data.sections.map((s) => ( |
| 110 | + <Stack sx={initialSectionStyle(Common.Utils.parseCss(s.css))} key={s.id}> |
| 111 | + <Common.Components.MDXRenderer text={s.body} /> |
| 112 | + </Stack> |
| 113 | + ))} |
| 114 | + </Stack> |
| 115 | + ); |
| 116 | + }) |
| 117 | +); |
| 118 | + |
| 119 | +export const RouteRenderer: React.FC = ErrorBoundary.with( |
| 120 | + { fallback: RouteErrorFallback }, |
| 121 | + Suspense.with({ fallback: <CenteredLoadingPage /> }, () => { |
| 122 | + const location = useLocation(); |
| 123 | + const { siteMapNode, currentSiteMapDepth } = useAppContext(); |
| 124 | + |
| 125 | + if (!siteMapNode) return <CenteredLoadingPage />; |
| 126 | + |
| 127 | + const routeInfo = !R.isEmpty(currentSiteMapDepth) && currentSiteMapDepth[currentSiteMapDepth.length - 1]; |
| 128 | + if (!routeInfo) throwPageNotFound(`Route ${location} not found`); |
| 129 | + |
| 130 | + return <PageRenderer id={routeInfo.page} />; |
| 131 | + }) |
| 132 | +); |
| 133 | + |
| 134 | +export const PageIdParamRenderer: React.FC = Suspense.with({ fallback: <CenteredLoadingPage /> }, () => { |
| 135 | + const { id } = useParams(); |
| 136 | + if (!id) throwPageNotFound("Page ID is required"); |
| 137 | + return <PageRenderer id={id} />; |
| 138 | +}); |
0 commit comments