-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathApp.tsx
More file actions
116 lines (104 loc) · 3.61 KB
/
App.tsx
File metadata and controls
116 lines (104 loc) · 3.61 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import "./App.css";
import { useWeb3AuthConnect, useWeb3AuthDisconnect, useWeb3AuthUser } from "@web3auth/modal/react";
// IMP START - Blockchain Calls
import { useAccount } from "wagmi";
import { SendTransaction } from "./components/sendTransaction";
import { Balance } from "./components/getBalance";
import { SwitchChain } from "./components/switchNetwork";
// IMP END - Blockchain Calls
import { useState, useRef, useEffect } from "react";
function App() {
// IMP START - Login
const { connect, isConnected, connectorName, loading: connectLoading, error: connectError } = useWeb3AuthConnect();
// IMP END - Login
// IMP START - Logout
const { disconnect, loading: disconnectLoading, error: disconnectError } = useWeb3AuthDisconnect();
// IMP END - Logout
const { userInfo } = useWeb3AuthUser();
// IMP START - Blockchain Calls
const { address } = useAccount();
// IMP END - Blockchain Calls
const [showConsole, setShowConsole] = useState(false);
const consoleContentRef = useRef<HTMLParagraphElement>(null);
function uiConsole(...args: any[]): void {
if (consoleContentRef.current) {
consoleContentRef.current.innerHTML = JSON.stringify(args || {}, null, 2);
console.log(...args);
}
}
useEffect(() => {
if (showConsole && userInfo && consoleContentRef.current) {
uiConsole(userInfo);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [showConsole]);
const handleToggleConsole = () => {
setShowConsole(!showConsole);
};
const loggedInView = (
<div className="grid">
<h2>Connected to {connectorName}</h2>
{/* // IMP START - Blockchain Calls */}
<div>{address}</div>
{/* // IMP END - Blockchain Calls */}
<div className="flex-container">
<div>
<button onClick={handleToggleConsole} className="card">
{showConsole ? "Hide User Info" : "Get User Info"}
</button>
</div>
{/* // IMP START - Logout */}
<div>
<button onClick={() => disconnect()} className="card">
Log Out
</button>
{disconnectLoading && <div className="loading">Disconnecting...</div>}
{disconnectError && <div className="error">{disconnectError.message}</div>}
</div>
{/* // IMP END - Logout */}
</div>
{showConsole && (
<div id="console" style={{ whiteSpace: "pre-line", textAlign: "left" }}>
<p ref={consoleContentRef} style={{ whiteSpace: "pre-line", margin: 0 }}></p>
</div>
)}
{/* IMP START - Blockchain Calls */}
<SendTransaction />
<Balance />
<SwitchChain />
{/* IMP END - Blockchain Calls */}
</div>
);
const unloggedInView = (
// IMP START - Login
<div className="grid">
<button onClick={() => connect()} className="card">
Login
</button>
{connectLoading && <div className="loading">Connecting...</div>}
{connectError && <div className="error">{connectError.message}</div>}
</div>
// IMP END - Login
);
return (
<div className="container">
<h1 className="title">
<a target="_blank" href="https://web3auth.io/docs/sdk/pnp/web/modal" rel="noreferrer">
Web3Auth{" "}
</a>
& React Modal Quick Start
</h1>
{isConnected ? loggedInView : unloggedInView}
<footer className="footer">
<a
href="https://github.com/Web3Auth/web3auth-examples/tree/main/quick-starts/react-quick-start"
target="_blank"
rel="noopener noreferrer"
>
Source code
</a>
</footer>
</div>
);
}
export default App;