Skip to content

Commit 1488256

Browse files
committed
Align components with API changes and keep backward compatibility
1 parent 7ff2b0e commit 1488256

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed

.changelog/2399.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prepare Explorer for incoming breaking changes in Nexus API

src/app/components/LabeledProgress/index.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { FC, ReactNode } from 'react'
22
import { Progress } from '@oasisprotocol/ui-library/src/components/progress'
3+
import BigNumber from 'bignumber.js'
34

45
type LabeledProgresssProps = {
5-
value?: number
6-
max?: number
6+
value?: number | string
7+
max?: number | string
78
label: ReactNode
89
}
910

@@ -12,9 +13,18 @@ export const LabeledProgress: FC<LabeledProgresssProps> = ({ value, max, label }
1213
return null
1314
}
1415

16+
const progressValue = new BigNumber(value)
17+
const progressMax = new BigNumber(max)
18+
19+
if (!progressValue || !progressMax || progressMax.lte(0)) {
20+
return null
21+
}
22+
23+
const percentage = progressValue.div(progressMax).multipliedBy(100).toNumber()
24+
1525
return (
1626
<div className="w-full relative">
17-
<Progress value={(value / max) * 100} className="h-8 bg-muted-foreground rounded-[8px]" />
27+
<Progress value={percentage} className="h-8 bg-muted-foreground rounded-[8px]" />
1828
<span className="absolute inset-y-0 left-3 flex items-center text-white text-xs font-normal">
1929
{label}
2030
</span>

src/app/components/PercentageValue/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
3+
import BigNumber from 'bignumber.js'
34

45
type PercentageValueProps = {
56
adaptMaximumFractionDigits?: boolean
6-
total?: number
7-
value: number | undefined
7+
total?: number | string
8+
value: number | string | undefined
89
maximumFractionDigits?: number
910
}
1011

@@ -16,11 +17,18 @@ export const PercentageValue: FC<PercentageValueProps> = ({
1617
}) => {
1718
const { t } = useTranslation()
1819

19-
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
20+
if (value === undefined || value === null || total === undefined || total === null) {
2021
return null
2122
}
2223

23-
const percentageValue = value / total
24+
const votes = new BigNumber(value)
25+
const totalVotes = new BigNumber(total)
26+
27+
if (!votes || !totalVotes || totalVotes.lte(0)) {
28+
return null
29+
}
30+
31+
const percentageValue = votes.div(totalVotes).toNumber()
2432

2533
return (
2634
<>

src/app/components/Validators/ValidatorCumulativeVoting.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
import { FC } from 'react'
22
import { PercentageValue } from '../PercentageValue'
3+
import BigNumber from 'bignumber.js'
34

45
type ValidatorCumulativeVotingProps = {
56
containerMarginThemeSpacing: number
6-
value: number | undefined
7-
total: number | undefined
7+
value: number | string | undefined
8+
total: number | string | undefined
89
}
910

1011
export const ValidatorCumulativeVoting: FC<ValidatorCumulativeVotingProps> = ({
1112
containerMarginThemeSpacing,
1213
value,
1314
total,
1415
}) => {
15-
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
16+
if (value === undefined || value === null || total === undefined || total === null) {
1617
return null
1718
}
1819

19-
const percentage = (value / total) * 100
20+
const votes = new BigNumber(value)
21+
const totalVotes = new BigNumber(total)
22+
23+
if (!votes || !totalVotes || totalVotes.lte(0)) {
24+
return null
25+
}
26+
27+
const percentage = votes.div(totalVotes).multipliedBy(100).toNumber()
2028

2129
return (
2230
<div className="flex-1 relative text-center">

src/app/pages/ValidatorDetailsPage/VotingPowerCard.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { Typography } from '@oasisprotocol/ui-library/src/components/typography'
4+
import BigNumber from 'bignumber.js'
45
import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
56
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'
67
import { LabeledProgress } from 'app/components/LabeledProgress'
@@ -18,13 +19,13 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator, stats })
1819
<SnapshotTextCard
1920
title={t('validator.votingPower')}
2021
label={
21-
typeof validator?.voting_power === 'number' && (
22-
<Typography>({validator?.voting_power.toLocaleString()})</Typography>
22+
validator?.voting_power !== undefined && (
23+
<Typography>({new BigNumber(validator?.voting_power).toFormat()})</Typography>
2324
)
2425
}
2526
withContentPadding={false}
2627
>
27-
{typeof validator?.voting_power === 'number' && stats?.total_voting_power && (
28+
{validator?.voting_power !== undefined && stats?.total_voting_power && (
2829
<>
2930
<Typography className="font-normal text-xs text-muted-foreground text-left pb-2">
3031
{t('validator.votingPowerOverall')}

src/app/pages/ValidatorDetailsPage/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { useHref, useLoaderData } from 'react-router-dom'
44
import { Card, CardContent } from '@oasisprotocol/ui-library/src/components/cards'
5+
import BigNumber from 'bignumber.js'
56
import {
67
Validator,
78
ValidatorAggStats,
@@ -229,17 +230,17 @@ export const ValidatorDetailsView: FC<{
229230
<dd>{formattedTime}</dd>
230231
</>
231232
)}
232-
{typeof validator.voting_power === 'number' && (
233+
{validator.voting_power !== undefined && (
233234
<>
234235
<dt>{t('validator.votingPower')}</dt>
235236
<dd>
236237
{stats?.total_voting_power ? (
237238
<>
238239
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} />
239-
&nbsp; ({validator.voting_power.toLocaleString()})
240+
&nbsp; ({new BigNumber(validator.voting_power).toFormat()})
240241
</>
241242
) : (
242-
validator.voting_power.toLocaleString()
243+
new BigNumber(validator.voting_power).toFormat()
243244
)}
244245
</dd>
245246
</>

0 commit comments

Comments
 (0)