hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { useLocation } from "@tanstack/solid-router"; |
| 2 | import { createSignal, For, onCleanup } from "solid-js"; |
| 3 | import { ApiError, formatApiError } from "@/api/client"; |
| 4 | import { Button } from "@/components/ui/button"; |
| 5 | import { EmptyState } from "@/components/ui/empty-state"; |
| 6 | import { IconChevronDown } from "@/components/ui/icons"; |
| 7 | import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; |
| 8 | import type { MessageKey } from "@/i18n/messages"; |
| 9 | |
| 10 | function errorKind(err: unknown): MessageKey { |
| 11 | if (err instanceof ApiError) { |
| 12 | if (err.status === 429) return "errors.pageLoad.kind.rateLimited"; |
| 13 | if (err.status === 403 && err.module != null) return "errors.pageLoad.kind.moduleOff"; |
| 14 | if (err.status === 401) return "errors.pageLoad.kind.unauthorized"; |
| 15 | if (err.status === 403) return "errors.pageLoad.kind.forbidden"; |
| 16 | if (err.status === 408) return "errors.pageLoad.kind.timeout"; |
| 17 | if (err.status === 503) return "errors.pageLoad.kind.unavailable"; |
| 18 | if (err.status >= 500) return "errors.pageLoad.kind.server"; |
| 19 | return "errors.pageLoad.kind.request"; |
| 20 | } |
| 21 | // fetch() rejects with a TypeError when the request never got an answer. |
| 22 | if (err instanceof TypeError && /fetch|network|load failed/i.test(err.message)) { |
| 23 | return typeof navigator !== "undefined" && navigator.onLine === false |
| 24 | ? "errors.pageLoad.kind.offline" |
| 25 | : "errors.pageLoad.kind.network"; |
| 26 | } |
| 27 | return "errors.pageLoad.kind.render"; |
| 28 | } |
| 29 | |
| 30 | function rawMessage(err: unknown): string { |
| 31 | if (err instanceof ApiError) return err.message; |
| 32 | if (err instanceof Error) return err.message ? `${err.name}: ${err.message}` : err.name; |
| 33 | return String(err); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * A page that could not load: the reason, a retry that waits out a |
| 38 | * rate limit, and the technical details behind a disclosure. Used by the |
| 39 | * router's error component and by RouteGuard when the session read fails. |
| 40 | */ |
| 41 | export function PageLoadError(props: { error: unknown; reset: () => void }) { |
| 42 | const location = useLocation(); |
| 43 | const api = props.error instanceof ApiError ? props.error : null; |
| 44 | |
| 45 | // A rate-limited or unavailable answer says when a retry can work; retrying |
| 46 | // sooner only earns another refusal, so the button waits it out. |
| 47 | const [wait, setWait] = createSignal( |
| 48 | api && (api.status === 429 || api.status === 503) && api.retryAfter != null ? Math.max(0, Math.ceil(api.retryAfter)) : 0, |
| 49 | ); |
| 50 | if (wait() > 0) { |
| 51 | const timer = setInterval(() => { |
| 52 | setWait((s) => { |
| 53 | const next = Math.max(0, s - 1); |
| 54 | if (next === 0) clearInterval(timer); |
| 55 | return next; |
| 56 | }); |
| 57 | }, 1000); |
| 58 | onCleanup(() => clearInterval(timer)); |
| 59 | } |
| 60 | |
| 61 | const rows = (): { label: string; value: string; mono?: boolean }[] => { |
| 62 | const list: { label: string; value: string; mono?: boolean }[] = []; |
| 63 | list.push({ label: tt("errors.pageLoad.kind"), value: tt(errorKind(props.error)) }); |
| 64 | if (api) list.push({ label: tt("errors.pageLoad.status"), value: String(api.status), mono: true }); |
| 65 | if (api?.code) list.push({ label: tt("errors.pageLoad.code"), value: api.code, mono: true }); |
| 66 | if (api?.module) list.push({ label: tt("errors.pageLoad.module"), value: api.module, mono: true }); |
| 67 | if (api?.retryAfter != null) |
| 68 | list.push({ label: tt("errors.pageLoad.retryAfter"), value: tt("errors.pageLoad.seconds", { seconds: api.retryAfter }) }); |
| 69 | list.push({ label: tt("errors.pageLoad.page"), value: location().href, mono: true }); |
| 70 | const raw = rawMessage(props.error).trim(); |
| 71 | if (raw) list.push({ label: tt("errors.pageLoad.message"), value: raw, mono: true }); |
| 72 | return list; |
| 73 | }; |
| 74 | |
| 75 | return ( |
| 76 | <PageCenter alert> |
| 77 | <EmptyState |
| 78 | kind="error" |
| 79 | class={PAGE_STATE_CARD} |
| 80 | title={tt("errors.pageLoad.title")} |
| 81 | description={formatApiError(props.error)} |
| 82 | action={ |
| 83 | <div class="flex w-full flex-col items-center gap-5"> |
| 84 | <Button type="button" size="sm" class="h-9 rounded-lg px-4 text-sm" disabled={wait() > 0} onClick={() => props.reset()}> |
| 85 | {wait() > 0 ? tt("errors.pageLoad.retryIn", { seconds: wait() }) : tt("errors.pageLoad.retry")} |
| 86 | </Button> |
| 87 | <details class="group w-full rounded-lg border border-border-hairline bg-surface-tint/60 text-left"> |
| 88 | <summary class="flex cursor-pointer list-none items-center justify-between gap-3 rounded-lg px-4 py-2.5 text-sm font-medium text-text-strong outline-hidden focus-visible:ring-2 focus-visible:ring-ring [&::-webkit-details-marker]:hidden"> |
| 89 | <span>{tt("errors.pageLoad.details")}</span> |
| 90 | <IconChevronDown class="h-4 w-4 shrink-0 text-text-subtle transition-transform group-open:rotate-180" /> |
| 91 | </summary> |
| 92 | <dl class="grid grid-cols-1 gap-x-4 gap-y-1 border-t border-border-hairline px-4 py-3 text-xs sm:grid-cols-[max-content_1fr] sm:gap-y-2"> |
| 93 | <For each={rows()}> |
| 94 | {(row) => ( |
| 95 | <> |
| 96 | <dt class="pt-1 font-medium text-text-subtle sm:pt-0">{row.label}</dt> |
| 97 | <dd class={row.mono ? "break-all font-mono text-text-strong" : "text-text-strong"}>{row.value}</dd> |
| 98 | </> |
| 99 | )} |
| 100 | </For> |
| 101 | </dl> |
| 102 | </details> |
| 103 | </div> |
| 104 | } |
| 105 | /> |
| 106 | </PageCenter> |
| 107 | ); |
| 108 | } |