Skip to content

Commit 1290bc1

Browse files
committed
fix(insights): don't show very small numbers as zero
1 parent ca9dbd4 commit 1290bc1

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

apps/insights/src/components/LivePrices/index.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import type { PriceData, PriceComponent } from "@pythnetwork/client";
55
import { Skeleton } from "@pythnetwork/component-library/Skeleton";
66
import type { ReactNode } from "react";
77
import { useMemo } from "react";
8-
import { useNumberFormatter, useDateFormatter } from "react-aria";
8+
import { useDateFormatter } from "react-aria";
99

1010
import styles from "./index.module.scss";
1111
import {
1212
useLivePriceComponent,
1313
useLivePriceData,
1414
} from "../../hooks/use-live-price-data";
15+
import { usePriceFormatter } from "../../hooks/use-price-formatter";
1516
import type { Cluster } from "../../services/pyth";
1617

1718
export const SKELETON_WIDTH = 20;
@@ -66,20 +67,17 @@ const Price = ({
6667
}: {
6768
prev?: number | undefined;
6869
current?: number | undefined;
69-
}) => {
70-
const numberFormatter = useNumberFormatter({ maximumFractionDigits: 5 });
71-
72-
return current === undefined ? (
70+
}) =>
71+
current === undefined ? (
7372
<Skeleton width={SKELETON_WIDTH} />
7473
) : (
7574
<span
7675
className={styles.price}
7776
data-direction={prev ? getChangeDirection(prev, current) : "flat"}
7877
>
79-
{numberFormatter.format(current)}
78+
<FormattedPriceValue n={current} />
8079
</span>
8180
);
82-
};
8381

8482
export const LiveConfidence = ({
8583
publisherKey,
@@ -119,19 +117,23 @@ const LiveComponentConfidence = ({
119117
return <Confidence confidence={current?.latest.confidence} />;
120118
};
121119

122-
const Confidence = ({ confidence }: { confidence?: number | undefined }) => {
123-
const numberFormatter = useNumberFormatter({ maximumFractionDigits: 5 });
124-
125-
return (
126-
<span className={styles.confidence}>
127-
<PlusMinus className={styles.plusMinus} />
128-
{confidence === undefined ? (
129-
<Skeleton width={SKELETON_WIDTH} />
130-
) : (
131-
<span>{numberFormatter.format(confidence)}</span>
132-
)}
133-
</span>
134-
);
120+
const Confidence = ({ confidence }: { confidence?: number | undefined }) => (
121+
<span className={styles.confidence}>
122+
<PlusMinus className={styles.plusMinus} />
123+
{confidence === undefined ? (
124+
<Skeleton width={SKELETON_WIDTH} />
125+
) : (
126+
<span>
127+
<FormattedPriceValue n={confidence} />
128+
</span>
129+
)}
130+
</span>
131+
);
132+
133+
const FormattedPriceValue = ({ n }: { n: number }) => {
134+
const formatter = usePriceFormatter();
135+
136+
return useMemo(() => formatter.format(n), [n, formatter]);
135137
};
136138

137139
export const LiveLastUpdated = ({

apps/insights/src/components/PriceFeed/chart.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { z } from "zod";
1111

1212
import styles from "./chart.module.scss";
1313
import { useLivePriceData } from "../../hooks/use-live-price-data";
14+
import { usePriceFormatter } from "../../hooks/use-price-formatter";
1415
import { Cluster } from "../../services/pyth";
1516

1617
type Props = {
@@ -44,6 +45,7 @@ const useChartElem = (symbol: string, feedId: string) => {
4445
const chartRef = useRef<ChartRefContents | undefined>(undefined);
4546
const earliestDateRef = useRef<bigint | undefined>(undefined);
4647
const isBackfilling = useRef(false);
48+
const priceFormatter = usePriceFormatter();
4749

4850
const backfillData = useCallback(() => {
4951
if (!isBackfilling.current && earliestDateRef.current) {
@@ -113,6 +115,9 @@ const useChartElem = (symbol: string, feedId: string) => {
113115
timeVisible: true,
114116
secondsVisible: true,
115117
},
118+
localization: {
119+
priceFormatter: priceFormatter.format,
120+
},
116121
});
117122

118123
const price = chart.addSeries(LineSeries, { priceFormat });
@@ -141,7 +146,7 @@ const useChartElem = (symbol: string, feedId: string) => {
141146
chart.remove();
142147
};
143148
}
144-
}, [backfillData]);
149+
}, [backfillData, priceFormatter]);
145150

146151
useEffect(() => {
147152
if (current && chartRef.current) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useCallback } from "react";
2+
import { useNumberFormatter } from "react-aria";
3+
4+
export const usePriceFormatter = () => {
5+
const bigNumberFormatter = useNumberFormatter({ maximumFractionDigits: 2 });
6+
const smallNumberFormatter = useNumberFormatter({
7+
maximumSignificantDigits: 5,
8+
});
9+
return {
10+
format: useCallback(
11+
(n: number) =>
12+
n >= 1000
13+
? bigNumberFormatter.format(n)
14+
: formatToSubscriptNumber(smallNumberFormatter.format(n)),
15+
[bigNumberFormatter, smallNumberFormatter],
16+
),
17+
};
18+
};
19+
20+
const formatToSubscriptNumber = (numString: string) => {
21+
const parts = numString.split(".");
22+
23+
const [integerPart, decimalPart] = parts;
24+
if (integerPart && decimalPart) {
25+
const zerosCount =
26+
decimalPart.length - decimalPart.replace(/^0+/, "").length;
27+
28+
return zerosCount < 5
29+
? numString
30+
: integerPart +
31+
"." +
32+
"0" +
33+
(zerosCount > 9
34+
? String.fromCodePoint(0x20_80 + Math.floor(zerosCount / 10))
35+
: "") +
36+
String.fromCodePoint(0x20_80 + (zerosCount % 10)) +
37+
decimalPart.replace(/^0+/, "");
38+
} else {
39+
return numString;
40+
}
41+
};

0 commit comments

Comments
 (0)