From c60c83336de294e26a879e9ad306f0c23a16631b Mon Sep 17 00:00:00 2001 From: Jakub Zajac Date: Mon, 14 Oct 2024 22:45:16 +0100 Subject: [PATCH] Fix: memoize finalize step transform to avoid pending button getting stuck --- .../steps/FinalizeStep/FinalizeStep.tsx | 23 ++++------ .../Motions/steps/FinalizeStep/hooks.tsx | 46 +++++++++++-------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/FinalizeStep.tsx b/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/FinalizeStep.tsx index 6db33e517b..d4ff678c73 100644 --- a/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/FinalizeStep.tsx +++ b/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/FinalizeStep.tsx @@ -101,19 +101,6 @@ const FinalizeStep: FC = ({ } }, [isMotionAgreement, isMotionFinalized, actionData, refetchColony]); - let action = { - actionType: ActionTypes.MOTION_FINALIZE, - transform: finalizePayload, - onSuccess: handleSuccess, - }; - if (isMotionClaimable) { - action = { - actionType: ActionTypes.MOTION_CLAIM, - transform: claimPayload, - onSuccess: handleClaimSuccess, - }; - } - /* * @NOTE This is just needed until we properly save motion data in the db * For now, we just fetch it live from chain, so when we uninstall the extension @@ -168,7 +155,15 @@ const FinalizeStep: FC = ({ { key: FinalizeStepSections.Finalize, content: ( - + {({ formState: { isSubmitting } }) => ( <> {items.length > 0 && ( diff --git a/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/hooks.tsx b/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/hooks.tsx index bdc91b6438..8eeaf03629 100644 --- a/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/hooks.tsx +++ b/src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/hooks.tsx @@ -12,7 +12,6 @@ import useExtensionData from '~hooks/useExtensionData.ts'; import { type ClaimMotionRewardsPayload } from '~redux/sagas/motions/claimMotionRewards.ts'; import { type MotionFinalizePayload } from '~redux/types/actions/motion.ts'; import Numeral from '~shared/Numeral/index.ts'; -import { type InstalledExtensionData } from '~types/extensions.ts'; import { type MotionAction } from '~types/motions.ts'; import { mapPayload } from '~utils/actions.ts'; import { getIsMotionOlderThanAWeek } from '~utils/dates.ts'; @@ -66,13 +65,17 @@ export const useFinalizeStep = (actionData: MotionAction) => { const isFinalizable = hasEnoughFundsToFinalize && !motionStateHistory.hasFailedNotFinalizable; - const transform = mapPayload( - (): MotionFinalizePayload => ({ - colonyAddress, - userAddress: user?.walletAddress || '', - motionId, - canMotionFail: isMotionOlderThanWeek, - }), + const transform = useMemo( + () => + mapPayload( + (): MotionFinalizePayload => ({ + colonyAddress, + userAddress: user?.walletAddress || '', + motionId, + canMotionFail: isMotionOlderThanWeek, + }), + ), + [colonyAddress, isMotionOlderThanWeek, motionId, user?.walletAddress], ); return { @@ -102,13 +105,11 @@ export const useClaimConfig = ( const { colony: { colonyAddress, nativeToken, motionsWithUnclaimedStakes }, } = useColonyContext(); - const extension = useExtensionData(Extension.VotingReputation); + const { extensionData } = useExtensionData(Extension.VotingReputation); const { pollLockedTokenBalance } = useUserTokenBalanceContext(); const [isClaimed, setIsClaimed] = useState(false); - const extensionData = extension?.extensionData as InstalledExtensionData; - const userAddress = user?.walletAddress; const nativeTokenDecimals = nativeToken.decimals; const nativeTokenSymbol = ` ${nativeToken.symbol}`; @@ -202,15 +203,20 @@ export const useClaimConfig = ( pollLockedTokenBalance(); }; - const claimPayload = mapPayload( - (): ClaimMotionRewardsPayload => ({ - userAddress: userAddress || '', - colonyAddress: colonyAddress || '', - transactionHash: transactionHash || '', - extensionAddress: isInstalledExtensionData(extensionData) - ? extensionData.address - : ADDRESS_ZERO, - }), + const claimPayload = useMemo( + () => + mapPayload( + (): ClaimMotionRewardsPayload => ({ + userAddress: userAddress || '', + colonyAddress: colonyAddress || '', + transactionHash: transactionHash || '', + extensionAddress: + extensionData && isInstalledExtensionData(extensionData) + ? extensionData.address + : ADDRESS_ZERO, + }), + ), + [userAddress, colonyAddress, transactionHash, extensionData], ); const getDescriptionItems = (): DescriptionListItem[] => {