Skip to content

Commit 4e0b208

Browse files
committed
feat(web-devtools): add-connect-wallet-button
1 parent baa186e commit 4e0b208

File tree

5 files changed

+109
-20
lines changed

5 files changed

+109
-20
lines changed

web-devtools/src/app/(main)/ruler/ChangeDeveloper.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useSimulateKlerosCoreRulerChangeRuler, useWriteKlerosCoreRulerChangeRul
1111
import { isUndefined } from "utils/isUndefined";
1212
import { wrapWithToast } from "utils/wrapWithToast";
1313

14+
import { EnsureChain } from "components/EnsureChain";
1415
import LabeledInput from "components/LabeledInput";
1516

1617
import Header from "./Header";
@@ -73,12 +74,14 @@ const ChangeDeveloper: React.FC = () => {
7374
variant={isValid ? "" : "error"}
7475
/>
7576
</InputContainer>
76-
<Button
77-
text="Update"
78-
onClick={handleClick}
79-
isLoading={isLoading || isChanging}
80-
disabled={!changeRulerConfig || isError || isLoading || isChanging || isUndefined(arbitrable) || !isValid}
81-
/>
77+
<EnsureChain>
78+
<Button
79+
text="Update"
80+
onClick={handleClick}
81+
isLoading={isLoading || isChanging}
82+
disabled={!changeRulerConfig || isError || isLoading || isChanging || isUndefined(arbitrable) || !isValid}
83+
/>
84+
</EnsureChain>
8285
</Container>
8386
);
8487
};

web-devtools/src/app/(main)/ruler/ManualRuling.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use client";
12
import React, { useCallback, useMemo, useState } from "react";
23
import styled from "styled-components";
34

@@ -16,6 +17,7 @@ import {
1617
import { isUndefined } from "utils/isUndefined";
1718
import { wrapWithToast } from "utils/wrapWithToast";
1819

20+
import { EnsureChain } from "components/EnsureChain";
1921
import LabeledInput from "components/LabeledInput";
2022

2123
import Header from "./Header";
@@ -111,13 +113,14 @@ const ManualRuling: React.FC = () => {
111113
onChange={() => setOverriden((prev) => !prev)}
112114
/>
113115
</SelectContainer>
114-
115-
<Button
116-
text="Rule"
117-
onClick={handleRuling}
118-
isLoading={isLoadingExecuteConfig || isSending}
119-
disabled={isDisabled || isError || isSending || isLoadingExecuteConfig}
120-
/>
116+
<EnsureChain>
117+
<Button
118+
text="Rule"
119+
onClick={handleRuling}
120+
isLoading={isLoadingExecuteConfig || isSending}
121+
disabled={isDisabled || isError || isSending || isLoadingExecuteConfig}
122+
/>
123+
</EnsureChain>
121124
</Container>
122125
);
123126
};

web-devtools/src/app/(main)/ruler/RulingModes.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { isUndefined } from "utils/isUndefined";
1919
import { wrapWithToast } from "utils/wrapWithToast";
2020

21+
import { EnsureChain } from "components/EnsureChain";
2122
import LabeledInput from "components/LabeledInput";
2223

2324
import Header from "./Header";
@@ -269,13 +270,14 @@ const RulingModes: React.FC = () => {
269270
/>
270271
</AutomaticPresetInputsContainer>
271272
</SelectContainer>
272-
273-
<Button
274-
text="Update"
275-
onClick={handleUpdate}
276-
isLoading={isLoading || isSending}
277-
disabled={isDisabled || isSending}
278-
/>
273+
<EnsureChain>
274+
<Button
275+
text="Update"
276+
onClick={handleUpdate}
277+
isLoading={isLoading || isSending}
278+
disabled={isDisabled || isSending}
279+
/>
280+
</EnsureChain>
279281
</Container>
280282
);
281283
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React, { useCallback } from "react";
2+
3+
import { useWeb3Modal, useWeb3ModalState } from "@web3modal/wagmi/react";
4+
import { useAccount, useSwitchChain } from "wagmi";
5+
6+
import { Button } from "@kleros/ui-components-library";
7+
8+
import { SUPPORTED_CHAINS, DEFAULT_CHAIN } from "consts/chains";
9+
10+
export const SwitchChainButton: React.FC<{ className?: string }> = ({ className }) => {
11+
// TODO isLoading is not documented, but exists in the type, might have changed to isPending
12+
const { switchChain, isLoading } = useSwitchChain();
13+
const handleSwitch = useCallback(() => {
14+
if (!switchChain) {
15+
console.error("Cannot switch network. Please do it manually.");
16+
return;
17+
}
18+
try {
19+
switchChain({ chainId: DEFAULT_CHAIN });
20+
} catch (err) {
21+
console.error(err);
22+
}
23+
}, [switchChain]);
24+
return (
25+
<Button
26+
{...{ className }}
27+
isLoading={isLoading}
28+
disabled={isLoading}
29+
text={`Switch to ${SUPPORTED_CHAINS[DEFAULT_CHAIN].name}`}
30+
onClick={handleSwitch}
31+
/>
32+
);
33+
};
34+
35+
const ConnectButton: React.FC<{ className?: string }> = ({ className }) => {
36+
const { open } = useWeb3Modal();
37+
const { open: isOpen } = useWeb3ModalState();
38+
return (
39+
<Button
40+
{...{ className }}
41+
disabled={isOpen}
42+
small
43+
text={"Connect"}
44+
onClick={async () => open({ view: "Connect" })}
45+
/>
46+
);
47+
};
48+
49+
const ConnectWallet: React.FC<{ className?: string }> = ({ className }) => {
50+
const { isConnected, chainId } = useAccount();
51+
52+
if (isConnected && chainId !== DEFAULT_CHAIN) {
53+
return <SwitchChainButton {...{ className }} />;
54+
} else return <ConnectButton {...{ className }} />;
55+
};
56+
57+
export default ConnectWallet;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useEffect, useState } from "react";
2+
3+
import { useAccount } from "wagmi";
4+
5+
import { DEFAULT_CHAIN } from "consts/chains";
6+
7+
import ConnectWallet from "components/ConnectWallet";
8+
9+
interface IEnsureChain {
10+
children: React.ReactElement;
11+
className?: string;
12+
}
13+
14+
export const EnsureChain: React.FC<IEnsureChain> = ({ children, className }) => {
15+
const { chainId } = useAccount();
16+
const [isClient, setIsClient] = useState(false);
17+
18+
// hydration error workaround, in server pre-render chainId is undefined so it mismatches with the client's initial render
19+
useEffect(() => setIsClient(true), []);
20+
21+
if (!isClient) return children;
22+
23+
return chainId === DEFAULT_CHAIN ? children : <ConnectWallet {...{ className }} />;
24+
};

0 commit comments

Comments
 (0)