From 848bf639a959f61267b25a6380949443ebc7e76b Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Fri, 9 Aug 2024 12:54:22 +0100 Subject: [PATCH] fix: sort by voting power --- .../governance/aggregates/voteHistory.ts | 35 ++++++++++--------- .../governance/utils/votingPowerSorting.ts | 4 +++ 2 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 src/renderer/features/governance/utils/votingPowerSorting.ts diff --git a/src/renderer/features/governance/aggregates/voteHistory.ts b/src/renderer/features/governance/aggregates/voteHistory.ts index 124f413e0a..75eeab4bb8 100644 --- a/src/renderer/features/governance/aggregates/voteHistory.ts +++ b/src/renderer/features/governance/aggregates/voteHistory.ts @@ -8,6 +8,7 @@ import { votingListService } from '../lib/votingListService'; import { networkSelectorModel } from '../model/networkSelector'; import { votingAssetModel } from '../model/votingAsset'; import { type AggregatedVoteHistory } from '../types/structs'; +import { votingPowerSorting } from '../utils/votingPowerSorting'; import { proposerIdentityAggregate } from './proposerIdentity'; import { tracksAggregate } from './tracks'; @@ -42,22 +43,24 @@ const $voteHistory = combine( for (const [referendumId, historyList] of Object.entries(history)) { const votes = votingService.getReferendumVoting(referendumId, voting); - acc[referendumId] = historyList.flatMap(({ voter }) => { - const proposer = proposers[voter] ?? null; - const vote = votes[voter]; - if (!vote) { - return []; - } - - const splitVotes = votingListService.getDecoupledVotesFromVote(referendumId, vote); - - return splitVotes.map((vote) => { - return { - ...vote, - name: proposer ? proposer.parent.name : null, - }; - }); - }); + acc[referendumId] = historyList + .flatMap(({ voter }) => { + const proposer = proposers[voter] ?? null; + const vote = votes[voter]; + if (!vote) { + return []; + } + + const splitVotes = votingListService.getDecoupledVotesFromVote(referendumId, vote); + + return splitVotes.map((vote) => { + return { + ...vote, + name: proposer ? proposer.parent.name : null, + }; + }); + }) + .sort(votingPowerSorting); } return acc; diff --git a/src/renderer/features/governance/utils/votingPowerSorting.ts b/src/renderer/features/governance/utils/votingPowerSorting.ts new file mode 100644 index 0000000000..076608fb94 --- /dev/null +++ b/src/renderer/features/governance/utils/votingPowerSorting.ts @@ -0,0 +1,4 @@ +import { type AggregatedVoteHistory } from '../types/structs'; + +export const votingPowerSorting = (a: AggregatedVoteHistory, b: AggregatedVoteHistory) => + b.votingPower.eq(a.votingPower) ? 0 : b.votingPower.gt(a.votingPower) ? 1 : -1;