Skip to content

Commit aa75c13

Browse files
committed
Refactor PercentageValue component
1 parent 4fe4e2c commit aa75c13

File tree

9 files changed

+18
-42
lines changed

9 files changed

+18
-42
lines changed

.changelog/2404.bugfix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fix voting power percentage value formatting
1+
Refactor PercentageValue component

src/app/components/PercentageValue/index.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ import { useTranslation } from 'react-i18next'
33
import { calculatePercentage } from '../../utils/number-utils'
44

55
type PercentageValueProps = {
6-
adaptMaximumFractionDigits?: boolean
7-
total?: number | string
86
value: number | string | undefined
9-
maximumFractionDigits?: number
10-
threshold?: number
11-
fractionDigitsForSmallValues?: number
7+
total?: number | string
8+
fractionDigits?: number
9+
adaptive?: boolean
1210
}
1311

1412
export const PercentageValue: FC<PercentageValueProps> = ({
15-
maximumFractionDigits = 0,
16-
adaptMaximumFractionDigits = false,
1713
value,
1814
total = 100,
19-
threshold = 0.001,
20-
fractionDigitsForSmallValues = 3,
15+
fractionDigits = 2,
16+
adaptive = false,
2117
}) => {
2218
const { t } = useTranslation()
2319

@@ -27,17 +23,17 @@ export const PercentageValue: FC<PercentageValueProps> = ({
2723
return null
2824
}
2925

26+
// Show 3 fraction digits for very small percentages (< 0.01%)
27+
const maximumFractionDigits = adaptive && percentageValue < 0.0001 ? 3 : fractionDigits
28+
3029
return (
3130
<>
3231
{t('common.valuePair', {
3332
value: percentageValue,
3433
formatParams: {
3534
value: {
3635
style: 'percent',
37-
maximumFractionDigits:
38-
adaptMaximumFractionDigits && percentageValue < threshold
39-
? fractionDigitsForSmallValues
40-
: maximumFractionDigits,
36+
maximumFractionDigits,
4137
} satisfies Intl.NumberFormatOptions,
4238
},
4339
})}

src/app/components/UptimeStatus/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ export const UptimeStatus: FC<UptimeStatusProps> = ({ uptime, small }) => {
5555
))}
5656
</div>
5757
{uptime?.window_uptime && uptime?.window_length && (
58-
<PercentageValue
59-
value={(uptime.window_uptime / uptime.window_length) * 100}
60-
maximumFractionDigits={1}
61-
/>
58+
<PercentageValue value={(uptime.window_uptime / uptime.window_length) * 100} fractionDigits={1} />
6259
)}
6360
</div>
6461
)

src/app/components/Validators/ValidatorCommission.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ type ValidatorCommissionProps = {
66
}
77

88
export const ValidatorCommission: FC<ValidatorCommissionProps> = ({ commission }) => {
9-
return <PercentageValue value={commission} total={100000} />
9+
return <PercentageValue value={commission} total={100000} fractionDigits={0} />
1010
}

src/app/components/Validators/ValidatorCumulativeVoting.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const ValidatorCumulativeVoting: FC<ValidatorCumulativeVotingProps> = ({
3030
}}
3131
/>
3232
<span className="relative z-10">
33-
<PercentageValue value={value} total={total} />
33+
<PercentageValue value={value} total={total} fractionDigits={0} />
3434
</span>
3535
</div>
3636
)

src/app/components/Validators/index.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,8 @@ export const Validators: FC<ValidatorsProps> = ({ isLoading, limit, pagination,
7777
{
7878
align: TableCellAlign.Right,
7979
content: (
80-
<PercentageValue
81-
value={validator.voting_power}
82-
total={stats?.total_voting_power}
83-
adaptMaximumFractionDigits
84-
threshold={0.01}
85-
fractionDigitsForSmallValues={2}
86-
/>
80+
<PercentageValue value={validator.voting_power} total={stats?.total_voting_power} adaptive />
8781
),
88-
8982
key: 'voting',
9083
},
9184
{

src/app/pages/ConsensusDashboardPage/SnapshotEpoch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const SnapshotEpoch: FC<{ scope: SearchScope }> = ({ scope }) => {
6868
<PercentageValue
6969
value={completedBlocksInCurrentEpoch}
7070
total={epochDiffHeight}
71-
maximumFractionDigits={1}
71+
fractionDigits={1}
7272
/>
7373
)
7474
</>

src/app/pages/ValidatorDetailsPage/VotingPowerCard.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator, stats })
3636
max={stats.total_voting_power}
3737
label={
3838
<span className="font-bold">
39-
<PercentageValue
40-
adaptMaximumFractionDigits
41-
value={validator.voting_power}
42-
total={stats.total_voting_power}
43-
maximumFractionDigits={2}
44-
/>
39+
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} adaptive />
4540
</span>
4641
}
4742
/>

src/app/pages/ValidatorDetailsPage/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ export const ValidatorDetailsView: FC<{
239239
<PercentageValue
240240
value={validator.voting_power}
241241
total={stats.total_voting_power}
242-
adaptMaximumFractionDigits
243-
threshold={0.01}
242+
adaptive
244243
/>
245244
&nbsp; ({new BigNumber(validator.voting_power).toFormat()})
246245
</>
@@ -311,11 +310,7 @@ export const ValidatorDetailsView: FC<{
311310
</dd>
312311
<dt>{t('validator.voting')}</dt>
313312
<dd>
314-
<PercentageValue
315-
value={validator.voting_power}
316-
total={stats?.total_voting_power}
317-
adaptMaximumFractionDigits
318-
/>
313+
<PercentageValue value={validator.voting_power} total={stats?.total_voting_power} adaptive />
319314
</dd>
320315
<dt>{t('common.staked')}</dt>
321316
<dd>

0 commit comments

Comments
 (0)