feat: add support for return back home (#435)

This commit is contained in:
Nya Candy 2023-04-26 18:46:08 +08:00 committed by GitHub
parent e7d1b1d7d8
commit b811993b35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 3 deletions

View File

@ -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}`)
}
}