Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor useEffect logic to simplify impersonation mode check #2199

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/features/common/Layout/UserPropsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ export const UserPropsProvider: FC = ({ children }) => {
}, [token, refetchUserData]);

React.useEffect(() => {
Copy link
Collaborator

@mohitb35 mohitb35 Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As impersonationModeOn is a dependency, and we are calling setIsImpersonationModeOn within the useEffect, unnecessary calls will be made to setIsImpersonationModeOn (at least 2 times while loading impersonation mode initially from local storage), and in the worst case we could end up in a loop.

Could we change this to:

React.useEffect(() => {
  const impersonationData = localStorage.getItem('impersonationData');
  if (impersonationData !== null && !isImpersonationModeOn) {
    setIsImpersonationModeOn(true);
  } else if (impersonationData === null && isImpersonationModeOn) {
    setIsImpersonationModeOn(false);
  }
}, []);

Would removing the dependency cause an issue?

if (localStorage.getItem('impersonationData') !== null) {
setIsImpersonationModeOn(true);
}
const impersonationData = localStorage.getItem('impersonationData');
setIsImpersonationModeOn(impersonationData !== null);
}, [isImpersonationModeOn]);

const value: UserPropsContextInterface | null = {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/apiRequests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function getAuthenticatedRequest<T>(
resolve(await res.json());
}
} else {
localStorage.removeItem('impersonationData');
logoutUser();
throw new ClientError(INVALID_TOKEN_STATUS_CODE, {
error_type: 'token_expired',
Expand Down Expand Up @@ -168,6 +169,7 @@ export function postAuthenticatedRequest<T>(
resolve(await res.json());
}
} else {
localStorage.removeItem('impersonationData');
logoutUser();
throw new ClientError(INVALID_TOKEN_STATUS_CODE, {
error_type: 'token_expired',
Expand Down Expand Up @@ -250,6 +252,7 @@ export function deleteAuthenticatedRequest<T>(
resolve(await res.json());
}
} else {
localStorage.removeItem('impersonationData');
logoutUser();
throw new ClientError(INVALID_TOKEN_STATUS_CODE, {
error_type: 'token_expired',
Expand Down Expand Up @@ -298,6 +301,7 @@ export function putAuthenticatedRequest<T>(
resolve(await res.json());
}
} else {
localStorage.removeItem('impersonationData');
logoutUser();
throw new ClientError(INVALID_TOKEN_STATUS_CODE, {
error_type: 'token_expired',
Expand Down
Loading