|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { dictFor, DEFAULT_LANG, type Lang } from "@/lib/i18n"; |
| 6 | + |
| 7 | +/* G-03 — Root segment error boundary. |
| 8 | + * |
| 9 | + * Mirror of `app/miasto/error.tsx` (R-07) but at the root segment so |
| 10 | + * exceptions outside `/miasto` (auth flows, /games, /leaderboard, |
| 11 | + * /loans, etc.) no longer fall through to the browser-default error |
| 12 | + * page. Next.js mounts this Client Component when ANY server or |
| 13 | + * client component below `app/` throws and no nearer error.tsx |
| 14 | + * catches it. |
| 15 | + * |
| 16 | + * i18n note: Client Components can't use `getLang()` (server-only). |
| 17 | + * We read `<html lang="...">` once on mount; if the value isn't a |
| 18 | + * supported locale, fall back to DEFAULT_LANG. The dict copy lives |
| 19 | + * in `lib/locales/{pl,uk,cs,en}.ts` `errors.*` so all four locales |
| 20 | + * see localized text instead of the legacy hardcoded PL. |
| 21 | + */ |
| 22 | +export default function RootError({ |
| 23 | + error, |
| 24 | + reset, |
| 25 | +}: { |
| 26 | + error: Error & { digest?: string }; |
| 27 | + reset: () => void; |
| 28 | +}) { |
| 29 | + const [lang, setLang] = useState<Lang>(DEFAULT_LANG); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (typeof document !== "undefined") { |
| 33 | + const docLang = document.documentElement.lang as Lang; |
| 34 | + if (["pl", "uk", "cs", "en"].includes(docLang)) setLang(docLang); |
| 35 | + } |
| 36 | + if (typeof console !== "undefined") { |
| 37 | + console.error("[root] segment error:", error); |
| 38 | + } |
| 39 | + }, [error]); |
| 40 | + |
| 41 | + const t = dictFor(lang).errors; |
| 42 | + |
| 43 | + return ( |
| 44 | + <main className="flex flex-col gap-4 max-w-xl mx-auto py-12 animate-slide-up"> |
| 45 | + <span aria-hidden className="text-5xl text-center"> |
| 46 | + ⚠️ |
| 47 | + </span> |
| 48 | + <h1 className="t-h2 text-[var(--accent)] text-center">{t.title}</h1> |
| 49 | + <p className="t-body text-[var(--ink-muted)] text-center">{t.body}</p> |
| 50 | + {error.digest && ( |
| 51 | + <p className="t-caption text-[var(--ink-subtle)] text-center font-mono"> |
| 52 | + ref: {error.digest} |
| 53 | + </p> |
| 54 | + )} |
| 55 | + <div className="flex flex-wrap items-center justify-center gap-3"> |
| 56 | + <button type="button" className="btn btn-primary" onClick={reset}> |
| 57 | + {t.retry} |
| 58 | + </button> |
| 59 | + <Link href="/" className="btn btn-secondary"> |
| 60 | + {t.back} |
| 61 | + </Link> |
| 62 | + </div> |
| 63 | + </main> |
| 64 | + ); |
| 65 | +} |
0 commit comments