hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { createResource } from "@/lib/create-resource"; |
| 2 | |
| 3 | /** |
| 4 | * Resources for a board of independent panels (the dashboard). |
| 5 | * |
| 6 | * A plain resource rethrows its error on read, so one failed request took |
| 7 | * down every panel with it. Here a failed source reads as `undefined` — its |
| 8 | * panel shows its own empty/"—" state while `.error` stays available for the |
| 9 | * board's notice — and `retryFailed` refetches only the sources that failed. |
| 10 | */ |
| 11 | export function createBoardResources() { |
| 12 | const sources: { failed: () => boolean; refetch: () => unknown }[] = []; |
| 13 | |
| 14 | const resource = ((...args: unknown[]) => { |
| 15 | const [res, actions] = (createResource as unknown as (...a: unknown[]) => [Record<string, unknown> & (() => unknown), { refetch: () => unknown }])(...args); |
| 16 | const read = () => (res.error ? undefined : res()); |
| 17 | for (const key of ["state", "loading", "error"] as const) { |
| 18 | Object.defineProperty(read, key, { get: () => res[key] }); |
| 19 | } |
| 20 | Object.defineProperty(read, "latest", { get: () => (res.error ? undefined : res.latest) }); |
| 21 | sources.push({ failed: () => !!res.error, refetch: actions.refetch }); |
| 22 | return [read, actions]; |
| 23 | }) as typeof createResource; |
| 24 | |
| 25 | const retryFailed = () => { |
| 26 | for (const source of sources) if (source.failed()) void source.refetch(); |
| 27 | }; |
| 28 | |
| 29 | return { createResource: resource, retryFailed }; |
| 30 | } |