Skip to content

Commit ff45769

Browse files
committed
Move percentage logic to number utils
1 parent 1488256 commit ff45769

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

src/app/components/LabeledProgress/index.tsx

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

55
type LabeledProgresssProps = {
66
value?: number | string
@@ -9,19 +9,12 @@ type LabeledProgresssProps = {
99
}
1010

1111
export const LabeledProgress: FC<LabeledProgresssProps> = ({ value, max, label }) => {
12-
if (!value || !max) {
13-
return null
14-
}
12+
const percentage = calculatePercentage(value, max)
1513

16-
const progressValue = new BigNumber(value)
17-
const progressMax = new BigNumber(max)
18-
19-
if (!progressValue || !progressMax || progressMax.lte(0)) {
14+
if (percentage === null) {
2015
return null
2116
}
2217

23-
const percentage = progressValue.div(progressMax).multipliedBy(100).toNumber()
24-
2518
return (
2619
<div className="w-full relative">
2720
<Progress value={percentage} className="h-8 bg-muted-foreground rounded-[8px]" />

src/app/components/PercentageValue/index.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
3-
import BigNumber from 'bignumber.js'
3+
import { calculatePercentage } from '../../utils/number-utils'
44

55
type PercentageValueProps = {
66
adaptMaximumFractionDigits?: boolean
@@ -17,19 +17,12 @@ export const PercentageValue: FC<PercentageValueProps> = ({
1717
}) => {
1818
const { t } = useTranslation()
1919

20-
if (value === undefined || value === null || total === undefined || total === null) {
21-
return null
22-
}
20+
const percentageValue = calculatePercentage(value, total, false)
2321

24-
const votes = new BigNumber(value)
25-
const totalVotes = new BigNumber(total)
26-
27-
if (!votes || !totalVotes || totalVotes.lte(0)) {
22+
if (percentageValue === null) {
2823
return null
2924
}
3025

31-
const percentageValue = votes.div(totalVotes).toNumber()
32-
3326
return (
3427
<>
3528
{t('common.valuePair', {

src/app/components/Validators/ValidatorCumulativeVoting.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC } from 'react'
22
import { PercentageValue } from '../PercentageValue'
3-
import BigNumber from 'bignumber.js'
3+
import { calculatePercentage } from '../../utils/number-utils'
44

55
type ValidatorCumulativeVotingProps = {
66
containerMarginThemeSpacing: number
@@ -13,19 +13,12 @@ export const ValidatorCumulativeVoting: FC<ValidatorCumulativeVotingProps> = ({
1313
value,
1414
total,
1515
}) => {
16-
if (value === undefined || value === null || total === undefined || total === null) {
17-
return null
18-
}
16+
const percentage = calculatePercentage(value, total)
1917

20-
const votes = new BigNumber(value)
21-
const totalVotes = new BigNumber(total)
22-
23-
if (!votes || !totalVotes || totalVotes.lte(0)) {
18+
if (percentage === null) {
2419
return null
2520
}
2621

27-
const percentage = votes.div(totalVotes).multipliedBy(100).toNumber()
28-
2922
return (
3023
<div className="flex-1 relative text-center">
3124
<div

src/app/utils/number-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,23 @@ export const getGasPrice = ({ fee = '', gasUsed = '' }): string | null => {
1717
}
1818

1919
export const convertToNano = (value: string): string => fromBaseUnits(value, -9)
20+
21+
export const calculatePercentage = (
22+
value: number | string | undefined,
23+
total: number | string | undefined,
24+
asPercentage: boolean = true,
25+
): number | null => {
26+
if (value === undefined || value === null || total === undefined || total === null) {
27+
return null
28+
}
29+
30+
const valueBN = new BigNumber(value)
31+
const totalBN = new BigNumber(total)
32+
33+
if (!valueBN || !totalBN || totalBN.lte(0)) {
34+
return null
35+
}
36+
37+
const result = valueBN.div(totalBN)
38+
return asPercentage ? result.multipliedBy(100).toNumber() : result.toNumber()
39+
}

0 commit comments

Comments
 (0)