fix(twenty-front): error on captcha initialisation (#11039)

This commit is contained in:
Antoine Moreaux
2025-03-19 17:09:35 +01:00
committed by GitHub
parent cfdb3f5778
commit 28028ca4c0
6 changed files with 72 additions and 69 deletions

View File

@ -2,9 +2,12 @@ import React from 'react';
import { CaptchaProviderScriptLoaderEffect } from '@/captcha/components/CaptchaProviderScriptLoaderEffect';
import { isCaptchaRequiredForPath } from '@/captcha/utils/isCaptchaRequiredForPath';
import { useLocation } from 'react-router-dom';
export const CaptchaProvider = ({ children }: React.PropsWithChildren) => {
if (!isCaptchaRequiredForPath(window.location.pathname)) {
const location = useLocation();
if (!isCaptchaRequiredForPath(location.pathname)) {
return <>{children}</>;
}

View File

@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRequestFreshCaptchaToken } from '@/captcha/hooks/useRequestFreshCaptchaToken';
import { isCaptchaScriptLoadedState } from '@/captcha/states/isCaptchaScriptLoadedState';
@ -10,7 +10,7 @@ import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
export const CaptchaProviderScriptLoaderEffect = () => {
const captcha = useRecoilValue(captchaState);
const setIsCaptchaScriptLoaded = useSetRecoilState(
const [isCaptchaScriptLoaded, setIsCaptchaScriptLoaded] = useRecoilState(
isCaptchaScriptLoadedState,
);
const { requestFreshCaptchaToken } = useRequestFreshCaptchaToken();
@ -46,8 +46,9 @@ export const CaptchaProviderScriptLoaderEffect = () => {
document.body.appendChild(scriptElement);
}
}, [captcha?.provider, captcha?.siteKey, setIsCaptchaScriptLoaded]);
useEffect(() => {
if (isUndefinedOrNull(captcha?.provider)) {
if (isUndefinedOrNull(captcha?.provider) || !isCaptchaScriptLoaded) {
return;
}
@ -68,7 +69,7 @@ export const CaptchaProviderScriptLoaderEffect = () => {
}
return () => clearInterval(refreshInterval);
}, [captcha?.provider, requestFreshCaptchaToken]);
}, [captcha?.provider, requestFreshCaptchaToken, isCaptchaScriptLoaded]);
return <></>;
};