From b811993b3576e07cdb820aa900c88a4a44ee529d Mon Sep 17 00:00:00 2001 From: Nya Candy Date: Wed, 26 Apr 2023 18:46:08 +0800 Subject: [PATCH] feat: add support for return back home (#435) --- src/pages/dashboard/index.tsx | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 61ef438a..b918d851 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -1,5 +1,5 @@ import { useRouter } from "next/router" -import { useEffect } from "react" +import { useEffect, useRef } from "react" import { useAccountState, @@ -19,14 +19,38 @@ export default function Dashboard() { ]) const connectModal = useConnectModal() + const isConnectModalShown = useRef(false) + const isMintCharacterModalShown = useRef(false) + useEffect(() => { if (ssrReady) { + // Wait till SSR is ready if (!isConnected) { - connectModal.show() + // Wallet not connected + if (!isConnectModalShown.current) { + // Not shown + isConnectModalShown.current = true + connectModal.show() + } else if (!connectModal.isActive) { + // Shown, but closed by user + router.push("/") // Go back home + } } else if (userSites.isSuccess) { + // Wallet is connected, wait till site is ready + // Reset connect wallet status to prevent unexpected redirect + isConnectModalShown.current = false if (!userSites.data?.length) { - walletMintNewCharacterModal.show() + // No character found, prompt to mint one + if (!isMintCharacterModalShown.current) { + // Not shown + isMintCharacterModalShown.current = true + walletMintNewCharacterModal.show() + } else if (!walletMintNewCharacterModal.isActive) { + // Shown, but closed by user + router.push("/") // Go back home + } } else { + // Already have characters, redirect to primary router.push(`/dashboard/${userSites.data[0].username}`) } }