Skip to content

Commit

Permalink
Handled 404 pages
Browse files Browse the repository at this point in the history
  • Loading branch information
silversonicaxel committed Sep 10, 2024
1 parent 9b80972 commit c134ad1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
3 changes: 3 additions & 0 deletions locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,8 @@
"label": "reload the {{page}} page"
}
}
},
"notfound": {
"description": "the page is not found, visit the home page to solve the problem."
}
}
3 changes: 3 additions & 0 deletions locales/it/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,8 @@
"label": "ricarica la pagina {{page}}"
}
}
},
"notfound": {
"description": "la pagina non è stata trovata, visita la pagina home per risolvere il problema."
}
}
30 changes: 30 additions & 0 deletions src/app/[locale]/not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'

import { useParams } from 'next/navigation'
import type { Metadata } from 'next/types'
import { useCallback } from 'react'

import { ErrorContent } from 'src/components/error-content'
import { useTranslationClient } from 'src/helpers/hooks/useTranslationClient'
import { ComponentErrorParams } from 'src/types/component'


type HomeNotFoundProps = ComponentErrorParams

export const metadata: Metadata = {
title: 'unknown art',
}

export default function HomeNotFound({ reset }: HomeNotFoundProps) {
const params = useParams()
const locale = params.locale as string

const { t } = useTranslationClient({ locale, namespace: 'common' })
const tPage = t('menu.home')

const onReset = useCallback(() => {
reset()
}, [reset])

return (<ErrorContent page={tPage} onReset={onReset} isNotFound />)
}
37 changes: 37 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'src/styles/globals.css'
import 'src/styles/reset.css'
import type { ReactNode } from 'react'

import { Analytics } from 'src/components/analytics'
import { Header } from 'src/components/header'
import { font } from 'src/helpers/config/font'
import { DialogProvider } from 'src/helpers/providers/dialog'
import { isEnvironmentProduction } from 'src/helpers/utils/isEnvironment'
import type { ComponentParams } from 'src/types/component'


type AppLayoutProps = {
children: ReactNode
} & ComponentParams

export default function AppLayout({ children }: AppLayoutProps) {
const environment = process.env.NEXT_PUBLIC_NODE_ENV || 'development'

return (
<html>
<head />

{isEnvironmentProduction(environment) && (<Analytics />)}

<body className={font.className}>
<DialogProvider />

<main>
<Header />

{children}
</main>
</body>
</html>
)
}
30 changes: 30 additions & 0 deletions src/app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'

import { useParams } from 'next/navigation'
import type { Metadata } from 'next/types'
import { useCallback } from 'react'

import { ErrorContent } from 'src/components/error-content'
import { useTranslationClient } from 'src/helpers/hooks/useTranslationClient'
import { ComponentErrorParams } from 'src/types/component'


type AppNotFoundProps = ComponentErrorParams

export const metadata: Metadata = {
title: 'unknown art',
}

export default function AppNotFound({ reset }: AppNotFoundProps) {
const params = useParams()
const locale = params.locale as string

const { t } = useTranslationClient({ locale, namespace: 'common' })
const tPage = t('menu.home')

const onReset = useCallback(() => {
reset()
}, [reset])

return (<ErrorContent page={tPage} onReset={onReset} isNotFound />)
}
7 changes: 5 additions & 2 deletions src/components/error-content/error-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ import { ComponentErrorParams } from 'src/types/component'
type ErrorContentProps = {
page: string
onReset: ComponentErrorParams['reset']
isNotFound?: boolean
}

export const ErrorContent: FC<ErrorContentProps> = memo(({ page, onReset }) => {
export const ErrorContent: FC<ErrorContentProps> = memo(({ page, onReset, isNotFound }) => {
const params = useParams()
const locale = params.locale as string

const { t } = useTranslationClient({ locale, namespace: 'common' })

const description = isNotFound ? t('notfound.description') : t('error.description', { page })

return (
<>
<h1>{t('title')}</h1>

<p>{t('error.description', { page })}</p>
<p>{description}</p>

<a
role="button"
Expand Down

0 comments on commit c134ad1

Please sign in to comment.