-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenOut.tsx
More file actions
154 lines (141 loc) · 4.45 KB
/
TokenOut.tsx
File metadata and controls
154 lines (141 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import styled from 'styled-components'
import { TokenIcon } from '@/src/components/token/TokenIcon'
import { Token } from '@/types/token'
import { SkeletonLoading } from '@/src/components/loading/SkeletonLoading'
import { BigNumber } from 'ethers'
import { getBridgeCommonInfo } from '@/src/hooks/bridge/utils/getBridgeCommonInfo'
import { Chains, ChainsValues } from '@/src/constants/config/types'
import { useBridgeFee } from '@/src/hooks/bridge/useBridgeFee'
import { formatUnits } from 'ethers/lib/utils'
import { ReceiveTokenSwitcher } from '@/src/pagePartials/bridge/bridgeForm/ReceiveTokenSwitcher'
import { chainsConfig } from '@/src/constants/config/chains'
import { genericSuspense } from '@/src/components/safeSuspense'
import { NATIVE_TOKEN_ADDRESS } from '@/src/constants/config/common'
import { isSameString } from '@/src/utils/tools'
import React, { useState } from 'react'
const NoTokenSelected = styled.span`
font-size: 1.5rem;
opacity: 0.8;
`
const TokenOutValue = styled.span<{ disabled?: boolean }>`
font-size: 1.5rem;
font-weight: 500;
margin-left: auto;
opacity: ${({ disabled }) => (disabled ? 0.7 : 1)};
@media (min-width: ${({ theme }) => theme.breakPoints.tabletLandscapeStart}) {
font-size: 1.6rem;
font-weight: 600;
}
`
const wethOptions = [
{
icon: '/images/icons/wethToken.svg',
label: 'WETH',
name: 'eth-types',
},
{
icon: '/images/icons/ethToken.svg',
label: 'ETH',
name: 'eth-types',
},
]
const xdaiOptions = [
{
icon: '/images/icons/dai.svg',
label: 'DAI',
name: 'xdai-types',
},
{
icon: '/images/icons/usds.webp',
label: 'USDS',
name: 'xdai-types',
},
]
export const NoTokenOut: React.FC<{ loading?: boolean }> = ({ loading }) => (
<>
<SkeletonLoading
$animate={false}
style={{
borderRadius: '50%',
height: '24px',
width: '24px',
minWidth: '0',
}}
/>
<NoTokenSelected>{loading ? 'Loading...' : 'No token selected'}</NoTokenSelected>
<TokenOutValue disabled>0.00</TokenOutValue>
</>
)
export const TokenOut: React.FC<{
amount: BigNumber
fromChainId: ChainsValues
setReceiveNativeToken: (receiveNative: boolean) => void
setReceiveUsds: (receiveUsds: boolean) => void
toChainId: ChainsValues
token: Token
tokenOut: Token
}> = genericSuspense(
({
amount,
fromChainId,
setReceiveNativeToken: onReceiveNativeChange,
setReceiveUsds: onReceiveUsdsChange,
toChainId,
token,
tokenOut,
}) => {
const { isFromHome, isNativeBridge } = getBridgeCommonInfo({
fromChainId: fromChainId,
toChainId: toChainId,
tokenAddress: token.address,
})
const { data: feeInfo } = useBridgeFee({
amount,
isFromHome,
isNativeBridge,
token,
})
const showNativeTokenSwitcher =
fromChainId == Chains.gnosis &&
token.address == chainsConfig[Chains.gnosis].bridge.wForeignNative
const showXDaiSwitcher =
fromChainId === Chains.gnosis && isSameString(token.address, NATIVE_TOKEN_ADDRESS)
// Add state for selected option
const [selectedNativeToken, setSelectedNativeToken] = useState(wethOptions[0].label)
const [selectedXDaiToken, setSelectedXDaiToken] = useState(xdaiOptions[0].label)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const tokenOutAmount = formatUnits(amount.sub(feeInfo!), tokenOut?.decimals)
return (
<>
{showNativeTokenSwitcher ? (
<ReceiveTokenSwitcher
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
setSelectedNativeToken(event.target.value)
onReceiveNativeChange(event.target.value === 'ETH')
}}
options={wethOptions}
optionsId="ethOptions"
value={selectedNativeToken}
/>
) : showXDaiSwitcher ? (
<ReceiveTokenSwitcher
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
setSelectedXDaiToken(event.target.value)
onReceiveUsdsChange(event.target.value === 'USDS')
}}
options={xdaiOptions}
optionsId="xdaiOptions"
value={selectedXDaiToken}
/>
) : (
<>
<TokenIcon dimensions={24} iconSource={tokenOut.logoURI} symbol={tokenOut.symbol} />
{tokenOut.symbol}
</>
)}
<TokenOutValue>{tokenOutAmount}</TokenOutValue>
</>
)
},
() => <NoTokenOut loading />,
)