From 000d4ff84684cf38072ddadcf7c79bb3a5d35fcb Mon Sep 17 00:00:00 2001 From: Adw8 Date: Wed, 7 Aug 2024 11:57:23 +0530 Subject: [PATCH] Redirect to previous page on connecting wallet --- src/App.tsx | 2 +- src/layout/SignPageLayout.tsx | 8 ++++++-- src/pages/ConnectWallet.tsx | 19 ++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f4896bb..ad4399f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -25,7 +25,7 @@ function App() { } /> } /> } /> - } /> + } /> } /> }> } /> diff --git a/src/layout/SignPageLayout.tsx b/src/layout/SignPageLayout.tsx index c59f459..208d503 100644 --- a/src/layout/SignPageLayout.tsx +++ b/src/layout/SignPageLayout.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { Outlet, useNavigate } from "react-router-dom"; +import { Outlet, useLocation, useNavigate } from "react-router-dom"; import { Toolbar, @@ -14,10 +14,14 @@ import { useWalletConnectContext } from "../context/WalletConnectContext"; const SignPageLayout = () => { const { disconnect, session } = useWalletConnectContext(); const navigate = useNavigate(); + const location = useLocation(); const disconnectHandler = async () => { + const { pathname } = location; + const redirectTo = pathname ? pathname.substring(1) : ""; + await disconnect(); - navigate("/"); + navigate(`/connect-wallet/${redirectTo}`); }; return ( diff --git a/src/pages/ConnectWallet.tsx b/src/pages/ConnectWallet.tsx index a0a83dd..5975303 100644 --- a/src/pages/ConnectWallet.tsx +++ b/src/pages/ConnectWallet.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from "react"; -import {useLocation, useNavigate } from "react-router-dom"; +import {useLocation, useNavigate, useParams } from "react-router-dom"; import { Button, Box, Container, Typography, colors } from "@mui/material"; @@ -11,15 +11,24 @@ const ConnectWallet = () => { const navigate = useNavigate(); const location = useLocation(); + const {redirectTo} = useParams(); useEffect(() => { if (session) { - navigate("/sign-with-nitro-key", { - state: location.state - }); + if (redirectTo){ + navigate(`/${redirectTo}`, { + state: location.state + }); + + } + else { + navigate("/sign-with-nitro-key", { + state: location.state + }); + } } - }, [session, navigate, location]); + }, [session, navigate, redirectTo, location.state]); const handler = async () => { await connect();