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,18 +6,21 @@ 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>
<Routes> <WalletConnectProvider>
<Route path="/" element={<ConnectWallet />} /> <Routes>
<Route element={<SignPageLayout />} > <Route path="/" element={<ConnectWallet />} />
<Route path="/sign-with-ethereum" element={<SignWithEthereum />} /> <Route element={<SignPageLayout />} >
<Route path="/sign-with-cosmos/:ethAddress/:ethSignature" element={<SignWithCosmos />} /> <Route path="/sign-with-ethereum" element={<SignWithEthereum />} />
</Route> <Route path="/sign-with-cosmos/:ethAddress/:ethSignature" element={<SignWithCosmos />} />
<Route path="*" element={<PageNotFound />} /> </Route>
</Routes> <Route path="*" element={<PageNotFound />} />
</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,36 +10,33 @@ const ConnectWallet = () => {
const navigate = useNavigate() const navigate = useNavigate()
if (session) { useEffect(() => {
navigate("/sign-with-ethereum") if (session) {
} 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}> <Box display="flex" flexDirection="column" alignItems="center" height="50vh" justifyContent="center" padding={5}>
</Grid> <Box display="flex" alignItems="center">
<Grid item xs={8}> <Avatar alt="Urbit logo" src="https://avatars.githubusercontent.com/u/5237680?s=200&v=4" />
<Box display="flex" flexDirection="column" alignItems="center" height="50vh" justifyContent="center" padding={5}> <Typography variant="h4" component="h6">
<Box display="flex" alignItems="center"> Urbit Onboarding
<Avatar alt="Urbit logo" src="https://avatars.githubusercontent.com/u/5237680?s=200&v=4" />
<Typography variant="h4" component="h6">
Urbit Onboarding
</Typography>
</Box>
<Typography variant="h6" component="h6" style={{ marginTop: '30px' }}>
Connect wallet
</Typography> </Typography>
<Button variant="contained" onClick={handler} style={{ marginTop: '20px' }}>
Connect
</Button>
</Box> </Box>
</Grid> <Typography variant="h6" component="h6" style={{ marginTop: '30px' }}>
<Grid item xs={2}> Connect wallet
</Grid> </Typography>
</Grid> <Button variant="contained" onClick={handler} style={{ marginTop: '20px' }}>
Connect
</Button>
</Box>
</Container>
); );
}; };

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,