-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
44 lines (36 loc) · 1.37 KB
/
index.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
import type { NextPage } from "next";
import { useEffect } from "react";
import Wallet from "../components/Wallet";
import { useListen } from "../hooks/useListen";
import { useMetamask } from "../hooks/useMetamask";
const Home: NextPage = () => {
const { dispatch } = useMetamask();
const listen = useListen();
useEffect(() => {
if (typeof window !== undefined) {
// start by checking if window.ethereum is present, indicating a wallet extension
const ethereumProviderInjected = typeof window.ethereum !== "undefined";
// this could be other wallets so we can verify if we are dealing with metamask
// using the boolean constructor to be explecit and not let this be used as a falsy value (optional)
const isMetamaskInstalled =
ethereumProviderInjected && Boolean(window.ethereum.isMetaMask);
const local = window.localStorage.getItem("metamaskState");
// user was previously connected, start listening to MM
if (local) {
listen();
}
// local could be null if not present in LocalStorage
const { wallet, balance } = local
? JSON.parse(local)
: // backup if local storage is empty
{ wallet: null, balance: null };
dispatch({ type: "pageLoaded", isMetamaskInstalled, wallet, balance });
}
}, []);
return (
<>
<Wallet />
</>
);
};
export default Home;