From d2397727f0807b64ca8e276aed19ceba8720f839 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 1de6dee..71cb74f 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();