videogen/apps/web/lib/use-generation-capabilities.ts
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
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.
2026-12-04 02:10:26 +03:00

101 lines
3.5 KiB
TypeScript

"use client";
import * as React from "react";
import { ApiError, getPilotCapabilities } from "@/lib/api";
import { hasRealClerkKey } from "@/lib/clerk";
import type { PilotCapabilities } from "@/lib/types";
import { useApiToken } from "@/lib/use-api-token";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
export interface GenerationCapabilitiesState {
capabilities: PilotCapabilities | null;
loading: boolean;
error: string;
/** Ask once more, e.g. after a shown error, without a full page reload. */
retry: () => void;
}
/** A brief server hiccup (5xx) or dropped connection, not a real 4xx policy answer. */
function isTransient(error: unknown): boolean {
if (error instanceof ApiError) return error.status >= 500;
return true;
}
/** How long to wait before the one silent retry on a transient failure. */
const AUTO_RETRY_DELAY_MS = 1500;
/**
* Load the server-owned feature gate for the authenticated session.
*
* On a transient failure (a 5xx, or any non-`ApiError` network hiccup) this
* retries silently once before giving up — a brand-new signup's very first
* authenticated page load fires this alongside several other calls that can
* race each other server-side (see the Clerk/Supabase user-provisioning
* race fixed 2026-11-17), and until that race is fixed everywhere a losing
* request here used to permanently show "reel rendering isn't open for this
* account yet" with no way back except a full reload. A real 4xx (the
* account genuinely isn't admitted) is trusted immediately, no retry.
*/
export function useGenerationCapabilities(): GenerationCapabilitiesState {
const getApiToken = useApiToken();
const [state, setState] = React.useState<Omit<GenerationCapabilitiesState, "retry">>({
capabilities: null,
loading: true,
error: "",
});
const [nonce, setNonce] = React.useState(0);
React.useEffect(() => {
let active = true;
const controller = new AbortController();
const load = async (attempt: number): Promise<void> => {
try {
const token = await getApiToken();
// Signed-out visitors are redirected to sign-up; the capability gate
// needs a session, so skip the call instead of logging a 401. Without
// Clerk (local dev) the app-key fallback still applies.
if (!token && hasRealClerkKey) {
if (active) {
setState({
capabilities: null,
loading: false,
error: "Sign in to check generation availability.",
});
}
return;
}
const capabilities = await getPilotCapabilities({
token,
signal: controller.signal,
});
if (active) setState({ capabilities, loading: false, error: "" });
} catch (error) {
if (!active || controller.signal.aborted) return;
if (attempt === 0 && isTransient(error)) {
await new Promise((resolve) => setTimeout(resolve, AUTO_RETRY_DELAY_MS));
if (active && !controller.signal.aborted) await load(1);
return;
}
setState({
capabilities: null,
loading: false,
error:
friendlyErrorMessage(error, "Could not check generation availability."),
});
}
};
void load(0);
return () => {
active = false;
controller.abort();
};
}, [getApiToken, nonce]);
const retry = React.useCallback(() => {
setState({ capabilities: null, loading: true, error: "" });
setNonce((n) => n + 1);
}, []);
return { ...state, retry };
}