Skip to content

Commit 0d1bfcf

Browse files
committed
fix: switch chain button without modal
1 parent 0ed5738 commit 0d1bfcf

File tree

2 files changed

+21
-116
lines changed

2 files changed

+21
-116
lines changed

web/src/components/ConnectButton.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React from "react";
22
import styled from "styled-components";
3-
import { useToggle } from "react-use";
4-
import { useAccount, useNetwork } from "wagmi";
3+
import { useAccount, useNetwork, useSwitchNetwork } from "wagmi";
54
import { useWeb3Modal } from "@web3modal/react";
65
import { shortenAddress } from "utils/shortenAddress";
76
import { Button } from "@kleros/ui-components-library";
8-
import { SUPPORTED_CHAINIDS } from "consts/chains";
9-
import SwitchChainModal from "./SwitchChainModal";
7+
import { SUPPORTED_CHAINS, DEFAULT_CHAIN } from "consts/chains";
108

119
const StyledContainer = styled.div`
1210
width: fit-content;
@@ -29,11 +27,6 @@ const StyledContainer = styled.div`
2927
}
3028
`;
3129

32-
const StyledSwitchChainButton = styled(Button)`
33-
width: fit-content;
34-
height: 34px;
35-
`;
36-
3730
const AccountDisplay: React.FC = () => {
3831
return (
3932
<StyledContainer>
@@ -54,20 +47,33 @@ export const AddressDisplay: React.FC = () => {
5447
};
5548

5649
export const SwitchChainButton: React.FC = () => {
57-
const [isSwitchChainModalOpen, toggleIsSwitchChainModalOpen] = useToggle(false);
50+
const { switchNetwork, isLoading } = useSwitchNetwork();
51+
const handleSwitch = () => {
52+
if (!switchNetwork) {
53+
console.error("Cannot switch network. Please do it manually.");
54+
return;
55+
}
56+
try {
57+
switchNetwork(DEFAULT_CHAIN);
58+
} catch (err) {
59+
console.error(err);
60+
}
61+
};
5862
return (
59-
<>
60-
<StyledSwitchChainButton text={`Switch chain`} onClick={toggleIsSwitchChainModalOpen} />
61-
{isSwitchChainModalOpen && <SwitchChainModal toggle={toggleIsSwitchChainModalOpen} />}
62-
</>
63+
<Button
64+
isLoading={isLoading}
65+
disabled={isLoading}
66+
text={`Switch to ${SUPPORTED_CHAINS[DEFAULT_CHAIN].chainName}`}
67+
onClick={handleSwitch}
68+
/>
6369
);
6470
};
6571

6672
const ConnectButton: React.FC = () => {
6773
const { chain } = useNetwork();
6874
const { open, isOpen } = useWeb3Modal();
6975
return chain ? (
70-
!SUPPORTED_CHAINIDS.includes(chain.id) ? (
76+
chain?.id !== DEFAULT_CHAIN ? (
7177
<SwitchChainButton />
7278
) : (
7379
<AccountDisplay />

web/src/components/SwitchChainModal.tsx

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)