Skip to content

Commit

Permalink
Revert "Start unload Script"
Browse files Browse the repository at this point in the history
This reverts commit 34d373c.
  • Loading branch information
FelixMalfait committed Apr 25, 2024
1 parent 34d373c commit 2d38128
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 86 deletions.
76 changes: 0 additions & 76 deletions packages/twenty-front/src/hooks/useCaptchaScript.ts

This file was deleted.

40 changes: 40 additions & 0 deletions packages/twenty-front/src/hooks/useInsertCaptchaScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useEffect } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';

import { isCaptchaLoadedState } from '@/auth/states/isCaptchaLoadedState';
import { captchaProviderState } from '@/client-config/states/captchaProviderState';
import { getCaptchaUrlByProvider } from '~/utils/captcha';

export const useInsertCaptchaScript = () => {
const captchaProvider = useRecoilValue(captchaProviderState);
const [isCaptchaLoaded, setIsCaptchaLoaded] =
useRecoilState(isCaptchaLoadedState);

useEffect(() => {
if (!captchaProvider?.provider || !captchaProvider.siteKey) {
return;
}

const scriptUrl = getCaptchaUrlByProvider(
captchaProvider.provider,
captchaProvider.siteKey,
);
if (!scriptUrl) {
return;
}

let scriptElement: HTMLScriptElement | null = document.querySelector(
`script[src="${scriptUrl}"]`,
);
if (!scriptElement) {
scriptElement = document.createElement('script');
scriptElement.src = scriptUrl;
scriptElement.onload = () => {
setIsCaptchaLoaded(true);
};
document.body.appendChild(scriptElement);
}
}, [captchaProvider?.provider, captchaProvider?.siteKey, setIsCaptchaLoaded]);

return isCaptchaLoaded;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useNavigateAfterSignInUp } from '@/auth/sign-in-up/hooks/useNavigateAft
import { Form } from '@/auth/sign-in-up/hooks/useSignInUpForm';
import { AppPath } from '@/types/AppPath';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { useCaptchaScript } from '~/hooks/useCaptchaScript';
import { useInsertCaptchaScript } from '~/hooks/useInsertCaptchaScript';
import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';

Expand Down Expand Up @@ -51,7 +51,7 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
checkUserExists: { checkUserExistsQuery },
} = useAuth();

const { loadCaptchaScript, unloadCaptchaScript } = useCaptchaScript();
const isCaptchaScriptLoaded = useInsertCaptchaScript();
const { generateCaptchaToken } = useGenerateCaptchaToken();

const [isGeneratingCaptchaToken, setIsGeneratingCaptchaToken] =
Expand All @@ -60,8 +60,9 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
const getCaptchaToken = useCallback(async () => {
setIsGeneratingCaptchaToken(true);
try {
const isCaptchaScriptLoaded = loadCaptchaScript();
console.log(isCaptchaScriptLoaded);
const captchaToken = await generateCaptchaToken(isCaptchaScriptLoaded);
console.log(captchaToken);
if (!isUndefinedOrNull(captchaToken)) {
form.setValue('captchaToken', captchaToken);
}
Expand All @@ -79,7 +80,7 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
} finally {
setIsGeneratingCaptchaToken(false);
}
}, [form, generateCaptchaToken, loadCaptchaScript, enqueueSnackBar]);
}, [form, generateCaptchaToken, isCaptchaScriptLoaded, enqueueSnackBar]);

const continueWithEmail = useCallback(() => {
setSignInUpStep(SignInUpStep.Email);
Expand Down Expand Up @@ -139,7 +140,6 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
data.captchaToken,
);

unloadCaptchaScript();
navigateAfterSignInUp(currentWorkspace, currentWorkspaceMember);
} catch (err: any) {
enqueueSnackBar(err?.message, {
Expand All @@ -155,7 +155,6 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
workspaceInviteHash,
navigateAfterSignInUp,
enqueueSnackBar,
unloadCaptchaScript,
],
);

Expand Down
9 changes: 5 additions & 4 deletions packages/twenty-front/src/utils/captcha.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CaptchaDriverType } from '~/generated-metadata/graphql';

export const getCaptchaUrlByProvider = (
name: CaptchaDriverType,
siteKey: string,
) => {
export const getCaptchaUrlByProvider = (name: string, siteKey: string) => {
if (!name) {
return '';
}

switch (name) {
case CaptchaDriverType.GoogleRecatpcha:
return `https://www.google.com/recaptcha/api.js?render=${siteKey}`;
Expand Down

0 comments on commit 2d38128

Please sign in to comment.