-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathConnectWithSelect.tsx
More file actions
148 lines (139 loc) · 4.44 KB
/
ConnectWithSelect.tsx
File metadata and controls
148 lines (139 loc) · 4.44 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
import type { CoinbaseWallet } from '@web3-react/coinbase-wallet'
import type { Web3ReactHooks } from '@web3-react/core'
import { GnosisSafe } from '@web3-react/gnosis-safe'
import { Ledger } from '@web3-react/ledger'
import type { MetaMask } from '@web3-react/metamask'
import { Network } from '@web3-react/network'
import { WalletConnect } from '@web3-react/walletconnect'
import { WalletConnect as WalletConnectV2 } from '@web3-react/walletconnect-v2'
import { useCallback, useEffect, useState } from 'react'
import { CHAINS, getAddChainParameters } from '../chains'
function ChainSelect({
activeChainId,
switchChain,
chainIds,
}: {
activeChainId: number
switchChain: (chainId: number) => void
chainIds: number[]
}) {
return (
<select
value={activeChainId}
onChange={(event) => {
switchChain(Number(event.target.value))
}}
disabled={switchChain === undefined}
>
<option hidden disabled>
Select chain
</option>
<option value={-1}>Default</option>
{chainIds.map((chainId) => (
<option key={chainId} value={chainId}>
{CHAINS[chainId]?.name ?? chainId}
</option>
))}
</select>
)
}
export function ConnectWithSelect({
connector,
activeChainId,
chainIds = Object.keys(CHAINS).map(Number),
isActivating,
isActive,
error,
setError,
}: {
connector: Ledger | MetaMask | WalletConnect | WalletConnectV2 | CoinbaseWallet | Network | GnosisSafe
activeChainId: ReturnType<Web3ReactHooks['useChainId']>
chainIds?: ReturnType<Web3ReactHooks['useChainId']>[]
isActivating: ReturnType<Web3ReactHooks['useIsActivating']>
isActive: ReturnType<Web3ReactHooks['useIsActive']>
error: Error | undefined
setError: (error: Error | undefined) => void
}) {
const [desiredChainId, setDesiredChainId] = useState<number>(undefined)
/**
* When user connects eagerly (`desiredChainId` is undefined) or to the default chain (`desiredChainId` is -1),
* update the `desiredChainId` value so that <select /> has the right selection.
*/
useEffect(() => {
if (activeChainId && (!desiredChainId || desiredChainId === -1)) {
setDesiredChainId(activeChainId)
}
}, [desiredChainId, activeChainId])
const switchChain = useCallback(
async (desiredChainId: number) => {
setDesiredChainId(desiredChainId)
try {
if (
// If we're already connected to the desired chain, return
desiredChainId === activeChainId ||
// If they want to connect to the default chain and we're already connected, return
(desiredChainId === -1 && activeChainId !== undefined)
) {
setError(undefined)
return
}
if (desiredChainId === -1 || connector instanceof GnosisSafe) {
await connector.activate()
} else if (
connector instanceof Ledger ||
connector instanceof WalletConnectV2 ||
connector instanceof WalletConnect ||
connector instanceof Network
) {
await connector.activate(desiredChainId)
} else {
await connector.activate(getAddChainParameters(desiredChainId))
}
setError(undefined)
} catch (error) {
setError(error)
}
},
[connector, activeChainId, setError]
)
return (
<div style={{ display: 'flex', flexDirection: 'column' }}>
{!(connector instanceof GnosisSafe) && (
<ChainSelect activeChainId={desiredChainId} switchChain={switchChain} chainIds={chainIds} />
)}
<div style={{ marginBottom: '1rem' }} />
{isActive ? (
error ? (
<button onClick={() => switchChain(desiredChainId)}>Try again?</button>
) : (
<button
onClick={() => {
if (connector?.deactivate) {
void connector.deactivate()
} else {
void connector.resetState()
}
setDesiredChainId(undefined)
}}
>
Disconnect
</button>
)
) : (
<button
onClick={() =>
connector instanceof GnosisSafe
? void connector
.activate()
.then(() => setError(undefined))
.catch(setError)
: switchChain(desiredChainId)
}
disabled={isActivating || !desiredChainId}
>
{error ? 'Try again?' : 'Connect'}
</button>
)}
</div>
)
}