-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-wagmi.tsx
102 lines (91 loc) · 3.06 KB
/
with-wagmi.tsx
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
import React, { useState } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { capsuleConnector } from "@usecapsule/wagmi-v2-integration";
import { OAuthMethod } from "@usecapsule/web-sdk";
import { capsuleClient } from "../capsule-client";
import { createConfig, WagmiProvider, type CreateConfigParameters, useConnect } from "wagmi";
import { http } from "wagmi";
import { sepolia } from "wagmi/chains";
import { ModalTriggerCard } from "../../demo-ui/components/modal-trigger-card";
import { WalletSelectorModal } from "../../demo-ui/components/wallet-selector";
const connector = capsuleConnector({
capsule: capsuleClient,
chains: [sepolia],
appName: "Capsule Wagmi Example",
options: {},
nameOverride: "Capsule",
idOverride: "capsule",
oAuthMethods: [OAuthMethod.GOOGLE, OAuthMethod.TWITTER, OAuthMethod.FACEBOOK, OAuthMethod.DISCORD, OAuthMethod.APPLE],
disableEmailLogin: false,
disablePhoneLogin: false,
onRampTestMode: true,
});
const config: CreateConfigParameters = {
chains: [sepolia],
connectors: [connector],
transports: {
[sepolia.id]: http(),
},
};
const wagmiProviderConfig = createConfig(config);
const queryClient = new QueryClient();
interface WalletContentProps {
isModalOpen: boolean;
setIsModalOpen: (open: boolean) => void;
onConnect: () => void;
}
const WalletContent: React.FC<WalletContentProps> = ({ isModalOpen, setIsModalOpen, onConnect }) => {
const { connect, connectors } = useConnect({
mutation: {
onSuccess: () => {
setIsModalOpen(false);
onConnect();
},
},
});
const handleSelectWallet = (walletId: string) => {
const connector = connectors.find((c) => c.id === walletId);
if (connector) {
connect({ connector });
}
};
return (
<WalletSelectorModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
onSelectWallet={handleSelectWallet}
walletOptions={connectors.map((connector) => ({ id: connector.id, name: connector.name }))}
/>
);
};
const AuthWithWagmi: React.FC = () => {
const [step, setStep] = useState<0 | 1>(0);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isLoading] = useState(false);
const handleConnect = () => {
setStep(1);
};
return (
// Wrap the application with WagmiProvider and QueryClientProvider. Normally you add this in your main app file. For this demo, we are adding it here.
<WagmiProvider config={wagmiProviderConfig}>
<QueryClientProvider client={queryClient}>
<ModalTriggerCard
step={step}
titles={{
initial: "Connect with Wagmi",
success: "Wallet Connected!",
}}
buttonLabel="Connect Wallet"
isLoading={isLoading}
onModalOpen={() => setIsModalOpen(true)}>
<WalletContent
isModalOpen={isModalOpen}
setIsModalOpen={setIsModalOpen}
onConnect={handleConnect}
/>
</ModalTriggerCard>
</QueryClientProvider>
</WagmiProvider>
);
};
export default AuthWithWagmi;