Skip to content

Commit

Permalink
Add offline/online event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
JaniAnttonen committed Mar 4, 2022
1 parent dc7521c commit 81744ff
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import dynamic from "next/dynamic";
import Head from "next/head";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { Provider } from "urql";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { Provider } from "urql";
import Box from "../components/Box";
import Footer from "../components/Footer";
import Navigation from "../components/Navigation";
Expand All @@ -15,7 +15,7 @@ import { state, useSnapshot } from "../state";
import {
connectWallet,
ensureCorrectChain,
initWalletSubscriptions,
initWalletSubscriptions
} from "../state/actions/wallet";
import { darkTheme } from "../stitches.config";
import "../styles/globals.css";
Expand Down Expand Up @@ -51,10 +51,14 @@ function MyApp({ Component, pageProps }: AppProps) {

window.ethereum.on("accountsChanged", connectWallet);
window.ethereum.on("chainChanged", ensureCorrectChain);
window.addEventListener('offline', () => state.online = false);
window.addEventListener('online', () => state.online = true);

return () => {
window.ethereum.removeListener("accountsChanged", connectWallet);
window.ethereum.removeListener("chainChanged", ensureCorrectChain);
window.removeEventListener('offline', () => state.online = false);
window.removeEventListener('online', () => state.online = true);
};
}, [signer]);

Expand Down

0 comments on commit 81744ff

Please sign in to comment.