Skip to content

Commit b155aaf

Browse files
authored
feat: add count up (#1642)
1 parent 9a3f991 commit b155aaf

File tree

7 files changed

+74
-9
lines changed

7 files changed

+74
-9
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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';
@@ -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(() => {
@@ -91,7 +93,17 @@ function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, f
9193
lineHeight={lineHeight}
9294
sx={{ color: textColor }}
9395
>
94-
{sign || currency?.sign || ''}{ commify ? fixFloatingPoint(total as number, _decimalPoint, true) : nFormatter(total as number, _decimalPoint)}
96+
{withCountUp
97+
? <CountUp
98+
decimals={_decimalPoint}
99+
duration={1}
100+
end={parseFloat(String(total))}
101+
prefix={sign || currency?.sign || ''}
102+
/>
103+
: <>
104+
{sign || currency?.sign || ''}{ commify ? fixFloatingPoint(total as number, _decimalPoint, true) : nFormatter(total as number, _decimalPoint)}
105+
</>
106+
}
95107
</Typography>
96108
: <Skeleton
97109
animation='wave'

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

Lines changed: 25 additions & 3 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,20 @@ 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+
208+
const changeSign = !youHave?.change
209+
? ''
210+
: youHave.change > 0 ? '+ ' : '- ';
211+
197212
return (
198213
<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' }}>
199214
<Grid alignItems='flex-start' container item justifyContent='flex-start'>
@@ -214,9 +229,16 @@ function TotalBalancePieChart ({ hideNumbers, setGroupedAssets }: Props): React.
214229
fontWeight={700}
215230
num={youHave?.portfolio}
216231
textColor= { isPriceOutdated(youHave) ? 'primary.light' : 'text.primary'}
232+
withCountUp
217233
/>
218-
<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)`}
234+
<Typography sx={{ color: !youHave.change ? 'secondary.contrastText' : youHave.change > 0 ? 'success.main' : 'warning.main', fontSize: '18px', fontWeight: 500 }}>
235+
<CountUp
236+
decimals={countDecimalPlaces(portfolioChange) || 2}
237+
duration={1}
238+
end={portfolioChange}
239+
prefix={`${changeSign}${currency?.sign}`}
240+
suffix={`(${COIN_GECKO_PRICE_CHANGE_DURATION}h)`}
241+
/>
220242
</Typography>
221243
</>
222244
}

packages/extension-polkagate/src/hooks/useYouHave.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import type { BN } from '@polkadot/util';
66
import { useCallback, useContext, useMemo } from 'react';
77

88
import { AccountsAssetsContext } from '../components';
9+
import { ASSETS_AS_CURRENCY_LIST } from '../util/currencyList';
910
import { amountToHuman } from '../util/utils';
10-
import { usePrices } from '.';
11+
import { useCurrency, usePrices } from '.';
1112

1213
export interface YouHaveType {
1314
change: number;
@@ -23,6 +24,7 @@ export interface YouHaveType {
2324
export default function useYouHave (): YouHaveType | undefined | null {
2425
const pricesInCurrencies = usePrices();
2526
const { accountsAssets } = useContext(AccountsAssetsContext);
27+
const currency = useCurrency();
2628

2729
const calcPrice = useCallback(
2830
(assetPrice: number | undefined, balance: BN, decimal: number) =>
@@ -62,10 +64,12 @@ export default function useYouHave (): YouHaveType | undefined | null {
6264
});
6365
});
6466

65-
const change = totalPrice - totalBeforeChange;
67+
const change = currency?.code
68+
? ASSETS_AS_CURRENCY_LIST.includes(currency.code.toUpperCase()) ? 0 : totalPrice - totalBeforeChange
69+
: 0;
6670

6771
return { change, date, portfolio: totalPrice } as unknown as YouHaveType;
68-
}, [accountsAssets, calcChange, calcPrice, pricesInCurrencies]);
72+
}, [accountsAssets, calcChange, calcPrice, currency, pricesInCurrencies]);
6973

7074
return youHave;
7175
}

packages/extension-polkagate/src/popup/home/YouHave.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ export default function YouHave ({ hideNumbers, setHideNumbers }: Props): React.
5656
/>
5757
: <Grid item pr='15px'>
5858
<FormatPrice
59-
fontSize='38px'
59+
fontSize='32px'
6060
fontWeight={500}
6161
height={36}
6262
num={youHave?.portfolio }
6363
skeletonHeight={36}
6464
textColor= { isPriceOutdated(youHave) ? 'primary.light' : 'text.primary'}
6565
width='223px'
66+
withCountUp
6667
/>
6768
</Grid>
6869
}

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)