From 2d926c0ce05ca18b59b8d2a52d7033d935630846 Mon Sep 17 00:00:00 2001 From: Art Date: Wed, 29 Nov 2023 19:38:41 +0100 Subject: [PATCH] fix(governance): sensible vote numbers (#5384) Co-authored-by: Madalina Raicu --- .../vote-breakdown/vote-breakdown.spec.tsx | 21 +++++++++++- .../vote-breakdown/vote-breakdown.tsx | 33 ++++++++++--------- libs/react-helpers/src/lib/format/number.tsx | 4 ++- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.spec.tsx b/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.spec.tsx index 623c81f67..efa63fb27 100644 --- a/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.spec.tsx +++ b/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.spec.tsx @@ -8,7 +8,7 @@ import { networkParamsQueryMock, nextWeek, } from '../../test-helpers/mocks'; -import { VoteBreakdown } from './vote-breakdown'; +import { CompactVotes, VoteBreakdown } from './vote-breakdown'; import type { ProposalQuery } from '../../proposal/__generated__/Proposal'; import type { MockedResponse } from '@apollo/client/testing'; import { @@ -346,3 +346,22 @@ describe('VoteBreakdown', () => { expect(style.width).toBe(`${expectedProgress}%`); }); }); + +describe('CompactVotes', () => { + it.each([ + [0, '0'], + [1, '1'], + [12, '12'], + [123, '123'], + [1234, '1.2K'], + [12345, '12.3K'], + [123456, '123.5K'], + [1234567, '1.2M'], + [12345678, '12.3M'], + [123456789, '123.5M'], + [1234567890, '1.2B'], + ])('compacts %s to %s', (input, output) => { + const { getByTestId } = render(); + expect(getByTestId('compact-number').textContent).toBe(output); + }); +}); diff --git a/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.tsx b/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.tsx index 121ce0800..7e8ae8c9c 100644 --- a/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.tsx +++ b/apps/governance/src/routes/proposals/components/vote-breakdown/vote-breakdown.tsx @@ -3,11 +3,21 @@ import BigNumber from 'bignumber.js'; import { useTranslation } from 'react-i18next'; import { useVoteInformation } from '../../hooks'; import { Icon, Tooltip } from '@vegaprotocol/ui-toolkit'; -import { formatNumber, toBigNum } from '@vegaprotocol/utils'; +import { formatNumber } from '@vegaprotocol/utils'; import { ProposalState } from '@vegaprotocol/types'; import type { ReactNode } from 'react'; import type { ProposalFieldsFragment } from '../../proposals/__generated__/Proposals'; import type { ProposalQuery } from '../../proposal/__generated__/Proposal'; +import { CompactNumber } from '@vegaprotocol/react-helpers'; + +export const CompactVotes = ({ number }: { number: BigNumber }) => ( + +); interface VoteBreakdownProps { proposal: ProposalFieldsFragment | ProposalQuery['proposal']; @@ -198,10 +208,7 @@ export const VoteBreakdown = ({ proposal }: VoteBreakdownProps) => { )} > @@ -226,10 +233,7 @@ export const VoteBreakdown = ({ proposal }: VoteBreakdownProps) => { )} > @@ -279,10 +283,7 @@ export const VoteBreakdown = ({ proposal }: VoteBreakdownProps) => { )} > @@ -321,7 +322,7 @@ export const VoteBreakdown = ({ proposal }: VoteBreakdownProps) => { {t('tokenVotesFor')}: @@ -341,7 +342,7 @@ export const VoteBreakdown = ({ proposal }: VoteBreakdownProps) => { {t('tokenVotesAgainst')}: @@ -384,7 +385,7 @@ export const VoteBreakdown = ({ proposal }: VoteBreakdownProps) => { {t('totalTokensVoted')}: diff --git a/libs/react-helpers/src/lib/format/number.tsx b/libs/react-helpers/src/lib/format/number.tsx index 299d03c52..b9efb0e9d 100644 --- a/libs/react-helpers/src/lib/format/number.tsx +++ b/libs/react-helpers/src/lib/format/number.tsx @@ -22,11 +22,13 @@ export const CompactNumber = ({ decimals = 'infer', compactDisplay = 'short', testId = 'compact-number', + compactAbove = DEFAULT_COMPACT_ABOVE, }: { number: BigNumber; decimals?: number | 'infer'; compactDisplay?: 'short' | 'long'; testId?: string; + compactAbove?: number; }) => { if (!number.isFinite()) { return ( @@ -44,7 +46,7 @@ export const CompactNumber = ({ return {INFINITY}; } - if (number.isLessThan(DEFAULT_COMPACT_ABOVE)) { + if (number.isLessThan(compactAbove)) { return ( {formatNumber(number, decimalPlaces)} );