Skip to content

Commit 5f33b40

Browse files
committed
feat: add count up
1 parent 39d4fc7 commit 5f33b40

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"memoize-one": "^6.0.0",
8282
"qrcode.react": "^3.0.2",
8383
"react-chartjs-2": "^5.2.0",
84+
"react-countup": "^6.5.3",
8485
"react-json-to-table": "^0.1.7",
8586
"react-markdown": "^8.0.7",
8687
"react-tooltip": "^4.2.21",

packages/extension-polkagate/src/components/FormatPrice.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import type { BN } from '@polkadot/util';
55

66
import { Grid, Skeleton, Typography } from '@mui/material';
77
import React, { useMemo } from 'react';
8+
import CountUp from 'react-countup';
89

910
import { useCurrency } from '../hooks';
1011
import { ASSETS_AS_CURRENCY_LIST } from '../util/currencyList';
11-
import { amountToHuman, fixFloatingPoint } from '../util/utils';
12+
import { amountToHuman, countDecimalPlaces, fixFloatingPoint } from '../util/utils';
1213

1314
interface Props {
1415
amount?: BN | null;
@@ -27,6 +28,7 @@ interface Props {
2728
textColor?: string;
2829
height?: number;
2930
width?: string;
31+
withCountUp?: boolean;
3032
}
3133

3234
export function nFormatter (num: number, decimalPoint: number) {
@@ -54,7 +56,7 @@ export function nFormatter (num: number, decimalPoint: number) {
5456

5557
const DECIMAL_POINTS_FOR_CRYPTO_AS_CURRENCY = 4;
5658

57-
function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, fontWeight, height, lineHeight = 1, mt = '0px', num, price, sign, skeletonHeight = 15, textAlign = 'left', textColor, width = '90px' }: Props): React.ReactElement<Props> {
59+
function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, fontWeight, height, lineHeight = 1, mt = '0px', num, price, sign, skeletonHeight = 15, textAlign = 'left', textColor, width = '90px', withCountUp }: Props): React.ReactElement<Props> {
5860
const currency = useCurrency();
5961

6062
const total = useMemo(() => {
@@ -77,6 +79,12 @@ function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, f
7779
return decimalPoint;
7880
}, [currency?.code, decimalPoint]);
7981

82+
const no = useMemo(() => {
83+
const temp = fixFloatingPoint(total as number, _decimalPoint);
84+
85+
return parseFloat(temp);
86+
}, [_decimalPoint, total]);
87+
8088
return (
8189
<Grid
8290
item
@@ -91,7 +99,17 @@ function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, f
9199
lineHeight={lineHeight}
92100
sx={{ color: textColor }}
93101
>
94-
{sign || currency?.sign || ''}{ commify ? fixFloatingPoint(total as number, _decimalPoint, true) : nFormatter(total as number, _decimalPoint)}
102+
{withCountUp
103+
? <CountUp
104+
decimals={countDecimalPlaces(no)}
105+
duration={1}
106+
end={no}
107+
prefix={sign || currency?.sign || ''}
108+
/>
109+
: <>
110+
{sign || currency?.sign || ''}{ commify ? fixFloatingPoint(total as number, _decimalPoint, true) : nFormatter(total as number, _decimalPoint)}
111+
</>
112+
}
95113
</Typography>
96114
: <Skeleton
97115
animation='wave'

packages/extension-polkagate/src/fullscreen/homeFullScreen/partials/TotalBalancePieChart.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { FetchedBalance } from '../../../hooks/useAssetsBalances';
88
import { ArrowDropDown as ArrowDropDownIcon } from '@mui/icons-material';
99
import { Box, Collapse, Divider, Grid, type Theme, Typography, useTheme } from '@mui/material';
1010
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
11+
import CountUp from 'react-countup';
1112

1213
import { BN, BN_ZERO } from '@polkadot/util';
1314

@@ -19,7 +20,7 @@ import { isPriceOutdated } from '../../../popup/home/YouHave';
1920
import { COIN_GECKO_PRICE_CHANGE_DURATION } from '../../../util/api/getPrices';
2021
import { DEFAULT_COLOR, TEST_NETS, TOKENS_WITH_BLACK_LOGO } from '../../../util/constants';
2122
import getLogo2 from '../../../util/getLogo2';
22-
import { amountToHuman, fixFloatingPoint } from '../../../util/utils';
23+
import { amountToHuman, countDecimalPlaces, fixFloatingPoint } from '../../../util/utils';
2324
import Chart from './Chart';
2425

2526
interface Props {
@@ -194,6 +195,16 @@ function TotalBalancePieChart ({ hideNumbers, setGroupedAssets }: Props): React.
194195

195196
const toggleAssets = useCallback(() => setShowMore(!showMore), [showMore]);
196197

198+
const portfolioChange = useMemo(() => {
199+
if (!youHave?.change) {
200+
return 0;
201+
}
202+
203+
const value = fixFloatingPoint(youHave.change, 2, false, true);
204+
205+
return parseFloat(value);
206+
}, [youHave?.change]);
207+
197208
return (
198209
<Grid alignItems='flex-start' container direction='column' item justifyContent='flex-start' sx={{ bgcolor: 'background.paper', borderRadius: '5px', boxShadow: '2px 3px 4px 0px rgba(0, 0, 0, 0.1)', minHeight: '287px', p: '15px 25px 10px', width: '430px' }}>
199210
<Grid alignItems='flex-start' container item justifyContent='flex-start'>
@@ -214,9 +225,18 @@ function TotalBalancePieChart ({ hideNumbers, setGroupedAssets }: Props): React.
214225
fontWeight={700}
215226
num={youHave?.portfolio}
216227
textColor= { isPriceOutdated(youHave) ? 'primary.light' : 'text.primary'}
228+
withCountUp
217229
/>
218230
<Typography sx={{ color: youHave.change > 0 ? 'success.main' : 'warning.main', fontSize: '18px', fontWeight: 500 }}>
219-
{youHave.change > 0 ? '+ ' : '- '}{currency?.sign}{ fixFloatingPoint(youHave?.change, 2, true, true)} {`(${COIN_GECKO_PRICE_CHANGE_DURATION}h)`}
231+
{portfolioChange &&
232+
<CountUp
233+
decimals={countDecimalPlaces(portfolioChange)}
234+
duration={1}
235+
end={portfolioChange}
236+
prefix={`${youHave.change > 0 ? '+ ' : '- '}${currency?.sign}`}
237+
suffix={`(${COIN_GECKO_PRICE_CHANGE_DURATION}h)`}
238+
/>
239+
}
220240
</Typography>
221241
</>
222242
}

packages/extension-polkagate/src/util/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ function countLeadingZerosInFraction (numStr: string) {
5050
return 0;
5151
}
5252

53+
export function countDecimalPlaces (n: number) {
54+
const match = n.toString().match(/\.(\d+)/);
55+
56+
return match ? match[1].length : 0;
57+
}
58+
5359
export function fixFloatingPoint (_number: number | string, decimalDigit = FLOATING_POINT_DIGIT, commify?: boolean, dynamicDecimal?: boolean): string {
5460
const MAX_DECIMAL_POINTS = 6;
5561

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8297,6 +8297,13 @@ __metadata:
82978297
languageName: node
82988298
linkType: hard
82998299

8300+
"countup.js@npm:^2.8.0":
8301+
version: 2.8.0
8302+
resolution: "countup.js@npm:2.8.0"
8303+
checksum: 10/6681de9de9acd0d65b91183af280a462f2fbbc19ac5a90d30ea94e388f12b0d1cc7fa2b63b6bb83adc9dc38f938cd2a41f727a0b2ae7edf0d82f4b2ccb51e98c
8304+
languageName: node
8305+
linkType: hard
8306+
83008307
"crc-32@npm:^1.2.2":
83018308
version: 1.2.2
83028309
resolution: "crc-32@npm:1.2.2"
@@ -16694,6 +16701,17 @@ __metadata:
1669416701
languageName: node
1669516702
linkType: hard
1669616703

16704+
"react-countup@npm:^6.5.3":
16705+
version: 6.5.3
16706+
resolution: "react-countup@npm:6.5.3"
16707+
dependencies:
16708+
countup.js: "npm:^2.8.0"
16709+
peerDependencies:
16710+
react: ">= 16.3.0"
16711+
checksum: 10/5699e475bdd82b0b100174acbe24a33c9025080bacbba9f28593308145eacfb3e0d30be5de5bb153121346b719375e317a9f2aea87776f38d508cbe769ac0d8b
16712+
languageName: node
16713+
linkType: hard
16714+
1669716715
"react-dom@npm:^16.7.0":
1669816716
version: 16.14.0
1669916717
resolution: "react-dom@npm:16.14.0"
@@ -17620,6 +17638,7 @@ __metadata:
1762017638
pinst: "npm:^3.0.0"
1762117639
qrcode.react: "npm:^3.0.2"
1762217640
react-chartjs-2: "npm:^5.2.0"
17641+
react-countup: "npm:^6.5.3"
1762317642
react-json-to-table: "npm:^0.1.7"
1762417643
react-markdown: "npm:^8.0.7"
1762517644
react-tooltip: "npm:^4.2.21"

0 commit comments

Comments
 (0)