Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 42 additions & 36 deletions src/app/[locale]/dashboard/page-component.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use client"

import { useRouter } from "next/navigation"
import { useEffect, useRef } from "react"

import { useRouter } from "next/navigation"
import {
useAccountState,
useConnectModal,
Expand All @@ -11,50 +10,57 @@ import {

export default function Dashboard() {
const router = useRouter()
const walletMintNewCharacterModal = useWalletMintNewCharacterModal()

// Hooks for wallet and modal states
const walletMintModal = useWalletMintNewCharacterModal()
const connectModal = useConnectModal()

const [ssrReady, account] = useAccountState(({ ssrReady, computed }) => [
ssrReady,
computed.account,
])
const connectModal = useConnectModal()

const isConnectModalShown = useRef(false)
const isMintCharacterModalShown = useRef(false)
// Prevent duplicate modal openings
const hasShownConnectModal = useRef(false)
const hasShownMintModal = useRef(false)

useEffect(() => {
if (ssrReady) {
// Wait till SSR is ready
if (!account) {
// 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
}
if (!ssrReady) return // Wait for SSR to be ready

const handleDisconnected = () => {
if (!hasShownConnectModal.current) {
hasShownConnectModal.current = true
connectModal.show()
} else if (!connectModal.isActive) {
router.push("/")
}
}

const handleNoCharacter = () => {
if (!hasShownMintModal.current) {
hasShownMintModal.current = true
walletMintModal.show()
} else if (!walletMintModal.isActive) {
router.push("/")
}
}

const handleHasCharacter = () => {
router.push(`/dashboard/${account.character.handle}`)
}

// --- Decision tree ---
if (!account) {
handleDisconnected()
} else {
hasShownConnectModal.current = false
if (!account.character) {
handleNoCharacter()
} else {
// Wallet is connected, wait till site is ready
// Reset connect wallet status to prevent unexpected redirect
isConnectModalShown.current = false
if (!account.character) {
// 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/${account.character.handle}`)
}
handleHasCharacter()
}
}
}, [ssrReady, router, walletMintNewCharacterModal, account, connectModal])
}, [ssrReady, account, connectModal, walletMintModal, router])

return null
}
Loading