Add listeners for session update and delete event (#8)

* Add event listeners for session update and delete

* Use container instead of grid

* Rename variable
This commit is contained in:
neerajvijay1997 2024-03-26 12:55:57 +05:30 committed by GitHub
parent 6505899598
commit 401ca279bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 56 additions and 39 deletions

View File

@ -6,10 +6,12 @@ import SignWithEthereum from "./pages/SignWithEthereum";
import SignWithCosmos from "./pages/SignWithCosmos"; import SignWithCosmos from "./pages/SignWithCosmos";
import PageNotFound from "./pages/PageNotFound"; import PageNotFound from "./pages/PageNotFound";
import SignPageLayout from "./layout/SignPageLayout"; import SignPageLayout from "./layout/SignPageLayout";
import { WalletConnectProvider } from "./context/WalletConnectContext";
function App() { function App() {
return ( return (
<Router> <Router>
<WalletConnectProvider>
<Routes> <Routes>
<Route path="/" element={<ConnectWallet />} /> <Route path="/" element={<ConnectWallet />} />
<Route element={<SignPageLayout />} > <Route element={<SignPageLayout />} >
@ -18,6 +20,7 @@ function App() {
</Route> </Route>
<Route path="*" element={<PageNotFound />} /> <Route path="*" element={<PageNotFound />} />
</Routes> </Routes>
</WalletConnectProvider >
</Router> </Router>
); );
} }

View File

@ -8,6 +8,7 @@ import React, {
useCallback, useCallback,
} from "react"; } from "react";
import { SnackbarProvider, enqueueSnackbar } from "notistack"; import { SnackbarProvider, enqueueSnackbar } from "notistack";
import { useNavigate } from "react-router-dom";
import SignClient from "@walletconnect/sign-client"; import SignClient from "@walletconnect/sign-client";
import { WalletConnectModal } from "@walletconnect/modal"; import { WalletConnectModal } from "@walletconnect/modal";
@ -45,6 +46,7 @@ export const WalletConnectProvider = ({
}) => { }) => {
const [signClient, setSignClient] = useState<SignClient>(); const [signClient, setSignClient] = useState<SignClient>();
const [session, setSession] = useState<SessionTypes.Struct | null>(null); const [session, setSession] = useState<SessionTypes.Struct | null>(null);
const navigate = useNavigate()
const disconnect = useCallback(async () => { const disconnect = useCallback(async () => {
if (signClient && session) { 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 createClient = useCallback(async () => {
const signClient = await SignClient.init({ const signClient = await SignClient.init({
projectId: PROJECT_ID, projectId: PROJECT_ID,
@ -77,8 +96,9 @@ export const WalletConnectProvider = ({
}); });
setSignClient(signClient); setSignClient(signClient);
await subscribeToEvents(signClient);
await checkPersistedState(signClient); await checkPersistedState(signClient);
}, [checkPersistedState]); }, [checkPersistedState, subscribeToEvents])
const connect = async () => { const connect = async () => {
if (!signClient) { if (!signClient) {

View File

@ -4,7 +4,6 @@ import { Buffer } from "buffer";
import './index.css'; import './index.css';
import App from './App'; import App from './App';
import reportWebVitals from './reportWebVitals'; import reportWebVitals from './reportWebVitals';
import { WalletConnectProvider } from './context/WalletConnectContext';
const root = ReactDOM.createRoot( const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement document.getElementById('root') as HTMLElement
@ -13,9 +12,7 @@ const root = ReactDOM.createRoot(
globalThis.Buffer = Buffer; globalThis.Buffer = Buffer;
root.render( root.render(
<React.StrictMode> <React.StrictMode>
<WalletConnectProvider>
<App /> <App />
</WalletConnectProvider>
</React.StrictMode> </React.StrictMode>
); );

View File

@ -1,7 +1,7 @@
import React from 'react'; import React, { useEffect } from 'react';
import { useNavigate } from 'react-router-dom' 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"; import { useWalletConnectContext } from "../context/WalletConnectContext";
@ -10,18 +10,18 @@ const ConnectWallet = () => {
const navigate = useNavigate() const navigate = useNavigate()
useEffect(() => {
if (session) { if (session) {
navigate("/sign-with-ethereum") navigate("/sign-with-ethereum")
} }
}, [session, navigate])
const handler = async () => { const handler = async () => {
await connect(); await connect();
} }
return ( return (
<Grid container spacing={2}> <Container maxWidth="lg">
<Grid item xs={2}>
</Grid>
<Grid item xs={8}>
<Box display="flex" flexDirection="column" alignItems="center" height="50vh" justifyContent="center" padding={5}> <Box display="flex" flexDirection="column" alignItems="center" height="50vh" justifyContent="center" padding={5}>
<Box display="flex" alignItems="center"> <Box display="flex" alignItems="center">
<Avatar alt="Urbit logo" src="https://avatars.githubusercontent.com/u/5237680?s=200&v=4" /> <Avatar alt="Urbit logo" src="https://avatars.githubusercontent.com/u/5237680?s=200&v=4" />
@ -36,10 +36,7 @@ const ConnectWallet = () => {
Connect Connect
</Button> </Button>
</Box> </Box>
</Grid> </Container>
<Grid item xs={2}>
</Grid>
</Grid>
); );
}; };

View File

@ -120,7 +120,7 @@ const SignWithEthereum = () => {
))} ))}
</Select> </Select>
{ Boolean(ethAddress) && (<Box {(Boolean(ethAddress) && Boolean(cosmosAddress)) && (<Box
sx={{ sx={{
backgroundColor: "lightgray", backgroundColor: "lightgray",
padding: 3, padding: 3,