|
| 1 | +import { Button } from '@gouvfr-lasuite/cunningham-react'; |
| 2 | +import Head from 'next/head'; |
| 3 | +import Image, { StaticImageData } from 'next/image'; |
| 4 | +import { useTranslation } from 'react-i18next'; |
| 5 | +import styled from 'styled-components'; |
| 6 | + |
| 7 | +import { Box, Icon, StyledLink, Text } from '@/components'; |
| 8 | + |
| 9 | +const StyledButton = styled(Button)` |
| 10 | + width: fit-content; |
| 11 | +`; |
| 12 | + |
| 13 | +interface ErrorPageProps { |
| 14 | + image: StaticImageData; |
| 15 | + description: string; |
| 16 | + refreshTarget?: string; |
| 17 | + showReload?: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +const getSafeRefreshUrl = (target?: string): string | undefined => { |
| 21 | + if (!target) { |
| 22 | + return undefined; |
| 23 | + } |
| 24 | + |
| 25 | + if (typeof window === 'undefined') { |
| 26 | + return target.startsWith('/') && !target.startsWith('//') |
| 27 | + ? target |
| 28 | + : undefined; |
| 29 | + } |
| 30 | + |
| 31 | + try { |
| 32 | + const url = new URL(target, window.location.origin); |
| 33 | + if (url.origin !== window.location.origin) { |
| 34 | + return undefined; |
| 35 | + } |
| 36 | + return url.pathname + url.search + url.hash; |
| 37 | + } catch { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +export const ErrorPage = ({ |
| 43 | + image, |
| 44 | + description, |
| 45 | + refreshTarget, |
| 46 | + showReload, |
| 47 | +}: ErrorPageProps) => { |
| 48 | + const { t } = useTranslation(); |
| 49 | + |
| 50 | + const errorTitle = t('An unexpected error occurred.'); |
| 51 | + const safeTarget = getSafeRefreshUrl(refreshTarget); |
| 52 | + |
| 53 | + return ( |
| 54 | + <> |
| 55 | + <Head> |
| 56 | + <title> |
| 57 | + {errorTitle} - {t('Docs')} |
| 58 | + </title> |
| 59 | + <meta |
| 60 | + property="og:title" |
| 61 | + content={`${errorTitle} - ${t('Docs')}`} |
| 62 | + key="title" |
| 63 | + /> |
| 64 | + </Head> |
| 65 | + <Box |
| 66 | + $align="center" |
| 67 | + $margin="auto" |
| 68 | + $gap="md" |
| 69 | + $padding={{ bottom: '2rem' }} |
| 70 | + > |
| 71 | + <Text as="h1" $textAlign="center" className="sr-only"> |
| 72 | + {errorTitle} - {t('Docs')} |
| 73 | + </Text> |
| 74 | + <Image |
| 75 | + src={image} |
| 76 | + alt="" |
| 77 | + width={300} |
| 78 | + style={{ |
| 79 | + maxWidth: '100%', |
| 80 | + height: 'auto', |
| 81 | + }} |
| 82 | + /> |
| 83 | + |
| 84 | + <Text |
| 85 | + as="p" |
| 86 | + $textAlign="center" |
| 87 | + $maxWidth="350px" |
| 88 | + $theme="neutral" |
| 89 | + $margin="0" |
| 90 | + > |
| 91 | + {description} |
| 92 | + </Text> |
| 93 | + |
| 94 | + <Box $direction="row" $gap="sm"> |
| 95 | + <StyledLink href="/"> |
| 96 | + <StyledButton |
| 97 | + color="neutral" |
| 98 | + icon={ |
| 99 | + <Icon |
| 100 | + iconName="house" |
| 101 | + variant="symbols-outlined" |
| 102 | + $withThemeInherited |
| 103 | + /> |
| 104 | + } |
| 105 | + > |
| 106 | + {t('Home')} |
| 107 | + </StyledButton> |
| 108 | + </StyledLink> |
| 109 | + |
| 110 | + {(safeTarget || showReload) && ( |
| 111 | + <StyledButton |
| 112 | + color="neutral" |
| 113 | + variant="bordered" |
| 114 | + icon={ |
| 115 | + <Icon |
| 116 | + iconName="refresh" |
| 117 | + variant="symbols-outlined" |
| 118 | + $withThemeInherited |
| 119 | + /> |
| 120 | + } |
| 121 | + onClick={() => |
| 122 | + safeTarget |
| 123 | + ? window.location.assign(safeTarget) |
| 124 | + : window.location.reload() |
| 125 | + } |
| 126 | + > |
| 127 | + {t('Refresh page')} |
| 128 | + </StyledButton> |
| 129 | + )} |
| 130 | + </Box> |
| 131 | + </Box> |
| 132 | + </> |
| 133 | + ); |
| 134 | +}; |
0 commit comments