1
1
import { Trans } from '@lingui/macro'
2
- import { ChainId , Currency } from '@uniswap/sdk-core'
2
+ import { ChainId , Currency , CurrencyAmount } from '@uniswap/sdk-core'
3
3
import { useWeb3React } from '@web3-react/core'
4
4
import { PortfolioLogo } from 'components/AccountDrawer/MiniPortfolio/PortfolioLogo'
5
5
import { getChainInfo } from 'constants/chainInfo'
6
6
import { asSupportedChain } from 'constants/chains'
7
+ import { useInfoExplorePageEnabled } from 'featureFlags/flags/infoExplore'
8
+ import { useInfoTDPEnabled } from 'featureFlags/flags/infoTDP'
9
+ import { Chain , PortfolioTokenBalancePartsFragment } from 'graphql/data/__generated__/types-and-hooks'
10
+ import { getTokenDetailsURL , gqlToCurrency , supportedChainIdFromGQLChain } from 'graphql/data/util'
7
11
import { useStablecoinValue } from 'hooks/useStablecoinPrice'
8
12
import useCurrencyBalance from 'lib/hooks/useCurrencyBalance'
9
13
import { useMemo } from 'react'
10
- import styled , { useTheme } from 'styled-components'
14
+ import { useNavigate } from 'react-router-dom'
15
+ import styled from 'styled-components'
11
16
import { ThemedText } from 'theme/components'
12
17
import { NumberType , useFormatter } from 'utils/formatNumbers'
13
18
14
- const BalancesCard = styled . div `
15
- border-radius: 16px;
19
+ import { MultiChainMap } from '.'
20
+
21
+ const BalancesCard = styled . div < { isInfoTDPEnabled ?: boolean } > `
16
22
color: ${ ( { theme } ) => theme . neutral1 } ;
17
- display: none;
23
+ display: flex;
24
+ flex-direction: column;
25
+ gap: 24px;
18
26
height: fit-content;
19
- padding: 16px;
27
+ ${ ( { isInfoTDPEnabled } ) => ! isInfoTDPEnabled && ' padding: 16px;' }
20
28
width: 100%;
21
29
22
30
// 768 hardcoded to match NFT-redesign navbar breakpoints
@@ -48,11 +56,13 @@ const BalanceContainer = styled.div`
48
56
flex: 1;
49
57
`
50
58
51
- const BalanceAmountsContainer = styled . div `
59
+ const BalanceAmountsContainer = styled . div < { isInfoTDPEnabled ?: boolean } > `
52
60
display: flex;
53
61
flex-direction: row;
54
62
justify-content: space-between;
55
63
align-items: center;
64
+ width: 100%;
65
+ ${ ( { isInfoTDPEnabled } ) => isInfoTDPEnabled && 'margin-left: 12px;' }
56
66
`
57
67
58
68
const StyledNetworkLabel = styled . div `
@@ -61,49 +71,187 @@ const StyledNetworkLabel = styled.div`
61
71
line-height: 16px;
62
72
`
63
73
64
- export default function BalanceSummary ( { token } : { token : Currency } ) {
65
- const { account, chainId } = useWeb3React ( )
66
- const theme = useTheme ( )
67
- const { label, color } = getChainInfo ( asSupportedChain ( chainId ) ?? ChainId . MAINNET )
68
- const balance = useCurrencyBalance ( account , token )
69
- const { formatCurrencyAmount } = useFormatter ( )
74
+ interface BalanceProps {
75
+ currency ?: Currency
76
+ chainId ?: ChainId
77
+ balance ?: CurrencyAmount < Currency > // TODO(WEB-3026): only used for pre-Info-project calculations, should remove after project goes live
78
+ gqlBalance ?: PortfolioTokenBalancePartsFragment
79
+ onClick ?: ( ) => void
80
+ }
81
+ const Balance = ( { currency, chainId = ChainId . MAINNET , balance, gqlBalance, onClick } : BalanceProps ) => {
82
+ const { formatCurrencyAmount, formatNumber } = useFormatter ( )
83
+ const { label : chainName , color } = getChainInfo ( asSupportedChain ( chainId ) ?? ChainId . MAINNET )
84
+ const currencies = useMemo ( ( ) => [ currency ] , [ currency ] )
85
+ const isInfoTDPEnabled = useInfoExplorePageEnabled ( )
86
+
70
87
const formattedBalance = formatCurrencyAmount ( {
71
88
amount : balance ,
72
89
type : NumberType . TokenNonTx ,
73
90
} )
74
91
const formattedUsdValue = formatCurrencyAmount ( {
75
92
amount : useStablecoinValue ( balance ) ,
76
- type : NumberType . FiatTokenStats ,
93
+ type : NumberType . PortfolioBalance ,
94
+ } )
95
+ const formattedGqlBalance = formatNumber ( {
96
+ input : gqlBalance ?. quantity ,
97
+ type : NumberType . TokenNonTx ,
77
98
} )
99
+ const formattedUsdGqlValue = formatNumber ( {
100
+ input : gqlBalance ?. denominatedValue ?. value ,
101
+ type : NumberType . PortfolioBalance ,
102
+ } )
103
+
104
+ if ( isInfoTDPEnabled ) {
105
+ return (
106
+ < BalanceRow onClick = { onClick } >
107
+ < PortfolioLogo currencies = { currencies } chainId = { chainId } size = "2rem" />
108
+ < BalanceAmountsContainer isInfoTDPEnabled >
109
+ < BalanceItem >
110
+ < ThemedText . BodyPrimary > { formattedUsdGqlValue } </ ThemedText . BodyPrimary >
111
+ </ BalanceItem >
112
+ < BalanceItem >
113
+ < ThemedText . BodySecondary > { formattedGqlBalance } </ ThemedText . BodySecondary >
114
+ </ BalanceItem >
115
+ </ BalanceAmountsContainer >
116
+ </ BalanceRow >
117
+ )
118
+ } else {
119
+ return (
120
+ < BalanceRow >
121
+ < PortfolioLogo currencies = { currencies } chainId = { chainId } size = "2rem" />
122
+ < BalanceContainer >
123
+ < BalanceAmountsContainer >
124
+ < BalanceItem >
125
+ < ThemedText . SubHeader >
126
+ { formattedBalance } { currency ?. symbol }
127
+ </ ThemedText . SubHeader >
128
+ </ BalanceItem >
129
+ < BalanceItem >
130
+ < ThemedText . BodyPrimary > { formattedUsdValue } </ ThemedText . BodyPrimary >
131
+ </ BalanceItem >
132
+ </ BalanceAmountsContainer >
133
+ < StyledNetworkLabel color = { color } > { chainName } </ StyledNetworkLabel >
134
+ </ BalanceContainer >
135
+ </ BalanceRow >
136
+ )
137
+ }
138
+ }
78
139
79
- const currencies = useMemo ( ( ) => [ token ] , [ token ] )
140
+ const ConnectedChainBalanceSummary = ( {
141
+ connectedChainBalance,
142
+ } : {
143
+ connectedChainBalance ?: CurrencyAmount < Currency >
144
+ } ) => {
145
+ const { chainId : connectedChainId } = useWeb3React ( )
146
+ if ( ! connectedChainId || ! connectedChainBalance || ! connectedChainBalance . greaterThan ( 0 ) ) return null
147
+ const token = connectedChainBalance . currency
148
+ const { label : chainName } = getChainInfo ( asSupportedChain ( connectedChainId ) ?? ChainId . MAINNET )
149
+ return (
150
+ < BalanceSection >
151
+ < ThemedText . SubHeaderSmall color = "neutral1" >
152
+ < Trans > Your balance on { chainName } </ Trans >
153
+ </ ThemedText . SubHeaderSmall >
154
+ < Balance currency = { token } chainId = { connectedChainId } balance = { connectedChainBalance } />
155
+ </ BalanceSection >
156
+ )
157
+ }
80
158
81
- if ( ! account || ! balance ) {
159
+ const PageChainBalanceSummary = ( { pageChainBalance } : { pageChainBalance ?: PortfolioTokenBalancePartsFragment } ) => {
160
+ if ( ! pageChainBalance || ! pageChainBalance . token ) return null
161
+ const currency = gqlToCurrency ( pageChainBalance . token )
162
+ return (
163
+ < BalanceSection >
164
+ < ThemedText . HeadlineSmall color = "neutral1" >
165
+ < Trans > Your balance</ Trans >
166
+ </ ThemedText . HeadlineSmall >
167
+ < Balance currency = { currency } chainId = { currency ?. chainId } gqlBalance = { pageChainBalance } />
168
+ </ BalanceSection >
169
+ )
170
+ }
171
+
172
+ const OtherChainsBalanceSummary = ( {
173
+ otherChainBalances,
174
+ hasPageChainBalance,
175
+ } : {
176
+ otherChainBalances : readonly PortfolioTokenBalancePartsFragment [ ]
177
+ hasPageChainBalance : boolean
178
+ } ) => {
179
+ const navigate = useNavigate ( )
180
+ const isInfoExplorePageEnabled = useInfoExplorePageEnabled ( )
181
+
182
+ if ( ! otherChainBalances . length ) return null
183
+ return (
184
+ < BalanceSection >
185
+ { hasPageChainBalance ? (
186
+ < ThemedText . SubHeaderSmall >
187
+ < Trans > On other networks</ Trans >
188
+ </ ThemedText . SubHeaderSmall >
189
+ ) : (
190
+ < ThemedText . HeadlineSmall >
191
+ < Trans > Balance on other networks</ Trans >
192
+ </ ThemedText . HeadlineSmall >
193
+ ) }
194
+ { otherChainBalances . map ( ( balance ) => {
195
+ const currency = balance . token && gqlToCurrency ( balance . token )
196
+ const chainId = ( balance . token && supportedChainIdFromGQLChain ( balance . token . chain ) ) ?? ChainId . MAINNET
197
+ return (
198
+ < Balance
199
+ key = { balance . id }
200
+ currency = { currency }
201
+ chainId = { chainId }
202
+ gqlBalance = { balance }
203
+ onClick = { ( ) =>
204
+ navigate (
205
+ getTokenDetailsURL ( {
206
+ address : balance . token ?. address ,
207
+ chain : balance . token ?. chain ?? Chain . Ethereum ,
208
+ isInfoExplorePageEnabled,
209
+ } )
210
+ )
211
+ }
212
+ />
213
+ )
214
+ } ) }
215
+ </ BalanceSection >
216
+ )
217
+ }
218
+
219
+ export default function BalanceSummary ( {
220
+ currency,
221
+ chain,
222
+ multiChainMap,
223
+ } : {
224
+ currency : Currency
225
+ chain : Chain
226
+ multiChainMap : MultiChainMap
227
+ } ) {
228
+ const { account } = useWeb3React ( )
229
+
230
+ const isInfoTDPEnabled = useInfoTDPEnabled ( )
231
+
232
+ const connectedChainBalance = useCurrencyBalance ( account , currency )
233
+
234
+ const pageChainBalance = multiChainMap [ chain ] . balance
235
+ const otherChainBalances : PortfolioTokenBalancePartsFragment [ ] = [ ]
236
+ for ( const [ key , value ] of Object . entries ( multiChainMap ) ) {
237
+ if ( key !== chain && value . balance !== undefined ) {
238
+ otherChainBalances . push ( value . balance )
239
+ }
240
+ }
241
+ const hasBalances = pageChainBalance || Boolean ( otherChainBalances . length )
242
+
243
+ if ( ! account || ! hasBalances ) {
82
244
return null
83
245
}
84
246
return (
85
- < BalancesCard >
86
- < BalanceSection >
87
- < ThemedText . SubHeaderSmall color = { theme . neutral1 } >
88
- < Trans > Your balance on { label } </ Trans >
89
- </ ThemedText . SubHeaderSmall >
90
- < BalanceRow >
91
- < PortfolioLogo currencies = { currencies } chainId = { token . chainId } size = "2rem" />
92
- < BalanceContainer >
93
- < BalanceAmountsContainer >
94
- < BalanceItem >
95
- < ThemedText . SubHeader >
96
- { formattedBalance } { token . symbol }
97
- </ ThemedText . SubHeader >
98
- </ BalanceItem >
99
- < BalanceItem >
100
- < ThemedText . BodyPrimary > { formattedUsdValue } </ ThemedText . BodyPrimary >
101
- </ BalanceItem >
102
- </ BalanceAmountsContainer >
103
- < StyledNetworkLabel color = { color } > { label } </ StyledNetworkLabel >
104
- </ BalanceContainer >
105
- </ BalanceRow >
106
- </ BalanceSection >
247
+ < BalancesCard isInfoTDPEnabled = { isInfoTDPEnabled } >
248
+ { ! isInfoTDPEnabled && < ConnectedChainBalanceSummary connectedChainBalance = { connectedChainBalance } /> }
249
+ { isInfoTDPEnabled && (
250
+ < >
251
+ < PageChainBalanceSummary pageChainBalance = { pageChainBalance } />
252
+ < OtherChainsBalanceSummary otherChainBalances = { otherChainBalances } hasPageChainBalance = { ! ! pageChainBalance } />
253
+ </ >
254
+ ) }
107
255
</ BalancesCard >
108
256
)
109
257
}
0 commit comments