From e081d8ab5e3955bf17c7a06ce40cc0b2fde5ef1b Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Fri, 7 Feb 2025 10:50:23 +0100 Subject: [PATCH] refactor(auth): simplify continueWithEmail and remove useCallback (#10068) Refactored continueWithEmail to remove unnecessary dependencies and simplified the useIsMatchingLocation hook by eliminating useCallback. This reduces complexity and addresses potential infinite loop issues. --- .../src/hooks/useIsMatchingLocation.ts | 36 +++++++------------ .../auth/sign-in-up/hooks/useSignInUp.ts | 12 +------ 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/packages/twenty-front/src/hooks/useIsMatchingLocation.ts b/packages/twenty-front/src/hooks/useIsMatchingLocation.ts index 36f525f1f..631bd032a 100644 --- a/packages/twenty-front/src/hooks/useIsMatchingLocation.ts +++ b/packages/twenty-front/src/hooks/useIsMatchingLocation.ts @@ -3,36 +3,26 @@ import { matchPath, useLocation } from 'react-router-dom'; import { AppBasePath } from '@/types/AppBasePath'; import { isNonEmptyString } from '@sniptt/guards'; import { isDefined } from 'twenty-shared'; -import { useCallback } from 'react'; export const useIsMatchingLocation = () => { const location = useLocation(); - // Infinite loop issue caused by `checkUserExistsQuery` in `useSignInUp`. - // Without executing this query, there is no infinite loop. - // I also noticed that in `isMatchingLocation` inside `continueWithEmail`, no loop occurs. - // Both functions are called within the `useEffect` of `SignInUpWorkspaceScopeFormEffect`. - // This led me to conclude that the issue comes from `useIsMatchingLocation`. - // Using `useCallback` prevent the loop. - const isMatchingLocation = useCallback( - (path: string, basePath?: AppBasePath) => { - const addTrailingSlash = (path: string) => - path.endsWith('/') ? path : path + '/'; + const addTrailingSlash = (path: string) => + path.endsWith('/') ? path : path + '/'; - const getConstructedPath = (path: string, basePath?: AppBasePath) => { - if (!isNonEmptyString(basePath)) return path; + const getConstructedPath = (path: string, basePath?: AppBasePath) => { + if (!isNonEmptyString(basePath)) return path; - return addTrailingSlash(basePath) + path; - }; + return addTrailingSlash(basePath) + path; + }; - const match = matchPath( - getConstructedPath(path, basePath), - location.pathname, - ); - return isDefined(match); - }, - [location.pathname], - ); + const isMatchingLocation = (path: string, basePath?: AppBasePath) => { + const match = matchPath( + getConstructedPath(path, basePath), + location.pathname, + ); + return isDefined(match); + }; return { isMatchingLocation, diff --git a/packages/twenty-front/src/modules/auth/sign-in-up/hooks/useSignInUp.ts b/packages/twenty-front/src/modules/auth/sign-in-up/hooks/useSignInUp.ts index e54d10bcc..5314cfb24 100644 --- a/packages/twenty-front/src/modules/auth/sign-in-up/hooks/useSignInUp.ts +++ b/packages/twenty-front/src/modules/auth/sign-in-up/hooks/useSignInUp.ts @@ -45,17 +45,7 @@ export const useSignInUp = (form: UseFormReturn
) => { const continueWithEmail = useCallback(() => { requestFreshCaptchaToken(); setSignInUpStep(SignInUpStep.Email); - setSignInUpMode( - isMatchingLocation(AppPath.SignInUp) - ? SignInUpMode.SignIn - : SignInUpMode.SignUp, - ); - }, [ - isMatchingLocation, - requestFreshCaptchaToken, - setSignInUpMode, - setSignInUpStep, - ]); + }, [requestFreshCaptchaToken, setSignInUpStep]); const continueWithCredentials = useCallback(async () => { const token = await readCaptchaToken();