Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
/**
|
|
* Loading lifecycle for a single catalog GET, with pull-to-refresh support.
|
|
*
|
|
* Owns the loading / ready / error status plus a separate `refreshing` flag so
|
|
* a pull-to-refresh keeps the current list on screen (rather than flashing the
|
|
* full-screen spinner). The `fetcher` MUST be memoised by the caller (usually
|
|
* `useCallback` keyed on the auth token) — the hook reloads whenever the
|
|
* fetcher identity changes.
|
|
*/
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import { isAbortError } from "@/lib/api";
|
|
import { describeError } from "@/components/me/errors";
|
|
import type { ResourceStatus } from "@/components/me/use-resource";
|
|
|
|
export interface CatalogResource<T> {
|
|
status: ResourceStatus;
|
|
data: T | null;
|
|
error: string;
|
|
refreshing: boolean;
|
|
reload: () => void;
|
|
refresh: () => void;
|
|
}
|
|
|
|
export function useCatalogResource<T>(fetcher: () => Promise<T>): CatalogResource<T> {
|
|
const [status, setStatus] = useState<ResourceStatus>("loading");
|
|
const [data, setData] = useState<T | null>(null);
|
|
const [error, setError] = useState("");
|
|
const [refreshing, setRefreshing] = useState(false);
|
|
const [nonce, setNonce] = useState(0);
|
|
|
|
const reload = useCallback(() => setNonce((n) => n + 1), []);
|
|
const refresh = useCallback(() => {
|
|
setRefreshing(true);
|
|
setNonce((n) => n + 1);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
if (data === null) setStatus("loading");
|
|
setError("");
|
|
|
|
fetcher()
|
|
.then((result) => {
|
|
if (!active) return;
|
|
setData(result);
|
|
setStatus("ready");
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (!active || isAbortError(err)) return;
|
|
setError(describeError(err, "Failed to load."));
|
|
setStatus("error");
|
|
})
|
|
.finally(() => {
|
|
if (active) setRefreshing(false);
|
|
});
|
|
|
|
return () => {
|
|
active = false;
|
|
};
|
|
// `data` intentionally omitted: we only branch on its first-load emptiness.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [fetcher, nonce]);
|
|
|
|
return { status, data, error, refreshing, reload, refresh };
|
|
}
|