videogen/apps/web/lib/use-payment-providers.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

81 lines
2.5 KiB
TypeScript

"use client";
import * as React from "react";
import { ApiError, getProviders } from "@/lib/api";
import { type PaymentProvider } from "@/lib/types";
import { useApiToken } from "@/lib/use-api-token";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
export type PaymentProvidersStatus = "loading" | "ready" | "error";
export interface UsePaymentProvidersResult {
status: PaymentProvidersStatus;
/**
* The ENABLED providers to offer at checkout, server-driven and in display
* order. Empty until the first successful load (or when none are configured).
*/
providers: readonly PaymentProvider[];
errorMessage: string;
/** Re-fetch the enabled providers. */
reload: () => void;
}
function messageFor(err: unknown): string {
if (err instanceof ApiError) return friendlyErrorMessage(err);
if (err instanceof Error) return friendlyErrorMessage(err);
return "Couldn't load payment options.";
}
/**
* Loads the payment providers the backend currently has enabled
* (`GET /billing/providers`) so callers render only configured options. The
* list is only ever replaced with the server's authoritative value, never
* mutated in place; loading and error states are exposed for the UI to handle.
*/
export function usePaymentProviders(): UsePaymentProvidersResult {
const getApiToken = useApiToken();
const [status, setStatus] = React.useState<PaymentProvidersStatus>("loading");
const [providers, setProviders] = React.useState<readonly PaymentProvider[]>(
[],
);
const [errorMessage, setErrorMessage] = React.useState("");
const [reloadNonce, setReloadNonce] = React.useState(0);
React.useEffect(() => {
let cancelled = false;
const controller = new AbortController();
const load = async (): Promise<void> => {
setStatus("loading");
setErrorMessage("");
try {
const token = await getApiToken();
const enabled = await getProviders({
token,
signal: controller.signal,
});
if (cancelled) return;
setProviders(enabled);
setStatus("ready");
} catch (err) {
if (cancelled) return;
if (err instanceof DOMException && err.name === "AbortError") return;
setErrorMessage(messageFor(err));
setStatus("error");
}
};
void load();
return () => {
cancelled = true;
controller.abort();
};
}, [getApiToken, reloadNonce]);
const reload = React.useCallback(() => {
setReloadNonce((n) => n + 1);
}, []);
return { status, providers, errorMessage, reload };
}