Skip to content

Commit c60c833

Browse files
committed
Fix: memoize finalize step transform to avoid pending button getting stuck
1 parent 24c780c commit c60c833

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/FinalizeStep.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,6 @@ const FinalizeStep: FC<FinalizeStepProps> = ({
101101
}
102102
}, [isMotionAgreement, isMotionFinalized, actionData, refetchColony]);
103103

104-
let action = {
105-
actionType: ActionTypes.MOTION_FINALIZE,
106-
transform: finalizePayload,
107-
onSuccess: handleSuccess,
108-
};
109-
if (isMotionClaimable) {
110-
action = {
111-
actionType: ActionTypes.MOTION_CLAIM,
112-
transform: claimPayload,
113-
onSuccess: handleClaimSuccess,
114-
};
115-
}
116-
117104
/*
118105
* @NOTE This is just needed until we properly save motion data in the db
119106
* For now, we just fetch it live from chain, so when we uninstall the extension
@@ -168,7 +155,15 @@ const FinalizeStep: FC<FinalizeStepProps> = ({
168155
{
169156
key: FinalizeStepSections.Finalize,
170157
content: (
171-
<ActionForm {...action} onSuccess={handleSuccess}>
158+
<ActionForm
159+
actionType={
160+
isMotionClaimable
161+
? ActionTypes.MOTION_CLAIM
162+
: ActionTypes.MOTION_FINALIZE
163+
}
164+
transform={isMotionClaimable ? claimPayload : finalizePayload}
165+
onSuccess={isMotionClaimable ? handleClaimSuccess : handleSuccess}
166+
>
172167
{({ formState: { isSubmitting } }) => (
173168
<>
174169
{items.length > 0 && (

src/components/v5/common/ActionSidebar/partials/Motions/steps/FinalizeStep/hooks.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import useExtensionData from '~hooks/useExtensionData.ts';
1212
import { type ClaimMotionRewardsPayload } from '~redux/sagas/motions/claimMotionRewards.ts';
1313
import { type MotionFinalizePayload } from '~redux/types/actions/motion.ts';
1414
import Numeral from '~shared/Numeral/index.ts';
15-
import { type InstalledExtensionData } from '~types/extensions.ts';
1615
import { type MotionAction } from '~types/motions.ts';
1716
import { mapPayload } from '~utils/actions.ts';
1817
import { getIsMotionOlderThanAWeek } from '~utils/dates.ts';
@@ -66,13 +65,17 @@ export const useFinalizeStep = (actionData: MotionAction) => {
6665
const isFinalizable =
6766
hasEnoughFundsToFinalize && !motionStateHistory.hasFailedNotFinalizable;
6867

69-
const transform = mapPayload(
70-
(): MotionFinalizePayload => ({
71-
colonyAddress,
72-
userAddress: user?.walletAddress || '',
73-
motionId,
74-
canMotionFail: isMotionOlderThanWeek,
75-
}),
68+
const transform = useMemo(
69+
() =>
70+
mapPayload(
71+
(): MotionFinalizePayload => ({
72+
colonyAddress,
73+
userAddress: user?.walletAddress || '',
74+
motionId,
75+
canMotionFail: isMotionOlderThanWeek,
76+
}),
77+
),
78+
[colonyAddress, isMotionOlderThanWeek, motionId, user?.walletAddress],
7679
);
7780

7881
return {
@@ -102,13 +105,11 @@ export const useClaimConfig = (
102105
const {
103106
colony: { colonyAddress, nativeToken, motionsWithUnclaimedStakes },
104107
} = useColonyContext();
105-
const extension = useExtensionData(Extension.VotingReputation);
108+
const { extensionData } = useExtensionData(Extension.VotingReputation);
106109
const { pollLockedTokenBalance } = useUserTokenBalanceContext();
107110

108111
const [isClaimed, setIsClaimed] = useState(false);
109112

110-
const extensionData = extension?.extensionData as InstalledExtensionData;
111-
112113
const userAddress = user?.walletAddress;
113114
const nativeTokenDecimals = nativeToken.decimals;
114115
const nativeTokenSymbol = ` ${nativeToken.symbol}`;
@@ -202,15 +203,20 @@ export const useClaimConfig = (
202203
pollLockedTokenBalance();
203204
};
204205

205-
const claimPayload = mapPayload(
206-
(): ClaimMotionRewardsPayload => ({
207-
userAddress: userAddress || '',
208-
colonyAddress: colonyAddress || '',
209-
transactionHash: transactionHash || '',
210-
extensionAddress: isInstalledExtensionData(extensionData)
211-
? extensionData.address
212-
: ADDRESS_ZERO,
213-
}),
206+
const claimPayload = useMemo(
207+
() =>
208+
mapPayload(
209+
(): ClaimMotionRewardsPayload => ({
210+
userAddress: userAddress || '',
211+
colonyAddress: colonyAddress || '',
212+
transactionHash: transactionHash || '',
213+
extensionAddress:
214+
extensionData && isInstalledExtensionData(extensionData)
215+
? extensionData.address
216+
: ADDRESS_ZERO,
217+
}),
218+
),
219+
[userAddress, colonyAddress, transactionHash, extensionData],
214220
);
215221

216222
const getDescriptionItems = (): DescriptionListItem[] => {

0 commit comments

Comments
 (0)