Skip to content

Commit

Permalink
Add memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Sep 5, 2024
1 parent c844892 commit 71b6102
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/components/Setup/SetupProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { useRouter } from 'next/router';
import React, { ReactNode, createContext, useContext, useEffect } from 'react';
import React, {
ReactNode,
createContext,
useContext,
useEffect,
useMemo,
} from 'react';
import { UserSetupStageEnum } from 'src/graphql/types.generated';
import { useSetupStageQuery } from './Setup.generated';

Expand Down Expand Up @@ -49,11 +55,17 @@ export const SetupProvider: React.FC<Props> = ({ children }) => {
}
}, [data]);

const settingUp = data
? data.userOptions.some(
const settingUp = useMemo(() => {
if (!data) {
return undefined;
}

return (
data.userOptions.some(
(option) => option.key === 'setup_position' && option.value !== '',
) || data.user.setup !== null
: undefined;
);
}, [data]);

return (
<SetupContext.Provider value={{ settingUp }}>
Expand Down
5 changes: 3 additions & 2 deletions src/components/Setup/useNextSetupPage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useRouter } from 'next/router';
import { useCallback } from 'react';
import { UserSetupStageEnum } from 'src/graphql/types.generated';
import { useUpdateUserOptionsMutation } from '../Contacts/ContactFlow/ContactFlowSetup/UpdateUserOptions.generated';
import { useSetupStageLazyQuery } from './Setup.generated';
Expand All @@ -21,7 +22,7 @@ export const useNextSetupPage = (): UseNextSetupPageResult => {
},
});

const next = async () => {
const next = useCallback(async () => {
const { data } = await getSetupStage();
switch (data?.user.setup) {
case UserSetupStageEnum.NoAccountLists:
Expand All @@ -40,7 +41,7 @@ export const useNextSetupPage = (): UseNextSetupPageResult => {
);
return;
}
};
}, []);

return {
next,
Expand Down

0 comments on commit 71b6102

Please sign in to comment.