From dafcad268fa4cbc626fc0ea44ed919a7d80ec933 Mon Sep 17 00:00:00 2001 From: kemuru <102478601+kemuru@users.noreply.github.com> Date: Tue, 29 Oct 2024 01:36:53 +0100 Subject: [PATCH] fix: memoize some values to prevent recalculations --- .../StakePanel/SimulatorPopup/index.tsx | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx b/web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx index 7f6a02e40..06481193e 100644 --- a/web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx +++ b/web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx @@ -150,18 +150,20 @@ const SimulatorPopup: React.FC = ({ amountToStake, isStaking }) const currentTreeDisputesPerPnk = foundCourt?.treeDisputesPerPnk; const currentTreeExpectedRewardPerPnk = foundCourt?.treeExpectedRewardPerPnk; - const totalVotes = - !isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeVotesPerPnk) - ? courtCurrentEffectiveStake * currentTreeVotesPerPnk - : undefined; - const totalCases = - !isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeDisputesPerPnk) - ? courtCurrentEffectiveStake * currentTreeDisputesPerPnk - : undefined; - const totalRewards = - !isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeExpectedRewardPerPnk) - ? courtCurrentEffectiveStake * currentTreeExpectedRewardPerPnk - : undefined; + const totals = useMemo(() => { + if (isUndefined(courtCurrentEffectiveStake)) return {}; + return { + votes: !isUndefined(currentTreeVotesPerPnk) ? courtCurrentEffectiveStake * currentTreeVotesPerPnk : undefined, + cases: !isUndefined(currentTreeDisputesPerPnk) + ? courtCurrentEffectiveStake * currentTreeDisputesPerPnk + : undefined, + rewards: !isUndefined(currentTreeExpectedRewardPerPnk) + ? courtCurrentEffectiveStake * currentTreeExpectedRewardPerPnk + : undefined, + }; + }, [courtCurrentEffectiveStake, currentTreeVotesPerPnk, currentTreeDisputesPerPnk, currentTreeExpectedRewardPerPnk]); + + const { votes: totalVotes, cases: totalCases, rewards: totalRewards } = totals; const courtFutureEffectiveStake = !isUndefined(courtCurrentEffectiveStake) ? Math.max(isStaking ? courtCurrentEffectiveStake + amountToStake : courtCurrentEffectiveStake - amountToStake, 0)