-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenInputDemo.tsx
158 lines (146 loc) · 4.78 KB
/
TokenInputDemo.tsx
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
155
156
157
158
import { useState } from 'react'
import styled, { css } from 'styled-components'
import { Item, SkeletonLoading, breakpointMediaQuery } from '@bootnodedev/db-ui-toolkit'
import { arbitrum, mainnet, optimism, polygon } from 'viem/chains'
import { OptionsButton } from '@/src/components/pageComponents/home/Examples/demos/OptionsButton'
import { OptionsDropdown } from '@/src/components/pageComponents/home/Examples/demos/OptionsDropdown'
import Arbitrum from '@/src/components/pageComponents/home/Examples/demos/assets/Arbitrum'
import Eth from '@/src/components/pageComponents/home/Examples/demos/assets/Eth'
import Optimism from '@/src/components/pageComponents/home/Examples/demos/assets/Optimism'
import Polygon from '@/src/components/pageComponents/home/Examples/demos/assets/Polygon'
import TokenInput from '@/src/components/sharedComponents/TokenInput'
import { useTokenInput } from '@/src/components/sharedComponents/TokenInput/useTokenInput'
import type { Networks } from '@/src/components/sharedComponents/TokenSelect/types'
import { useTokenLists } from '@/src/hooks/useTokenLists'
import { useTokenSearch } from '@/src/hooks/useTokenSearch'
import { useWeb3Status } from '@/src/hooks/useWeb3Status'
import { withSuspenseAndRetry } from '@/src/utils/suspenseWrapper'
const Wrapper = styled.div`
padding-top: var(--base-common-padding);
width: 100%;
${breakpointMediaQuery(
'desktopStart',
css`
padding-top: calc(var(--base-common-padding) * 3);
`,
)}
`
type Options = 'single' | 'multi'
const SkeletonLoadingTokenInput = () => (
<SkeletonLoading
$animate={false}
style={{
height: '144px',
display: 'flex',
flexDirection: 'column',
padding: '16px',
rowGap: '8px',
width: '100%',
}}
>
<SkeletonLoading style={{ width: '80px', minHeight: '0', height: '17px' }} />
<SkeletonLoading style={{ width: '100%', minHeight: '58px', borderRadius: '8px' }} />
</SkeletonLoading>
)
const TokenInputs = withSuspenseAndRetry(
({ currentTokenInput }: { currentTokenInput: Options }) => {
const { isWalletConnected } = useWeb3Status()
const [currentNetworkId, setCurrentNetworkId] = useState<number>()
const { tokensByChainId } = useTokenLists()
const { searchResult } = useTokenSearch({
tokens: tokensByChainId[1],
defaultSearchTerm: 'WETH',
})
const tokenInputMulti = useTokenInput()
const tokenInputSingle = useTokenInput(searchResult[0])
const networks: Networks = [
{
icon: <Eth />,
id: mainnet.id,
label: mainnet.name,
onClick: () => setCurrentNetworkId(mainnet.id),
},
{
icon: <Optimism />,
id: optimism.id,
label: optimism.name,
onClick: () => setCurrentNetworkId(optimism.id),
},
{
icon: <Arbitrum />,
id: arbitrum.id,
label: arbitrum.name,
onClick: () => setCurrentNetworkId(arbitrum.id),
},
{
icon: <Polygon />,
id: polygon.id,
label: polygon.name,
onClick: () => setCurrentNetworkId(polygon.id),
},
]
return (
<>
{currentTokenInput === 'multi' && (
<TokenInput
currentNetworkId={currentNetworkId}
networks={networks}
showAddTokenButton
showBalance={isWalletConnected}
showTopTokens
title="You pay"
tokenInput={tokenInputMulti}
/>
)}
{currentTokenInput === 'single' && (
<TokenInput
currentNetworkId={currentNetworkId}
networks={networks}
showAddTokenButton
showBalance={isWalletConnected}
showTopTokens
singleToken
title="You pay"
tokenInput={tokenInputSingle}
/>
)}
</>
)
},
)
/**
* This demo uses the TokenInput component to show how to use it in a single
* token or multi token mode.
*/
const TokenInputDemo = () => {
const dropdownItems = [
{ label: 'Single token', type: 'single' },
{ label: 'Multi token', type: 'multi' },
]
const [currentTokenInput, setCurrentTokenInput] = useState<Options>('single')
return (
<Wrapper>
<OptionsDropdown
button={
<OptionsButton>
{dropdownItems.find((item) => item.type === currentTokenInput)?.label}
</OptionsButton>
}
defaultActiveItem={0}
items={dropdownItems.map((item) => (
<Item
key={`${item.type}`}
onClick={() => setCurrentTokenInput(item.type as Options)}
>
{item.label}
</Item>
))}
/>
<TokenInputs
currentTokenInput={currentTokenInput}
suspenseFallback={<SkeletonLoadingTokenInput />}
/>
</Wrapper>
)
}
export default TokenInputDemo