-
Notifications
You must be signed in to change notification settings - Fork 574
♿️(frontend) structure correctly 5xx error alerts #2128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { Button } from '@gouvfr-lasuite/cunningham-react'; | ||
| import Head from 'next/head'; | ||
| import Image, { StaticImageData } from 'next/image'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import styled from 'styled-components'; | ||
|
|
||
| import { Box, Icon, StyledLink, Text } from '@/components'; | ||
|
|
||
| const StyledButton = styled(Button)` | ||
| width: fit-content; | ||
| `; | ||
|
|
||
| interface ErrorPageProps { | ||
| image: StaticImageData; | ||
| description: string; | ||
| refreshTarget?: string; | ||
| showReload?: boolean; | ||
| } | ||
|
|
||
| const getSafeRefreshUrl = (target?: string): string | undefined => { | ||
| if (!target) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (typeof window === 'undefined') { | ||
| return target.startsWith('/') && !target.startsWith('//') | ||
| ? target | ||
| : undefined; | ||
| } | ||
|
|
||
| try { | ||
| const url = new URL(target, window.location.origin); | ||
| if (url.origin !== window.location.origin) { | ||
| return undefined; | ||
| } | ||
| return url.pathname + url.search + url.hash; | ||
| } catch { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| export const ErrorPage = ({ | ||
| image, | ||
| description, | ||
| refreshTarget, | ||
| showReload, | ||
| }: ErrorPageProps) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| const errorTitle = t('An unexpected error occurred.'); | ||
| const safeTarget = getSafeRefreshUrl(refreshTarget); | ||
|
|
||
| return ( | ||
| <> | ||
| <Head> | ||
| <title> | ||
| {errorTitle} - {t('Docs')} | ||
| </title> | ||
| <meta | ||
| property="og:title" | ||
| content={`${errorTitle} - ${t('Docs')}`} | ||
| key="title" | ||
| /> | ||
| </Head> | ||
| <Box | ||
| $align="center" | ||
| $margin="auto" | ||
| $gap="md" | ||
| $padding={{ bottom: '2rem' }} | ||
| > | ||
| <Text as="h1" $textAlign="center" className="sr-only"> | ||
| {errorTitle} - {t('Docs')} | ||
| </Text> | ||
| <Image | ||
| src={image} | ||
| alt="" | ||
| width={300} | ||
| style={{ | ||
| maxWidth: '100%', | ||
| height: 'auto', | ||
| }} | ||
| /> | ||
|
|
||
| <Text | ||
| as="p" | ||
| $textAlign="center" | ||
| $maxWidth="350px" | ||
| $theme="neutral" | ||
| $margin="0" | ||
| > | ||
| {description} | ||
| </Text> | ||
|
|
||
| <Box $direction="row" $gap="sm"> | ||
| <StyledLink href="/"> | ||
| <StyledButton | ||
| color="neutral" | ||
| icon={ | ||
| <Icon | ||
| iconName="house" | ||
| variant="symbols-outlined" | ||
| $withThemeInherited | ||
| /> | ||
| } | ||
| > | ||
| {t('Home')} | ||
| </StyledButton> | ||
| </StyledLink> | ||
|
|
||
| {(safeTarget || showReload) && ( | ||
| <StyledButton | ||
| color="neutral" | ||
| variant="bordered" | ||
| icon={ | ||
| <Icon | ||
| iconName="refresh" | ||
| variant="symbols-outlined" | ||
| $withThemeInherited | ||
| /> | ||
| } | ||
| onClick={() => | ||
| safeTarget | ||
| ? window.location.assign(safeTarget) | ||
Check warningCode scanning / CodeQL Client-side URL redirect Medium
Untrusted URL redirection depends on a
user-provided value Error loading related location Loading |
||
|
|
||
| : window.location.reload() | ||
| } | ||
| > | ||
| {t('Refresh page')} | ||
| </StyledButton> | ||
| )} | ||
| </Box> | ||
| </Box> | ||
| </> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Ovgodd marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { useRouter } from 'next/router'; | ||
| import { ReactElement } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import error_img from '@/assets/icons/error-coffee.png'; | ||
| import { ErrorPage } from '@/components'; | ||
| import { PageLayout } from '@/layouts'; | ||
| import { NextPageWithLayout } from '@/types/next'; | ||
|
|
||
| const Page: NextPageWithLayout = () => { | ||
| const { t } = useTranslation(); | ||
| const { query } = useRouter(); | ||
| const from = Array.isArray(query.from) ? query.from[0] : query.from; | ||
| const refreshTarget = | ||
| from?.startsWith('/') && !from.startsWith('//') ? from : undefined; | ||
|
|
||
| return ( | ||
| <ErrorPage | ||
| image={error_img} | ||
| description={t( | ||
| 'An unexpected error occurred. Go grab a coffee or try to refresh the page.', | ||
| )} | ||
| refreshTarget={refreshTarget} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| Page.getLayout = function getLayout(page: ReactElement) { | ||
| return <PageLayout withFooter={false}>{page}</PageLayout>; | ||
| }; | ||
|
|
||
| export default Page; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.