-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectWallet.tsx
54 lines (45 loc) · 1.74 KB
/
ConnectWallet.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
import { useContext, useEffect, useState} from 'react'
import { connectWallet, getConnectedAddress, shrinkedAddress, tryConnectWallet, WalletState, web3 } from './crypto'
import {Web3Context} from "./Web3Context";
import {Button} from "@mui/material";
type Props = {
className?: string;
}
function ConnectWallet(props: Props) {
const [ connectState, setConnectState ] = useState(WalletState.NOT_LOADED);
const [ connectedAddress, updateConnectedAddress ] = useState<string|undefined>();
const [ web3State, setWeb3State ] = useContext(Web3Context);
useEffect(() => {
if (web3State.walletState === WalletState.WEB3_CONNECTED) {
getConnectedAddress().then(address => updateConnectedAddress(address))
}
if (web3State.walletState === WalletState.NOT_LOADED) {
tryConnectWallet()
.then(isConnected => {
if (isConnected) {
setWeb3State({ walletState: WalletState.WEB3_CONNECTED })
}
})
}
}, [web3State]);
const onClick = async () => {
const isConnected = await connectWallet()
if (isConnected) {
setWeb3State({walletState: WalletState.WEB3_CONNECTED})
} else {
setWeb3State({walletState: WalletState.UNKNOWN_ERROR})
}
}
return (
<Button
className="!mt-6"
variant="contained"
onClick={onClick}
{...props}
>
{web3State.walletState !== WalletState.WEB3_CONNECTED && 'Connect Wallet'}
{web3State.walletState === WalletState.WEB3_CONNECTED && `Connected ${shrinkedAddress(connectedAddress)}`}
</Button>
)
}
export default ConnectWallet