import type { CoinbaseWallet } from '@web3-react/coinbase-wallet' import type { Web3ReactHooks } from '@web3-react/core' import { GnosisSafe } from '@web3-react/gnosis-safe' 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 ( { switchChain(Number(event.target.value)) }} disabled={switchChain === undefined} > Select chain Default {chainIds.map((chainId) => ( {CHAINS[chainId]?.name ?? chainId} ))} ) } export function ConnectWithSelect({ connector, activeChainId, chainIds = Object.keys(CHAINS).map(Number), isActivating, isActive, error, setError, }: { connector: MetaMask | WalletConnect | WalletConnectV2 | CoinbaseWallet | Network | GnosisSafe activeChainId: ReturnType chainIds?: ReturnType[] isActivating: ReturnType isActive: ReturnType error: Error | undefined setError: (error: Error | undefined) => void }) { const [desiredChainId, setDesiredChainId] = useState(undefined) /** * When user connects eagerly (`desiredChainId` is undefined) or to the default chain (`desiredChainId` is -1), * update the `desiredChainId` value so that 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 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 ( {!(connector instanceof GnosisSafe) && ( )} {isActive ? ( error ? ( switchChain(desiredChainId)}>Try again? ) : ( { if (connector?.deactivate) { void connector.deactivate() } else { void connector.resetState() } setDesiredChainId(undefined) }} > Disconnect ) ) : ( connector instanceof GnosisSafe ? void connector .activate() .then(() => setError(undefined)) .catch(setError) : switchChain(desiredChainId) } disabled={isActivating || !desiredChainId} > {error ? 'Try again?' : 'Connect'} )} ) }