Skip to content

Commit 66bd518

Browse files
committed
Refactor PercentageValue component
1 parent 6d8dd76 commit 66bd518

9 files changed

Lines changed: 21 additions & 32 deletions

File tree

.changelog/2404.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor PercentageValue component

src/app/components/PercentageValue/index.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +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
7+
total?: number | string
8+
fractionDigits?: number
9+
adaptive?: boolean
1010
}
1111

1212
export const PercentageValue: FC<PercentageValueProps> = ({
13-
maximumFractionDigits,
14-
adaptMaximumFractionDigits = 2,
1513
value,
1614
total = 100,
15+
fractionDigits = 2,
16+
adaptive = false,
1717
}) => {
1818
const { t } = useTranslation()
1919

@@ -23,15 +23,16 @@ export const PercentageValue: FC<PercentageValueProps> = ({
2323
return null
2424
}
2525

26+
const maximumFractionDigits = adaptive && percentageValue < 0.001 ? 3 : fractionDigits
27+
2628
return (
2729
<>
2830
{t('common.valuePair', {
2931
value: percentageValue,
3032
formatParams: {
3133
value: {
3234
style: 'percent',
33-
maximumFractionDigits:
34-
adaptMaximumFractionDigits && percentageValue < 0.001 ? 3 : maximumFractionDigits,
35+
maximumFractionDigits,
3536
} satisfies Intl.NumberFormatOptions,
3637
},
3738
})}

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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +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-
/>
80+
<PercentageValue value={validator.voting_power} total={stats?.total_voting_power} adaptive />
8581
),
86-
8782
key: 'voting',
8883
},
8984
{

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ export const ValidatorDetailsView: FC<{
236236
<dd>
237237
{stats?.total_voting_power ? (
238238
<>
239-
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} />
239+
<PercentageValue
240+
value={validator.voting_power}
241+
total={stats.total_voting_power}
242+
adaptive
243+
/>
240244
&nbsp; ({new BigNumber(validator.voting_power).toFormat()})
241245
</>
242246
) : (
@@ -306,11 +310,7 @@ export const ValidatorDetailsView: FC<{
306310
</dd>
307311
<dt>{t('validator.voting')}</dt>
308312
<dd>
309-
<PercentageValue
310-
value={validator.voting_power}
311-
total={stats?.total_voting_power}
312-
adaptMaximumFractionDigits
313-
/>
313+
<PercentageValue value={validator.voting_power} total={stats?.total_voting_power} adaptive />
314314
</dd>
315315
<dt>{t('common.staked')}</dt>
316316
<dd>

0 commit comments

Comments
 (0)