import { createContext, use } from "react"; export type SocialProvider = "github" | "google"; // Public client config served by /api/config. The worker fails closed // with 503 when Turnstile is not configured; consumers see an explicit // `unavailable` state so they can render an actionable error instead of // an indefinite loading spinner. export type ClientConfig = | { status: "ok"; turnstileSiteKey: string; issuer: string; socialProviders: SocialProvider[]; operatorName: string; operatorContactEmail: string; } | { status: "unavailable" }; // `null` while the initial fetch is in flight; resolves to the // ClientConfig union once /api/config has answered. Consumers that need // to render before resolution (sign-in, invite-accept) handle the // `null` case by falling back to a loading-state placeholder. export type ClientConfigState = ClientConfig | null; // Created in this stable .ts module so Vite Fast Refresh of the Provider // component does not recreate the context instance and orphan consumers. export const ClientConfigContext = createContext(null); // Returns: // - `null` while the initial /api/config fetch is in flight // - `{ status: "ok", ... }` when the worker answered with a usable payload // - `{ status: "unavailable" }` when /api/config returned 503 or the // fetch threw export const useClientConfig = (): ClientConfigState => use(ClientConfigContext);