|
1 | | -import React from "react"; |
2 | | -import { useAccount, useChainId, useSwitchNetwork } from "wagmi"; |
3 | | -import { useWeb3Modal } from "@web3modal/react"; |
| 1 | +import React, { useCallback } from "react"; |
| 2 | +import { useAccount, useChainId, useSwitchChain } from "wagmi"; |
| 3 | +import { useWeb3Modal, useWeb3ModalState } from "@web3modal/wagmi/react"; |
4 | 4 | import { Button } from "@kleros/ui-components-library"; |
5 | 5 | import { SUPPORTED_CHAINS, DEFAULT_CHAIN } from "consts/chains"; |
6 | 6 | import AccountDisplay from "./AccountDisplay"; |
7 | 7 |
|
8 | | -export const SwitchChainButton: React.FC = () => { |
9 | | - const { switchNetwork, isLoading } = useSwitchNetwork(); |
10 | | - const handleSwitch = () => { |
11 | | - if (!switchNetwork) { |
| 8 | +export const SwitchChainButton: React.FC<{ className?: string }> = ({ className }) => { |
| 9 | + const { switchChain, isPending } = useSwitchChain(); |
| 10 | + const handleSwitch = useCallback(() => { |
| 11 | + if (!switchChain) { |
12 | 12 | console.error("Cannot switch network. Please do it manually."); |
13 | 13 | return; |
14 | 14 | } |
15 | 15 | try { |
16 | | - switchNetwork(DEFAULT_CHAIN); |
| 16 | + switchChain({ chainId: DEFAULT_CHAIN }); |
17 | 17 | } catch (err) { |
18 | 18 | console.error(err); |
19 | 19 | } |
20 | | - }; |
| 20 | + }, [switchChain]); |
21 | 21 | return ( |
22 | 22 | <Button |
23 | | - isLoading={isLoading} |
24 | | - disabled={isLoading} |
| 23 | + {...{ className }} |
| 24 | + isLoading={isPending} |
| 25 | + disabled={isPending} |
25 | 26 | text={`Switch to ${SUPPORTED_CHAINS[DEFAULT_CHAIN].name}`} |
26 | 27 | onClick={handleSwitch} |
27 | 28 | /> |
28 | 29 | ); |
29 | 30 | }; |
30 | 31 |
|
31 | | -const ConnectButton: React.FC = () => { |
32 | | - const { open, isOpen } = useWeb3Modal(); |
33 | | - return <Button disabled={isOpen} small text={"Connect"} onClick={async () => open({ route: "ConnectWallet" })} />; |
| 32 | + |
| 33 | +const ConnectButton: React.FC<{ className?: string }> = ({ className }) => { |
| 34 | + const { open } = useWeb3Modal(); |
| 35 | + const { open: isOpen } = useWeb3ModalState(); |
| 36 | + return ( |
| 37 | + <Button |
| 38 | + {...{ className }} |
| 39 | + disabled={isOpen} |
| 40 | + small |
| 41 | + text={"Connect"} |
| 42 | + onClick={async () => open({ view: "Connect" })} |
| 43 | + /> |
| 44 | + ); |
34 | 45 | }; |
35 | 46 |
|
36 | | -const ConnectWallet: React.FC = () => { |
| 47 | +const ConnectWallet: React.FC<{ className?: string }> = ({ className }) => { |
37 | 48 | const chainId = useChainId(); |
38 | 49 | const { isConnected } = useAccount(); |
39 | 50 | if (isConnected) { |
40 | 51 | if (chainId !== DEFAULT_CHAIN) { |
41 | | - return <SwitchChainButton />; |
| 52 | + return <SwitchChainButton {...{ className }} />; |
42 | 53 | } else return <AccountDisplay />; |
43 | | - } else return <ConnectButton />; |
| 54 | + } else return <ConnectButton {...{ className }} />; |
44 | 55 | }; |
45 | 56 |
|
46 | 57 | export default ConnectWallet; |
0 commit comments