caelum0x d56639a594
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
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

156 lines
5.5 KiB
TypeScript

"use client";
import { useEffect, useRef, useState } from "react";
import Script from "next/script";
type TurnstileWidgetId = string;
type TurnstileRenderOptions = {
sitekey: string;
action: string;
appearance?: "always" | "execute" | "interaction-only";
callback?: (token: string) => void;
"expired-callback"?: () => void;
"error-callback"?: () => void;
};
type TurnstileApi = {
render: (container: HTMLElement, options: TurnstileRenderOptions) => TurnstileWidgetId;
remove: (widgetId: TurnstileWidgetId) => void;
reset: (widgetId: TurnstileWidgetId) => void;
};
declare global {
interface Window {
turnstile?: TurnstileApi;
}
}
/** How long to wait before treating a never-rendered/never-resolved widget as
* blocked (ad blocker, privacy extension, or a corporate filter list that
* blocks `challenges.cloudflare.com`) rather than merely slow. */
const BLOCKED_AFTER_MS = 8_000;
export interface TurnstileProps {
readonly siteKey: string;
readonly action: string;
/** Fires with a real token once Turnstile passes this visitor (usually
* non-interactively -- see `appearance` below), or with `""` once this
* component gives up waiting (blocked widget, error, or no site key
* configured) so the caller can fall back to the server's own soft mode. */
readonly onToken: (token: string) => void;
}
/**
* Cloudflare Turnstile for the anonymous reel-preview box. Renders nothing
* (and reports an empty token immediately) when `siteKey` is blank -- the
* normal state until an operator creates a real widget (owner-only, see
* `.env.example`); the API's own per-IP rate limit is the abuse control in
* that mode. `appearance: "interaction-only"` keeps no visible placeholder
* on screen for the common case where Turnstile passes a visitor invisibly;
* a visible challenge only ever appears if Turnstile itself decides this
* specific visitor needs one. Mirrors the hardening history in
* `bidstage-tenders/components/Turnstile.tsx` (same real production dead
* ends: a widget that never renders, one that renders but never resolves,
* one whose token ages out) trimmed to what this box needs: no submit
* button to gate, just a token to hand the preview call once ready.
*/
export function Turnstile({ siteKey, action, onToken }: TurnstileProps) {
const containerRef = useRef<HTMLDivElement>(null);
const widgetId = useRef<TurnstileWidgetId | null>(null);
const settledRef = useRef(false);
const onTokenRef = useRef(onToken);
onTokenRef.current = onToken;
useEffect(() => {
if (!siteKey) {
onTokenRef.current("");
return;
}
settledRef.current = false;
function renderWidget(): void {
const container = containerRef.current;
if (!container || !window.turnstile || widgetId.current !== null) return;
let ownId: TurnstileWidgetId | null = null;
const isCurrent = () => ownId !== null && widgetId.current === ownId;
try {
const id = window.turnstile.render(container, {
sitekey: siteKey,
action,
appearance: "interaction-only",
callback: (token) => {
if (!isCurrent()) return;
settledRef.current = true;
onTokenRef.current(token);
},
"expired-callback": () => {
if (!isCurrent()) return;
settledRef.current = false;
window.turnstile?.reset(ownId as TurnstileWidgetId);
},
"error-callback": () => {
if (!isCurrent()) return;
settledRef.current = true;
onTokenRef.current("");
},
});
ownId = id;
widgetId.current = id;
} catch {
// A CSP violation or a privacy extension that neuters the API: don't
// leave the caller waiting forever for a token that will never come.
settledRef.current = true;
onTokenRef.current("");
}
}
renderWidget();
const timer = window.setTimeout(() => {
if (settledRef.current) return;
const hasVisibleChallenge = (containerRef.current?.querySelectorAll("iframe").length ?? 0) > 0;
if (!hasVisibleChallenge) {
// Rendered-but-never-resolved or never-rendered at all (blocked
// `challenges.cloudflare.com`): fall back rather than hang -- a
// widget that DID paint a real challenge is left alone past this
// timeout since a human may still be solving it.
settledRef.current = true;
onTokenRef.current("");
}
}, BLOCKED_AFTER_MS);
return () => {
window.clearTimeout(timer);
if (widgetId.current !== null && window.turnstile) {
window.turnstile.remove(widgetId.current);
widgetId.current = null;
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps -- re-render only on a new site key/action, not on every onToken identity change.
}, [siteKey, action]);
if (!siteKey) return null;
return (
<>
<Script
src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit"
strategy="afterInteractive"
/>
<div ref={containerRef} />
</>
);
}
/** True once a token (possibly empty, meaning "proceed in soft mode") exists. */
export function useTurnstileToken(siteKey: string): {
readonly token: string;
readonly ready: boolean;
readonly node: (action: string) => React.ReactNode;
} {
const [token, setToken] = useState<string | null>(null);
return {
token: token ?? "",
ready: !siteKey || token !== null,
node: (action: string) => <Turnstile siteKey={siteKey} action={action} onToken={setToken} />,
};
}