fix(auth): prevent infinite redirect (#10065)
Refactored the isMatchingLocation function to use useCallback for memoization, improving performance by avoiding unnecessary re-creations. Moved addTrailingSlash and getConstructedPath inside useCallback for better encapsulation.
This commit is contained in:
@ -3,28 +3,36 @@ import { matchPath, useLocation } from 'react-router-dom';
|
|||||||
import { AppBasePath } from '@/types/AppBasePath';
|
import { AppBasePath } from '@/types/AppBasePath';
|
||||||
import { isNonEmptyString } from '@sniptt/guards';
|
import { isNonEmptyString } from '@sniptt/guards';
|
||||||
import { isDefined } from 'twenty-shared';
|
import { isDefined } from 'twenty-shared';
|
||||||
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
export const useIsMatchingLocation = () => {
|
export const useIsMatchingLocation = () => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
const addTrailingSlash = (path: string) =>
|
// Infinite loop issue caused by `checkUserExistsQuery` in `useSignInUp`.
|
||||||
path.endsWith('/') ? path : path + '/';
|
// 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 getConstructedPath = (path: string, basePath?: AppBasePath) => {
|
const getConstructedPath = (path: string, basePath?: AppBasePath) => {
|
||||||
if (!isNonEmptyString(basePath)) return path;
|
if (!isNonEmptyString(basePath)) return path;
|
||||||
|
|
||||||
return addTrailingSlash(basePath) + path;
|
return addTrailingSlash(basePath) + path;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isMatchingLocation = (path: string, basePath?: AppBasePath) => {
|
const match = matchPath(
|
||||||
const match = matchPath(
|
getConstructedPath(path, basePath),
|
||||||
getConstructedPath(path, basePath),
|
location.pathname,
|
||||||
location.pathname,
|
);
|
||||||
);
|
return isDefined(match);
|
||||||
const isMatching = isDefined(match);
|
},
|
||||||
|
[location.pathname],
|
||||||
return isMatching;
|
);
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isMatchingLocation,
|
isMatchingLocation,
|
||||||
|
|||||||
Reference in New Issue
Block a user