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.
124 lines
3.6 KiB
TypeScript
124 lines
3.6 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { ApiError } from "@/lib/api";
|
|
import {
|
|
listAvatars,
|
|
listCaptionStyles,
|
|
listCoverStyles,
|
|
listHooks,
|
|
listLayouts,
|
|
listMusic,
|
|
listTemplates,
|
|
listVoices,
|
|
} from "@/lib/api";
|
|
import type { RequestOptions } from "@/src/shared/api";
|
|
import type {
|
|
Avatar,
|
|
CaptionStyle,
|
|
CoverStyle,
|
|
HookCatalog,
|
|
LayoutOption,
|
|
MusicTrack,
|
|
Template,
|
|
Voice,
|
|
} from "@/lib/types";
|
|
import { useApiToken } from "@/lib/use-api-token";
|
|
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
|
|
|
|
export type ResourceStatus = "loading" | "ready" | "error";
|
|
|
|
export interface UseResourceResult<T> {
|
|
status: ResourceStatus;
|
|
data: T | null;
|
|
error: string;
|
|
reload: () => void;
|
|
}
|
|
|
|
function messageOf(err: unknown, fallback: string): string {
|
|
if (err instanceof ApiError) return friendlyErrorMessage(err);
|
|
if (err instanceof Error) return friendlyErrorMessage(err);
|
|
return fallback;
|
|
}
|
|
|
|
/**
|
|
* Loads a read-only backend resource once on mount (and on demand via `reload`),
|
|
* threading the caller's Clerk token through the shared API client. `fetcher`
|
|
* MUST be a stable module-level reference (the exported `lib/api` functions are).
|
|
* State is always replaced, never mutated in place.
|
|
*/
|
|
export function useApiResource<T>(
|
|
fetcher: (options?: RequestOptions) => Promise<T>,
|
|
fallback: string,
|
|
): UseResourceResult<T> {
|
|
const getApiToken = useApiToken();
|
|
const [status, setStatus] = React.useState<ResourceStatus>("loading");
|
|
const [data, setData] = React.useState<T | null>(null);
|
|
const [error, setError] = React.useState<string>("");
|
|
const [nonce, setNonce] = React.useState(0);
|
|
|
|
React.useEffect(() => {
|
|
let cancelled = false;
|
|
const controller = new AbortController();
|
|
|
|
const load = async (): Promise<void> => {
|
|
setStatus("loading");
|
|
setError("");
|
|
try {
|
|
const token = await getApiToken();
|
|
const result = await fetcher({ token, signal: controller.signal });
|
|
if (cancelled) return;
|
|
setData(result);
|
|
setStatus("ready");
|
|
} catch (err) {
|
|
if (cancelled) return;
|
|
if (err instanceof DOMException && err.name === "AbortError") return;
|
|
setError(messageOf(err, fallback));
|
|
setStatus("error");
|
|
}
|
|
};
|
|
|
|
void load();
|
|
return () => {
|
|
cancelled = true;
|
|
controller.abort();
|
|
};
|
|
}, [fetcher, fallback, getApiToken, nonce]);
|
|
|
|
const reload = React.useCallback(() => setNonce((n) => n + 1), []);
|
|
|
|
return { status, data, error, reload };
|
|
}
|
|
|
|
export function useCaptionStyles(): UseResourceResult<CaptionStyle[]> {
|
|
return useApiResource(listCaptionStyles, "Could not load caption styles.");
|
|
}
|
|
|
|
export function useMusic(): UseResourceResult<MusicTrack[]> {
|
|
return useApiResource(listMusic, "Could not load the music library.");
|
|
}
|
|
|
|
export function useLayouts(): UseResourceResult<LayoutOption[]> {
|
|
return useApiResource(listLayouts, "Could not load composition layouts.");
|
|
}
|
|
|
|
export function useTemplates(): UseResourceResult<Template[]> {
|
|
return useApiResource(listTemplates, "Could not load templates.");
|
|
}
|
|
|
|
export function useHookCatalog(): UseResourceResult<HookCatalog> {
|
|
return useApiResource(listHooks, "Could not load the hook catalog.");
|
|
}
|
|
|
|
export function useCoverStylesCatalog(): UseResourceResult<CoverStyle[]> {
|
|
return useApiResource(listCoverStyles, "Could not load cover styles.");
|
|
}
|
|
|
|
export function useAvatarsCatalog(): UseResourceResult<Avatar[]> {
|
|
return useApiResource(listAvatars, "Could not load the avatar gallery.");
|
|
}
|
|
|
|
export function useVoicesCatalog(): UseResourceResult<Voice[]> {
|
|
return useApiResource(listVoices, "Could not load the voice gallery.");
|
|
}
|