3
3
4
4
import type { BN } from '@polkadot/util' ;
5
5
6
- import { Grid , Skeleton , Typography } from '@mui/material' ;
6
+ import { Grid , Skeleton , Stack , Typography , useTheme } from '@mui/material' ;
7
7
import React , { useMemo } from 'react' ;
8
8
import CountUp from 'react-countup' ;
9
9
10
10
import { useCurrency } from '../hooks' ;
11
11
import { ASSETS_AS_CURRENCY_LIST } from '../util/currencyList' ;
12
- import { amountToHuman , fixFloatingPoint } from '../util/utils' ;
12
+ import { amountToHuman , fixFloatingPoint , getDecimal } from '../util/utils' ;
13
13
14
14
interface Props {
15
15
amount ?: BN | null ;
@@ -29,6 +29,7 @@ interface Props {
29
29
height ?: number ;
30
30
width ?: string ;
31
31
withCountUp ?: boolean ;
32
+ withSmallDecimal ?: boolean ;
32
33
}
33
34
34
35
export function nFormatter ( num : number , decimalPoint : number ) {
@@ -56,8 +57,9 @@ export function nFormatter (num: number, decimalPoint: number) {
56
57
57
58
const DECIMAL_POINTS_FOR_CRYPTO_AS_CURRENCY = 4 ;
58
59
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 > {
60
+ 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, withSmallDecimal } : Props ) : React . ReactElement < Props > {
60
61
const currency = useCurrency ( ) ;
62
+ const theme = useTheme ( ) ;
61
63
62
64
const total = useMemo ( ( ) => {
63
65
if ( num !== undefined ) {
@@ -72,12 +74,28 @@ function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, f
72
74
} , [ amount , decimals , num , price ] ) ;
73
75
74
76
const _decimalPoint = useMemo ( ( ) => {
77
+ if ( withSmallDecimal ) {
78
+ return 0 ;
79
+ }
80
+
75
81
if ( currency ?. code && ASSETS_AS_CURRENCY_LIST . includes ( currency . code ) ) {
76
82
return DECIMAL_POINTS_FOR_CRYPTO_AS_CURRENCY ;
77
83
}
78
84
79
85
return decimalPoint ;
80
- } , [ currency ?. code , decimalPoint ] ) ;
86
+ } , [ currency ?. code , decimalPoint , withSmallDecimal ] ) ;
87
+
88
+ function reduceFontSize ( fontSize : string | undefined , percentage : number ) {
89
+ if ( ! fontSize ) {
90
+ return undefined ;
91
+ }
92
+
93
+ const numericValue = parseFloat ( fontSize ) ;
94
+
95
+ const reducedSize = numericValue * ( 1 - ( percentage / 100 ) ) ;
96
+
97
+ return `${ Math . round ( reducedSize ) } px` ;
98
+ }
81
99
82
100
return (
83
101
< Grid
@@ -87,24 +105,47 @@ function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, f
87
105
textAlign = { textAlign }
88
106
>
89
107
{ total !== undefined
90
- ? < Typography
91
- fontSize = { fontSize }
92
- fontWeight = { fontWeight }
93
- lineHeight = { lineHeight }
94
- sx = { { color : textColor } }
108
+ ? < Stack
109
+ alignItems = 'baseline'
110
+ direction = 'row'
95
111
>
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
- </ >
112
+ < Typography
113
+ fontSize = { fontSize }
114
+ fontWeight = { fontWeight }
115
+ lineHeight = { lineHeight }
116
+ sx = { { color : textColor } }
117
+ >
118
+ { withCountUp
119
+ ? < CountUp
120
+ decimals = { _decimalPoint }
121
+ duration = { 1 }
122
+ end = { parseFloat ( String ( total ) ) }
123
+ prefix = { sign || currency ?. sign || '' }
124
+ />
125
+ : < >
126
+ { sign || currency ?. sign || '' } { commify ? fixFloatingPoint ( total as number , _decimalPoint , true ) : nFormatter ( total as number , _decimalPoint ) }
127
+ </ >
128
+ }
129
+ </ Typography >
130
+ { withSmallDecimal && Number ( total ) > 0 &&
131
+ < Typography
132
+ fontSize = { reduceFontSize ( fontSize , 20 ) }
133
+ fontWeight = { fontWeight }
134
+ lineHeight = { lineHeight }
135
+ sx = { { color : theme . palette . secondary . contrastText } }
136
+ >
137
+ { withCountUp
138
+ ? < CountUp
139
+ duration = { 1 }
140
+ end = { Number ( getDecimal ( total ) ) }
141
+ prefix = { '.' }
142
+ />
143
+ : < >
144
+ { `.${ getDecimal ( total ) } ` }
145
+ </ > }
146
+ </ Typography >
106
147
}
107
- </ Typography >
148
+ </ Stack >
108
149
: < Skeleton
109
150
animation = 'wave'
110
151
height = { skeletonHeight }
0 commit comments