127 lines
3.9 KiB
TypeScript
127 lines
3.9 KiB
TypeScript
// * Packages
|
|
import { ReactNode, useEffect, useState } from "react";
|
|
import { InfoIcon } from "lucide-react";
|
|
// * Components shadcn
|
|
import { Button } from "@/components/ui/button";
|
|
// * Other
|
|
import useTrans from "@/hooks/use-translations";
|
|
|
|
declare global {
|
|
interface Window {
|
|
__CN_Recaptcha?: () => void;
|
|
__CN_Recaptcha_Reset?: () => void;
|
|
__CN_Recaptcha_Loaded?: () => void;
|
|
grecaptcha?: unknown;
|
|
WebCookieNotice?: {
|
|
// eslint-disable-next-line no-unused-vars
|
|
getCnCookieStatus: (key: string) => string;
|
|
};
|
|
}
|
|
}
|
|
|
|
export default function InitRecaptcha(forms: HTMLFormElement[]) {
|
|
if (!forms.length) return;
|
|
|
|
// Setup reCAPTCHA loader
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
window.__CN_Recaptcha = () => {
|
|
forms.forEach((form) => {
|
|
if (!form.querySelector(".r_captcha > div")) {
|
|
const newDiv = document.createElement("div");
|
|
newDiv.classList.add("g-recaptcha");
|
|
form.querySelector(".r_captcha")?.appendChild(newDiv);
|
|
}
|
|
// eslint-disable-next-line no-param-reassign
|
|
form.inert = true;
|
|
});
|
|
|
|
// Inject reCAPTCHA script
|
|
const script = document.createElement("script");
|
|
const { lang } = document.documentElement;
|
|
script.src = `https://www.google.com/recaptcha/api.js?render=explicit&onload=__CN_Recaptcha_Loaded&hl=${lang}`;
|
|
script.async = true;
|
|
document.head.appendChild(script);
|
|
};
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
window.__CN_Recaptcha_Reset = () => {
|
|
forms.forEach((form) => {
|
|
form.querySelector(".r_captcha > div")?.remove();
|
|
// eslint-disable-next-line no-param-reassign
|
|
form.inert = true;
|
|
});
|
|
};
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
window.__CN_Recaptcha_Loaded = () => {
|
|
forms.forEach((form) => {
|
|
// eslint-disable-next-line no-param-reassign
|
|
form.inert = false;
|
|
const target = form.querySelector(".r_captcha > div");
|
|
if (target) {
|
|
// @ts-expect-error Render exist on grecaptcha.
|
|
window.grecaptcha?.render(target, {
|
|
sitekey: form.dataset.recaptchaKey ?? "",
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
// Set recaptcha when cookie doesn't do it.
|
|
forms.forEach((form) => {
|
|
if (
|
|
window.WebCookieNotice?.getCnCookieStatus("recaptcha") === "true" &&
|
|
!form.querySelector(".r_captcha > div")
|
|
) {
|
|
// @ts-expect-error AcceptCookie exist on WebCookieNotice.
|
|
window.WebCookieNotice?.acceptCookie("recaptcha");
|
|
}
|
|
});
|
|
}
|
|
|
|
export function ActivateCookieRecaptcha(): ReactNode {
|
|
const [status, setStatus] = useState(true);
|
|
|
|
const titleTrans = useTrans("cookie-notice.custom.recaptcha.title");
|
|
const warningTrans = useTrans("cookie-notice.custom.recaptcha.warning");
|
|
const labelTrans = useTrans("cookie-notice.button_allow");
|
|
|
|
useEffect(() => {
|
|
const interval = setInterval(() => {
|
|
const currentStatus =
|
|
window.WebCookieNotice?.getCnCookieStatus("recaptcha") === "true";
|
|
setStatus((prev) => {
|
|
if (prev !== currentStatus) return currentStatus;
|
|
return prev;
|
|
});
|
|
}, 500);
|
|
|
|
return () => clearInterval(interval);
|
|
}, []);
|
|
|
|
const handleAccept = () => {
|
|
// @ts-expect-error AcceptCookie exist on WebCookieNotice.
|
|
window.WebCookieNotice?.acceptCookie("recaptcha");
|
|
setStatus(true);
|
|
};
|
|
|
|
if (status) return null;
|
|
|
|
return (
|
|
<div
|
|
data-cookie-name="recaptcha"
|
|
className="cn-cookie-activate flex flex-col rounded-lg border border-destructive/50 bg-card text-card-foreground shadow-sm gap-4 mb-4 p-6"
|
|
>
|
|
<p className="text-2xl font-semibold leading-none tracking-tight flex flex-row w-full items-start">
|
|
<InfoIcon className="mr-1" />
|
|
{titleTrans}
|
|
</p>
|
|
<div className="flex flex-row justify-between items-center">
|
|
<small className="text-success fw-normal fs-5">{warningTrans}</small>
|
|
<Button id="recaptcha-accept" type="button" onClick={handleAccept}>
|
|
{labelTrans}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|