Skip to content

Commit 024bdba

Browse files
committed
chore: abstract beautifystatnumber
1 parent f23a29d commit 024bdba

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx

+1-17
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { formatEther } from "viem";
1010
import { CoinIds } from "consts/coingecko";
1111

1212
import { formatUSD } from "utils/format";
13-
import { commify } from "utils/commify";
1413
import { isUndefined } from "utils/index";
14+
import { beautifyStatNumber } from "utils/beautifyStatNumber";
1515

1616
import { useCoinPrice } from "hooks/useCoinPrice";
1717
import { useHomePageExtraStats } from "queries/useHomePageExtraStats";
@@ -110,22 +110,6 @@ const InfoContainer = styled.div`
110110
padding-top: 4px;
111111
`;
112112

113-
function beautifyStatNumber(value: number): string {
114-
const absValue = Math.abs(value);
115-
116-
if (absValue >= 1e9) {
117-
return `${commify((value / 1e9).toFixed(2))}B`;
118-
} else if (absValue >= 1e6) {
119-
return `${commify((value / 1e6).toFixed(2))}M`;
120-
} else if (absValue >= 1e3) {
121-
return `${commify((value / 1e3).toFixed(0))}K`;
122-
} else if (absValue > 0 && absValue < 1) {
123-
return value.toFixed(2);
124-
}
125-
126-
return commify(value.toFixed(0));
127-
}
128-
129113
const calculateJurorOdds = (newStake: number, totalStake: number): string => {
130114
const odds = totalStake !== 0 ? (newStake * 100) / totalStake : 0;
131115
return `${odds.toFixed(2)}%`;

web/src/pages/Courts/CourtDetails/Stats.tsx

+5-22
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { useHomePageExtraStats } from "queries/useHomePageExtraStats";
2727
import { calculateSubtextRender } from "utils/calculateSubtextRender";
2828
import { formatETH, formatPNK, formatUnitsWei, formatUSD } from "utils/format";
2929
import { isUndefined } from "utils/index";
30-
import { commify } from "utils/commify";
30+
import { beautifyStatNumber } from "utils/beautifyStatNumber";
3131

3232
import StatDisplay, { IStatDisplay } from "components/StatDisplay";
3333
import { StyledSkeleton } from "components/StyledSkeleton";
@@ -109,23 +109,6 @@ const StyledDropdownSelect = styled(DropdownSelect)`
109109
}
110110
`;
111111

112-
function beautifyStatNumber(value: number): string {
113-
const absValue = Math.abs(value);
114-
115-
if (absValue >= 1e9) {
116-
return `${commify((value / 1e9).toFixed(2))}B`;
117-
} else if (absValue >= 1e6) {
118-
return `${commify((value / 1e6).toFixed(2))}M`;
119-
} else if (absValue >= 1e3) {
120-
return `${commify((value / 1e3).toFixed(0))}K`;
121-
} else if (absValue < 1 && absValue !== 0) {
122-
const inverseValue = 1 / absValue;
123-
return commify(inverseValue.toFixed(0));
124-
}
125-
126-
return commify(value.toFixed(0));
127-
}
128-
129112
interface IStat {
130113
title: string;
131114
coinId?: number;
@@ -255,7 +238,7 @@ const Stats = () => {
255238
const ethPriceUSD = pricesData ? pricesData[CoinIds.ETH]?.price : undefined;
256239
if (!ethPriceUSD || !treeExpectedRewardPerPnk) return "N/A";
257240
const pnkNeeded = treeExpectedRewardPerPnk * ethPriceUSD;
258-
return beautifyStatNumber(pnkNeeded);
241+
return beautifyStatNumber(pnkNeeded, true);
259242
},
260243
color: "purple",
261244
icon: PNKUSDIcon,
@@ -270,7 +253,7 @@ const Stats = () => {
270253
const treeExpectedRewardPerPnk = data?.treeExpectedRewardPerPnk;
271254
if (!treeExpectedRewardPerPnk) return "N/A";
272255
const pnkNeeded = treeExpectedRewardPerPnk;
273-
return beautifyStatNumber(pnkNeeded);
256+
return beautifyStatNumber(pnkNeeded, true);
274257
},
275258
color: "blue",
276259
icon: PNKETHIcon,
@@ -283,7 +266,7 @@ const Stats = () => {
283266
),
284267
getText: (data) => {
285268
const treeVotesPerPnk = data?.treeVotesPerPnk;
286-
return beautifyStatNumber(treeVotesPerPnk);
269+
return beautifyStatNumber(treeVotesPerPnk, true);
287270
},
288271
color: "orange",
289272
icon: VotesPerPNKIcon,
@@ -299,7 +282,7 @@ const Stats = () => {
299282
),
300283
getText: (data) => {
301284
const treeDisputesPerPnk = data?.treeDisputesPerPnk;
302-
return beautifyStatNumber(treeDisputesPerPnk);
285+
return beautifyStatNumber(treeDisputesPerPnk, true);
303286
},
304287
color: "orange",
305288
icon: BalanceWithPNKIcon,

web/src/utils/beautifyStatNumber.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { commify } from "./commify";
2+
3+
export function beautifyStatNumber(value: number, invertValue: boolean = false): string {
4+
const absValue = Math.abs(value);
5+
6+
if (absValue >= 1e9) {
7+
return `${commify((value / 1e9).toFixed(2))}B`;
8+
} else if (absValue >= 1e6) {
9+
return `${commify((value / 1e6).toFixed(2))}M`;
10+
} else if (absValue >= 1e3) {
11+
return `${commify((value / 1e3).toFixed(0))}K`;
12+
} else if (absValue > 0 && absValue < 1) {
13+
if (invertValue) {
14+
const inverseValue = 1 / absValue;
15+
return commify(inverseValue.toFixed(0));
16+
} else {
17+
return value.toFixed(2);
18+
}
19+
}
20+
21+
return commify(value.toFixed(0));
22+
}

0 commit comments

Comments
 (0)