From 233723f818be8d05de4950b0a4e942cd2189b0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Fri, 25 Sep 2026 14:44:29 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=A7=AD=20centered,=20illustrated?= =?UTF-8?q?=20load-error,=20not-found=20and=20404=20states?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - shared PageCenter layout fills the content area and centers the card on both axes with a 600px cap, on desktop and phones - load error shows the message, a retry (with a countdown when the response carries retry-after) and a collapsible technical block with the cause, HTTP status, code, module, wait time, page and raw message - record-not-found and the router 404 use the same layout and a not-found scene; hard-coded strings move to i18n - new unDraw server-down scene and an `error` kind on EmptyState --- src/assets/illustrations/README.md | 1 + src/assets/illustrations/server-down.svg | 1 + src/components/layout/page-center.tsx | 33 ++++++ src/components/layout/record-not-found.tsx | 36 +++--- src/components/layout/route-error.test.tsx | 37 ++++++ src/components/layout/route-error.tsx | 125 +++++++++++++++++---- src/components/system/not-found-page.tsx | 55 ++++----- src/components/ui/empty-state.tsx | 19 +++- src/i18n/locales/en.ts | 27 +++++ src/i18n/locales/tr.ts | 27 +++++ src/i18n/messages.ts | 27 +++++ src/lib/illustrations.ts | 1 + 12 files changed, 323 insertions(+), 66 deletions(-) create mode 100644 src/assets/illustrations/server-down.svg create mode 100644 src/components/layout/page-center.tsx diff --git a/src/assets/illustrations/README.md b/src/assets/illustrations/README.md index 24e4e8c..422ff1f 100644 --- a/src/assets/illustrations/README.md +++ b/src/assets/illustrations/README.md @@ -26,5 +26,6 @@ Files are kept exactly as downloaded (`https://cdn.undraw.co/illustrations/server down \ No newline at end of file diff --git a/src/components/layout/page-center.tsx b/src/components/layout/page-center.tsx new file mode 100644 index 0000000..f89337f --- /dev/null +++ b/src/components/layout/page-center.tsx @@ -0,0 +1,33 @@ +import type { JSX } from "solid-js"; +import { currentLocale } from "@/api/client"; +import { formatMessage, messageFor, type MessageKey } from "@/i18n/messages"; +import { cn } from "@/lib/cn"; + +/** Card sizing shared by the full-page states (load failure, record not found). */ +export const PAGE_STATE_CARD = "min-h-[420px] gap-6 px-6 py-10 sm:px-10 sm:py-12"; + +/** + * Text for the full-page states. They can render from the router's error + * component, above the i18n provider, so they read the loaded dictionary + * directly instead of `useT()` (the boot awaits the stored locale first). + */ +export function pageStateText(key: MessageKey, vars?: Record): string { + return formatMessage(messageFor(currentLocale(), key) ?? key, vars); +} + +/** + * Fills the content column under the shell header and centers a full-page + * state on both axes, capped at a comfortable reading width. + */ +export function PageCenter(props: { children: JSX.Element; alert?: boolean; class?: string }) { + return ( + // Below lg: no header, but the shell's page title, 1.5rem top padding and + // the tab bar + quick-action clearance (up to 9rem) at the bottom. + // lg+: the 49px header plus the column's py-6. +
+
+ {props.children} +
+
+ ); +} diff --git a/src/components/layout/record-not-found.tsx b/src/components/layout/record-not-found.tsx index 62e127b..5f61e5f 100644 --- a/src/components/layout/record-not-found.tsx +++ b/src/components/layout/record-not-found.tsx @@ -1,37 +1,35 @@ import { Link, useLocation, useRouter } from "@tanstack/solid-router"; -import { currentLocale } from "@/api/client"; import { buttonVariants } from "@/components/ui/button"; import { EmptyState } from "@/components/ui/empty-state"; +import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; import { cn } from "@/lib/cn"; import { listPathFor } from "@/lib/record-list-path"; /** * What a detail page shows when its record does not exist (a 404 — deleted, * or a bad link): there is nothing to retry, so it offers the way back to the - * list instead. Also rendered by the router's error fallback, which can sit - * above the i18n provider, hence the client's bilingual-fallback idiom. + * list instead. Centered in the content column like the route error view. + * Also rendered by the router's error fallback, which can sit above the i18n + * provider, hence `pageStateText` instead of `useT()`. */ export function RecordNotFound(props: { backTo?: string }) { const router = useRouter(); const location = useLocation(); - const tr = () => currentLocale() === "tr"; const backTo = () => props.backTo ?? listPathFor(location().pathname, (path) => path in router.routesByPath); return ( - - {backTo() === "/" ? (tr() ? "Ana sayfaya dön" : "Back to home") : tr() ? "Listeye dön" : "Back to the list"} - - } - /> + + + {backTo() === "/" ? tt("errors.recordNotFound.backHome") : tt("errors.recordNotFound.backToList")} + + } + /> + ); } diff --git a/src/components/layout/route-error.test.tsx b/src/components/layout/route-error.test.tsx index 85b00c2..bfc2874 100644 --- a/src/components/layout/route-error.test.tsx +++ b/src/components/layout/route-error.test.tsx @@ -73,3 +73,40 @@ test("a 404 shows not-found with a way back to the list, not a retry", async () expect(back.getAttribute("href")).toBe("/courses"); expect(screen.queryByRole("button")).toBeNull(); }); + +test("a rate-limited page is centered with an illustration, waits out retry-after and names the cause", async () => { + const { ApiError } = await import("@/api/client"); + const root = createRootRoute({ component: () => }); + const page = createRoute({ + getParentRoute: () => root, + path: "/exams", + component: () => { + throw new ApiError(429, "too many requests", 15, null, "rate_limited"); + }, + }); + const router = createRouter({ + routeTree: root.addChildren([page]), + history: createMemoryHistory({ initialEntries: ["/exams"] }), + defaultErrorComponent: RouteErrorFallback, + }); + + render(() => ); + + const alert = await screen.findByRole("alert"); + expect(alert.querySelector(".illustration")).toBeTruthy(); + expect(screen.getByText(/^(This page could not load|Bu sayfa yüklenemedi)$/)).toBeTruthy(); + expect(screen.getByText(/^(Try again in 15s\.|15 sn sonra tekrar dene\.)$/)).toBeTruthy(); + + // Retrying before retry-after only earns another 429. + const retry = screen.getByRole("button") as HTMLButtonElement; + expect(retry.disabled).toBe(true); + expect(retry.textContent).toMatch(/15/); + + // The technical details carry the real fields of the error. + const details = alert.querySelector("dl")!; + expect(details.textContent).toContain("429"); + expect(details.textContent).toContain("rate_limited"); + expect(details.textContent).toContain("/exams"); + expect(details.textContent).toContain("too many requests"); + expect(details.textContent).toMatch(/rate limited|hız sınırı/); +}); diff --git a/src/components/layout/route-error.tsx b/src/components/layout/route-error.tsx index 67ebbef..7582091 100644 --- a/src/components/layout/route-error.tsx +++ b/src/components/layout/route-error.tsx @@ -1,8 +1,12 @@ -import type { ErrorComponentProps } from "@tanstack/solid-router"; -import { currentLocale, formatApiError } from "@/api/client"; +import { useLocation, type ErrorComponentProps } from "@tanstack/solid-router"; +import { createSignal, For, onCleanup } from "solid-js"; +import { ApiError, formatApiError } from "@/api/client"; import { Button } from "@/components/ui/button"; -import { IconAlert } from "@/components/ui/icons"; +import { EmptyState } from "@/components/ui/empty-state"; +import { IconChevronDown } from "@/components/ui/icons"; +import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; import { RecordNotFound } from "@/components/layout/record-not-found"; +import type { MessageKey } from "@/i18n/messages"; import { isNotFoundError } from "@/lib/record-list-path"; /** @@ -12,26 +16,109 @@ import { isNotFoundError } from "@/lib/record-list-path"; * Without it the throw reached the app-wide boundary and replaced everything * with a full-screen 500. * - * It can also render for the root route, above the i18n provider, so it uses - * the client's bilingual-fallback idiom instead of `t(...)`. + * It can also render for the root route, above the i18n provider, so its + * text comes from `pageStateText` instead of `useT()`. */ export function RouteErrorFallback(props: ErrorComponentProps) { - const tr = () => currentLocale() === "tr"; // A missing record is an answer, not a failure: retrying a 404 changes nothing. - if (isNotFoundError(props.error)) return ; + if (isNotFoundError(props.error)) { + return ; + } console.error("Route render error:", props.error); + return ; +} + +function errorKind(err: unknown): MessageKey { + if (err instanceof ApiError) { + if (err.status === 429) return "errors.pageLoad.kind.rateLimited"; + if (err.status === 403 && err.module != null) return "errors.pageLoad.kind.moduleOff"; + if (err.status === 401) return "errors.pageLoad.kind.unauthorized"; + if (err.status === 403) return "errors.pageLoad.kind.forbidden"; + if (err.status === 408) return "errors.pageLoad.kind.timeout"; + if (err.status === 503) return "errors.pageLoad.kind.unavailable"; + if (err.status >= 500) return "errors.pageLoad.kind.server"; + return "errors.pageLoad.kind.request"; + } + // fetch() rejects with a TypeError when the request never got an answer. + if (err instanceof TypeError && /fetch|network|load failed/i.test(err.message)) { + return typeof navigator !== "undefined" && navigator.onLine === false + ? "errors.pageLoad.kind.offline" + : "errors.pageLoad.kind.network"; + } + return "errors.pageLoad.kind.render"; +} + +function rawMessage(err: unknown): string { + if (err instanceof ApiError) return err.message; + if (err instanceof Error) return err.message ? `${err.name}: ${err.message}` : err.name; + return String(err); +} + +function PageLoadError(props: { error: unknown; reset: () => void }) { + const location = useLocation(); + const api = props.error instanceof ApiError ? props.error : null; + + // A rate-limited or unavailable answer says when a retry can work; retrying + // sooner only earns another refusal, so the button waits it out. + const [wait, setWait] = createSignal( + api && (api.status === 429 || api.status === 503) && api.retryAfter != null ? Math.max(0, Math.ceil(api.retryAfter)) : 0, + ); + if (wait() > 0) { + const timer = setInterval(() => { + setWait((s) => { + const next = Math.max(0, s - 1); + if (next === 0) clearInterval(timer); + return next; + }); + }, 1000); + onCleanup(() => clearInterval(timer)); + } + + const rows = (): { label: string; value: string; mono?: boolean }[] => { + const list: { label: string; value: string; mono?: boolean }[] = []; + list.push({ label: tt("errors.pageLoad.kind"), value: tt(errorKind(props.error)) }); + if (api) list.push({ label: tt("errors.pageLoad.status"), value: String(api.status), mono: true }); + if (api?.code) list.push({ label: tt("errors.pageLoad.code"), value: api.code, mono: true }); + if (api?.module) list.push({ label: tt("errors.pageLoad.module"), value: api.module, mono: true }); + if (api?.retryAfter != null) + list.push({ label: tt("errors.pageLoad.retryAfter"), value: tt("errors.pageLoad.seconds", { seconds: api.retryAfter }) }); + list.push({ label: tt("errors.pageLoad.page"), value: location().href, mono: true }); + const raw = rawMessage(props.error).trim(); + if (raw) list.push({ label: tt("errors.pageLoad.message"), value: raw, mono: true }); + return list; + }; + return ( - + + + +
+ + {tt("errors.pageLoad.details")} + + +
+ + {(row) => ( + <> +
{row.label}
+
{row.value}
+ + )} +
+
+
+ + } + /> +
); } diff --git a/src/components/system/not-found-page.tsx b/src/components/system/not-found-page.tsx index c2490ae..79b2431 100644 --- a/src/components/system/not-found-page.tsx +++ b/src/components/system/not-found-page.tsx @@ -1,37 +1,40 @@ import { Link } from "@tanstack/solid-router"; import { Button } from "@/components/ui/button"; -import { IconChevronLeft, IconHome, IconSearch } from "@/components/ui/icons"; +import { EmptyState } from "@/components/ui/empty-state"; +import { IconChevronLeft, IconHome } from "@/components/ui/icons"; +import { PAGE_STATE_CARD, PageCenter } from "@/components/layout/page-center"; import { useGoBack } from "@/lib/use-go-back"; import { useT } from "@/stores/preferences-context"; +/** The router's 404: same centered, illustrated card as a missing record. */ export function NotFoundPage() { const t = useT(); const goBack = useGoBack(); return ( -
-
-
- -
-
-

404

-

{t("common.notFound")}

-

{t("errors.notFoundDescription")}

-
-
- - - - {t("common.goHome")} - -
-
-
+ + + + + + {t("common.goHome")} + + + } + /> + ); } diff --git a/src/components/ui/empty-state.tsx b/src/components/ui/empty-state.tsx index e8b2ce0..dafcf55 100644 --- a/src/components/ui/empty-state.tsx +++ b/src/components/ui/empty-state.tsx @@ -1,4 +1,5 @@ import { Show } from "solid-js"; +import { Dynamic } from "solid-js/web"; import type { JSX } from "solid-js"; import { Illustration } from "@/components/ui/illustration"; import { cn } from "@/lib/cn"; @@ -19,7 +20,8 @@ export type EmptyStateKind = | "schedule" | "whiteboard" | "work" - | "coming-soon"; + | "coming-soon" + | "error"; const KIND_ILLUSTRATION: Record = { default: "empty", @@ -37,11 +39,16 @@ const KIND_ILLUSTRATION: Record = { whiteboard: "whiteboard", work: "work", "coming-soon": "coming-soon", + error: "server-down", }; /** Empty panel with a theme-tinted illustration matching what is missing. */ export function EmptyState(props: { title: string; + /** Small line above the title, e.g. the "404" of the not-found page. */ + eyebrow?: string; + /** Render the title as the page's h1 (full-page states that own the page). */ + pageHeading?: boolean; description?: string; kind?: EmptyStateKind; /** A way out, e.g. "clear search" under a no-results panel. */ @@ -60,7 +67,15 @@ export function EmptyState(props: {
-

{props.title}

+ +

{props.eyebrow}

+
+ + {props.title} +

{props.description}

diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index 97dba71..a6ded95 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -383,6 +383,33 @@ export const en: Dict = { "notifications.unreadCount": "{count} unread", "errors.notFoundDescription": "The page you're looking for may have moved or been deleted.", "errors.accessDeniedDescription": "Your role can't view this section. Ask an administrator if you need access.", + "errors.pageLoad.title": "This page could not load", + "errors.pageLoad.retry": "Try again", + "errors.pageLoad.retryIn": "Try again ({seconds}s)", + "errors.pageLoad.details": "Technical details", + "errors.pageLoad.status": "HTTP status", + "errors.pageLoad.kind": "Cause", + "errors.pageLoad.code": "Error code", + "errors.pageLoad.module": "Module", + "errors.pageLoad.retryAfter": "Retry after", + "errors.pageLoad.seconds": "{seconds}s", + "errors.pageLoad.page": "Page", + "errors.pageLoad.message": "Raw message", + "errors.pageLoad.kind.rateLimited": "Too many requests (rate limited)", + "errors.pageLoad.kind.moduleOff": "Module switched off for the school", + "errors.pageLoad.kind.unauthorized": "Session is not valid", + "errors.pageLoad.kind.forbidden": "No permission", + "errors.pageLoad.kind.timeout": "Request timed out", + "errors.pageLoad.kind.unavailable": "Service temporarily unavailable", + "errors.pageLoad.kind.server": "Server error", + "errors.pageLoad.kind.request": "Request rejected", + "errors.pageLoad.kind.offline": "No internet connection", + "errors.pageLoad.kind.network": "Could not reach the server", + "errors.pageLoad.kind.render": "Page error in the app", + "errors.recordNotFound.title": "Record not found", + "errors.recordNotFound.description": "This record may have been deleted, or the link is wrong.", + "errors.recordNotFound.backToList": "Back to the list", + "errors.recordNotFound.backHome": "Back to home", "network.offlineTitle": "You're offline", "network.offlineDescription": "Some actions won't complete until your connection returns.", "network.reconnect": "Reconnect", diff --git a/src/i18n/locales/tr.ts b/src/i18n/locales/tr.ts index 929f88a..35f26b4 100644 --- a/src/i18n/locales/tr.ts +++ b/src/i18n/locales/tr.ts @@ -379,6 +379,33 @@ export const tr: Dict = { "notifications.unreadCount": "{count} okunmamış", "errors.notFoundDescription": "Aradığın sayfa taşınmış ya da silinmiş olabilir.", "errors.accessDeniedDescription": "Rolün bu bölümü görüntülemeye yetkili değil. Gerekliyse kurum yöneticinden yetki iste.", + "errors.pageLoad.title": "Bu sayfa yüklenemedi", + "errors.pageLoad.retry": "Tekrar dene", + "errors.pageLoad.retryIn": "Tekrar dene ({seconds} sn)", + "errors.pageLoad.details": "Teknik ayrıntı", + "errors.pageLoad.status": "HTTP durumu", + "errors.pageLoad.kind": "Sebep", + "errors.pageLoad.code": "Hata kodu", + "errors.pageLoad.module": "Modül", + "errors.pageLoad.retryAfter": "Bekleme süresi", + "errors.pageLoad.seconds": "{seconds} sn", + "errors.pageLoad.page": "Sayfa", + "errors.pageLoad.message": "Ham mesaj", + "errors.pageLoad.kind.rateLimited": "Çok fazla istek (hız sınırı)", + "errors.pageLoad.kind.moduleOff": "Modül okulda kapalı", + "errors.pageLoad.kind.unauthorized": "Oturum geçersiz", + "errors.pageLoad.kind.forbidden": "Yetki yok", + "errors.pageLoad.kind.timeout": "İstek zaman aşımına uğradı", + "errors.pageLoad.kind.unavailable": "Servis geçici olarak kullanılamıyor", + "errors.pageLoad.kind.server": "Sunucu hatası", + "errors.pageLoad.kind.request": "İstek reddedildi", + "errors.pageLoad.kind.offline": "İnternet bağlantısı yok", + "errors.pageLoad.kind.network": "Sunucuya ulaşılamadı", + "errors.pageLoad.kind.render": "Uygulamada sayfa hatası", + "errors.recordNotFound.title": "Kayıt bulunamadı", + "errors.recordNotFound.description": "Bu kayıt silinmiş ya da bağlantı hatalı olabilir.", + "errors.recordNotFound.backToList": "Listeye dön", + "errors.recordNotFound.backHome": "Ana sayfaya dön", "network.offlineTitle": "Bağlantın koptu", "network.offlineDescription": "Bağlantı geri gelene kadar bazı işlemler tamamlanmayabilir.", "network.reconnect": "Yeniden bağlan", diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 0c46b96..1824667 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -588,6 +588,33 @@ export type MessageKey = | "network.offlineTitle" | "network.offlineDescription" | "network.reconnect" + | "errors.pageLoad.title" + | "errors.pageLoad.retry" + | "errors.pageLoad.retryIn" + | "errors.pageLoad.details" + | "errors.pageLoad.status" + | "errors.pageLoad.kind" + | "errors.pageLoad.code" + | "errors.pageLoad.module" + | "errors.pageLoad.retryAfter" + | "errors.pageLoad.seconds" + | "errors.pageLoad.page" + | "errors.pageLoad.message" + | "errors.pageLoad.kind.rateLimited" + | "errors.pageLoad.kind.moduleOff" + | "errors.pageLoad.kind.unauthorized" + | "errors.pageLoad.kind.forbidden" + | "errors.pageLoad.kind.timeout" + | "errors.pageLoad.kind.unavailable" + | "errors.pageLoad.kind.server" + | "errors.pageLoad.kind.request" + | "errors.pageLoad.kind.offline" + | "errors.pageLoad.kind.network" + | "errors.pageLoad.kind.render" + | "errors.recordNotFound.title" + | "errors.recordNotFound.description" + | "errors.recordNotFound.backToList" + | "errors.recordNotFound.backHome" | "parents.title" | "parents.subtitle" | "common.loading" diff --git a/src/lib/illustrations.ts b/src/lib/illustrations.ts index 7495476..286b19b 100644 --- a/src/lib/illustrations.ts +++ b/src/lib/illustrations.ts @@ -21,6 +21,7 @@ export type IllustrationName = | "payments" | "people" | "schedule" + | "server-down" | "whiteboard" | "work";