import { usePage } from "@inertiajs/react"; import { InfoIcon } from "lucide-react"; import type React from "react"; import { type SubmitEvent } from "react"; import { Button } from "@/components/ui/button"; import { cnServices } from "@/front/Components/cookie/constants"; import InputError from "@/front/Components/cookie/InputError"; import { useRecaptcha } from "@/front/Components/cookie/recaptcha/RecaptchaProvider"; import { type CookieEnv } from "@/front/types/cookie"; import useCookieNotice from "@/hooks/use-cookie-notice"; import useTrans from "@/hooks/use-translations"; import { cn } from "@/lib/utils"; interface CustomData { "g-recaptcha-response"?: string; } interface BannerClassName { classNames?: { wrapperClassName?: string; btnClassName?: string; }; } interface Props { onSubmit: (e: SubmitEvent) => void; data: TFormData & CustomData; children: React.ReactNode; footer?: React.ReactNode; banner?: BannerClassName; classNames?: { wrapperClassName?: string; formClassName?: string; }; } function RecaptchaNotice({ banner }: { banner?: BannerClassName }) { const { enabled } = useRecaptcha(); const title = useTrans("cookie-notice.custom.recaptcha.title"); const warning = useTrans("cookie-notice.custom.recaptcha.warning"); const label = useTrans("cookie-notice.custom.recaptcha.accept"); const { accept } = useCookieNotice(cnServices.RECAPTCHA); if (enabled) return null; return (

{title}

{warning}
); } export default function RecaptchaForm({ onSubmit, classNames, banner, data, children, footer, }: Props) { const { getToken, formRef, enabled } = useRecaptcha(); const { errors } = usePage().props; const { recaptchaKey, recaptchaBypassKey } = usePage().props .cookieEnv as CookieEnv; const { mode } = usePage().props.appEnv as AppEnv; const isTestingEnv = mode === "testing"; const formIsEnabled = isTestingEnv || enabled; const handleSubmit = (e: SubmitEvent) => { e.preventDefault(); if (!formIsEnabled) return; const token = getToken(); // eslint-disable-next-line react-hooks/immutability data["g-recaptcha-response"] = isTestingEnv ? recaptchaBypassKey : token; onSubmit(e); }; return (
{children} {formIsEnabled && ( <>
)} {footer}
); }