diff --git a/packages/page-referenda/src/Referenda/Votes.tsx b/packages/page-referenda/src/Referenda/Votes.tsx index 5ea36b0e0c..d930725c7a 100644 --- a/packages/page-referenda/src/Referenda/Votes.tsx +++ b/packages/page-referenda/src/Referenda/Votes.tsx @@ -1,8 +1,7 @@ // Copyright 2017-2022 @polkadot/app-referenda authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { u32 } from '@polkadot/types'; -import type { PalletConvictionVotingTally, PalletRankedCollectiveTally } from '@polkadot/types/lookup'; +import type { PalletConvictionVotingTally, PalletRankedCollectiveTally, PalletRankedCollectiveVoteRecord } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; import type { PalletVote } from '../types'; @@ -10,6 +9,7 @@ import React, { useCallback, useMemo } from 'react'; import { AddressMini, Expander } from '@polkadot/react-components'; import { FormatBalance } from '@polkadot/react-query'; +import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; import useVotes from './useVotes'; @@ -22,49 +22,61 @@ interface Props { tally: PalletConvictionVotingTally | PalletRankedCollectiveTally; } +function renderMini (list?: [string, BN][]): React.ReactNode[] | undefined { + return list + ?.sort(([, a], [, b]) => b.cmp(a)) + .map(([a]) => ( + + )); +} + +function extractVotes (votes: Record = {}): [[string, BN][]?, [string, BN][]?] { + const ayes: [string, BN][] = []; + const nays: [string, BN][] = []; + const entries = Object.entries(votes); + + for (let i = 0; i < entries.length; i++) { + const [accountId, vote] = entries[i]; + + if (vote.isAye) { + ayes.push([accountId, vote.asAye]); + } else { + nays.push([accountId, vote.asNay]); + } + } + + return [ + ayes.length ? ayes : undefined, + nays.length ? nays : undefined + ]; +} + function Votes ({ className = '', id, isConvictionVote, palletVote, tally }: Props): React.ReactElement { const { t } = useTranslation(); const votes = useVotes(palletVote, id, isConvictionVote); const [ayes, nays] = useMemo( - () => votes - ? Object.entries(votes).reduce<[[string, u32][], [string, u32][]]>(([ayes, nays], [accountId, vote]) => { - if (vote.isAye) { - ayes.push([accountId, vote.asAye]); - } else { - nays.push([accountId, vote.asNay]); - } - - return [ayes, nays]; - }, [[], []]) - : [], + () => extractVotes(votes), [votes] ); const renderAyes = useCallback( - () => ayes?.map(([a]) => ( - - )), + () => renderMini(ayes), [ayes] ); const renderNays = useCallback( - () => nays?.map(([a]) => ( - - )), + () => renderMini(nays), [nays] ); return ( ) - : t('Aye {{count}}', { replace: { count: tally.ayes.toNumber() } }) + : t('Aye {{count}}', { replace: { count: formatNumber(tally.ayes) } }) } /> ) - : t('Nay {{count}}', { replace: { count: tally.nays.toNumber() } }) + : t('Nay {{count}}', { replace: { count: formatNumber(tally.nays) } }) } />