diff --git a/src/App.tsx b/src/App.tsx index 24cd223..fe29475 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,18 +6,21 @@ import SignWithEthereum from "./pages/SignWithEthereum"; import SignWithCosmos from "./pages/SignWithCosmos"; import PageNotFound from "./pages/PageNotFound"; import SignPageLayout from "./layout/SignPageLayout"; +import { WalletConnectProvider } from "./context/WalletConnectContext"; function App() { return ( - - } /> - } > - } /> - } /> - - } /> - + + + } /> + } > + } /> + } /> + + } /> + + ); } diff --git a/src/context/WalletConnectContext.tsx b/src/context/WalletConnectContext.tsx index 744afe7..0e7e8ab 100644 --- a/src/context/WalletConnectContext.tsx +++ b/src/context/WalletConnectContext.tsx @@ -8,6 +8,7 @@ import React, { useCallback, } from "react"; import { SnackbarProvider, enqueueSnackbar } from "notistack"; +import { useNavigate } from "react-router-dom"; import SignClient from "@walletconnect/sign-client"; import { WalletConnectModal } from "@walletconnect/modal"; @@ -45,6 +46,7 @@ export const WalletConnectProvider = ({ }) => { const [signClient, setSignClient] = useState(); const [session, setSession] = useState(null); + const navigate = useNavigate() const disconnect = useCallback(async () => { if (signClient && session) { @@ -65,6 +67,23 @@ export const WalletConnectProvider = ({ } }, []); + const subscribeToEvents = useCallback( + async (client: SignClient) => { + client.on("session_update", ({ topic, params }) => { + const { namespaces } = params; + const currentSession = client.session.get(topic); + const updatedSession = { ...currentSession, namespaces }; + setSession(updatedSession); + }); + + client.on("session_delete", () => { + setSession(null); + navigate("/") + }); + }, + [navigate] + ); + const createClient = useCallback(async () => { const signClient = await SignClient.init({ projectId: PROJECT_ID, @@ -77,8 +96,9 @@ export const WalletConnectProvider = ({ }); setSignClient(signClient); + await subscribeToEvents(signClient); await checkPersistedState(signClient); - }, [checkPersistedState]); + }, [checkPersistedState, subscribeToEvents]) const connect = async () => { if (!signClient) { diff --git a/src/index.tsx b/src/index.tsx index 05fcf12..f8fdf20 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,7 +4,6 @@ import { Buffer } from "buffer"; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; -import { WalletConnectProvider } from './context/WalletConnectContext'; const root = ReactDOM.createRoot( document.getElementById('root') as HTMLElement @@ -13,9 +12,7 @@ const root = ReactDOM.createRoot( globalThis.Buffer = Buffer; root.render( - - - + ); diff --git a/src/pages/ConnectWallet.tsx b/src/pages/ConnectWallet.tsx index 4dd14ea..4a5b23e 100644 --- a/src/pages/ConnectWallet.tsx +++ b/src/pages/ConnectWallet.tsx @@ -1,7 +1,7 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { useNavigate } from 'react-router-dom' -import { Typography, Button, Box, Grid, Avatar } from '@mui/material'; +import { Typography, Button, Box, Container, Avatar } from '@mui/material'; import { useWalletConnectContext } from "../context/WalletConnectContext"; @@ -10,36 +10,33 @@ const ConnectWallet = () => { const navigate = useNavigate() - if (session) { - navigate("/sign-with-ethereum") - } + useEffect(() => { + if (session) { + navigate("/sign-with-ethereum") + } + }, [session, navigate]) + const handler = async () => { await connect(); } return ( - - - - - - - - - Urbit Onboarding - - - - Connect wallet + + + + + + Urbit Onboarding - - - - - + + Connect wallet + + + + ); }; diff --git a/src/pages/SignWithEthereum.tsx b/src/pages/SignWithEthereum.tsx index a64cd83..e3b6c11 100644 --- a/src/pages/SignWithEthereum.tsx +++ b/src/pages/SignWithEthereum.tsx @@ -120,7 +120,7 @@ const SignWithEthereum = () => { ))} - { Boolean(ethAddress) && (