-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.tsx
60 lines (57 loc) · 1.68 KB
/
Header.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
import {
detectConcordiumProvider,
WalletApi,
} from "@concordium/browser-wallet-api-helpers";
import { AppBar, Toolbar, Typography, Button } from "@mui/material";
import { useState } from "react";
export default function Header(props: {
onConnected: (provider: WalletApi, account: string) => void;
onDisconnected: () => void;
}) {
const [isConnected, setConnected] = useState(false);
function connect() {
detectConcordiumProvider()
.then((provider) => {
provider
.connect()
.then((account) => {
setConnected(true);
props.onConnected(provider, account!);
})
.catch((_) => {
alert("Please allow wallet connection");
setConnected(false);
});
provider.removeAllListeners();
provider.on("accountDisconnected", () => {
setConnected(false);
props.onDisconnected();
});
provider.on("accountChanged", (account) => {
props.onDisconnected();
props.onConnected(provider, account);
setConnected(true);
});
provider.on("chainChanged", () => {
props.onDisconnected();
setConnected(false);
});
})
.catch((_) => {
console.error(`could not find provider`);
alert("Please download Concordium Wallet");
});
}
return (
<AppBar>
<Toolbar>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
Concordium NFT Minting
</Typography>
<Button color="inherit" onClick={connect} disabled={isConnected}>
{isConnected ? "Connected" : "Connect"}
</Button>
</Toolbar>
</AppBar>
);
}