diff --git a/src/api/client/client.ts b/src/api/client/client.ts index 77959c9..19a2432 100644 --- a/src/api/client/client.ts +++ b/src/api/client/client.ts @@ -172,7 +172,7 @@ const FIELD_LABELS: Record> = { username: { en: "Username", tr: "Kullanıcı adı" }, }; -function currentLocale(): Locale { +export function currentLocale(): Locale { if (typeof window === "undefined") return "en"; try { const saved = window.localStorage.getItem("hezarfen.locale"); diff --git a/src/app.tsx b/src/app.tsx index 7dcecef..c193b01 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,18 +1,43 @@ import { RouterProvider } from "@tanstack/solid-router"; -import { Suspense, lazy } from "solid-js"; +import { ErrorBoundary, Suspense, lazy } from "solid-js"; +import { currentLocale, formatApiError } from "@/api/client"; import { router } from "@/routes/router"; const RouterDevtools = import.meta.env.DEV ? lazy(() => import("@/router-devtools")) : undefined; +// Last-resort backstop for anything that throws during render. It lives above +// the i18n provider, so it mirrors the client's own bilingual-fallback idiom +// instead of using t(...); a real message + reload beats a blank document. +function RootErrorFallback(props: { error: unknown; reset: () => void }) { + const tr = currentLocale() === "tr"; + return ( +
+
+

{formatApiError(props.error)}

+ +
+
+ ); +} + export function App() { return ( - <> + }> {RouterDevtools ? ( ) : null} - + ); } diff --git a/src/components/layout/route-guard.tsx b/src/components/layout/route-guard.tsx index 1d60118..89c49f8 100644 --- a/src/components/layout/route-guard.tsx +++ b/src/components/layout/route-guard.tsx @@ -1,7 +1,9 @@ import { Navigate } from "@tanstack/solid-router"; import { type ParentProps, Show } from "solid-js"; import type { Role } from "@/api/client"; +import { formatApiError } from "@/api/client"; import { Alert } from "@/components/ui/alert"; +import { Button } from "@/components/ui/button"; import { PageSpinner } from "@/components/ui/page-spinner"; import { hasExactRole, roleInRange } from "@/lib/roles"; import { useAuth } from "@/stores/auth-context"; @@ -27,6 +29,16 @@ export function RouteGuard( return ( }> + +
+

{formatApiError(auth.error())}

+ +
+ + }> }> {(u) => ( )} +
); } diff --git a/src/stores/auth-context.tsx b/src/stores/auth-context.tsx index b123203..8d62a32 100644 --- a/src/stores/auth-context.tsx +++ b/src/stores/auth-context.tsx @@ -5,6 +5,7 @@ import { createEffect, createResource, createSignal, + onCleanup, useContext, } from "solid-js"; import { getMe } from "@/api/users"; @@ -16,21 +17,42 @@ import { usePreferences } from "@/stores/preferences-context"; type AuthContextValue = { user: Accessor; loading: Accessor; + /** Non-401 bootstrap failure. 401 is "logged out", not an error. */ + error: Accessor; refresh: () => Promise; logout: () => Promise; }; const AuthContext = createContext(); +/** Retry-After (seconds) for a transient 429/503, else null. */ +function retryAfterFor(err: unknown): number | null { + if ( + err instanceof ApiError && + (err.status === 429 || err.status === 503) && + err.retryAfter != null + ) { + return Math.min(Math.max(err.retryAfter, 0), 60); + } + return null; +} + export function AuthProvider(props: ParentProps) { const prefs = usePreferences(); const [override, setOverride] = createSignal(undefined); + const [error, setError] = createSignal(null); + // The fetcher swallows every failure into `error` state and resolves + // IMMEDIATELY: it must never stay `loading` (that suspends the whole shell + // and blanks the page) nor re-throw during render. Transient 429/503 waits + // happen off the resource, in the background timer below. const [me, { refetch }] = createResource(async () => { + setError(null); try { return await getMe(); } catch (err) { if (err instanceof ApiError && err.status === 401) return null; - throw err; + setError(err); + return null; } }); @@ -51,6 +73,17 @@ export function AuthProvider(props: ParentProps) { return result as User | null | undefined; }; + // Auto-heal a rate-limited bootstrap without blocking render: once `error` + // holds a 429/503 with a Retry-After, schedule one background refresh. The + // user is already looking at the error Alert while this waits; on success + // the app paints. Re-running the effect (new attempt) clears the old timer. + createEffect(() => { + const wait = retryAfterFor(error()); + if (wait == null) return; + const timer = setTimeout(() => void refresh(), wait * 1000); + onCleanup(() => clearTimeout(timer)); + }); + const logout = async () => { try { await postLogout(); @@ -62,6 +95,7 @@ export function AuthProvider(props: ParentProps) { const value: AuthContextValue = { user, loading: () => me.loading && override() === undefined, + error: () => (override() === undefined ? error() : null), refresh, logout, };