From 75fe0162d32193fd3af00f3adc55eb1f03cc2102 Mon Sep 17 00:00:00 2001 From: Ritu Nethraa C S Date: Fri, 10 Oct 2025 08:21:41 +0530 Subject: [PATCH] Update - component.tsx Extracted modal handling logic to reusable functions Potential modal race conditions Added clear early returns and ref guards --- src/app/[locale]/dashboard/page-component.tsx | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/app/[locale]/dashboard/page-component.tsx b/src/app/[locale]/dashboard/page-component.tsx index a2a6f324ea..d4af248f9a 100644 --- a/src/app/[locale]/dashboard/page-component.tsx +++ b/src/app/[locale]/dashboard/page-component.tsx @@ -1,8 +1,7 @@ "use client" -import { useRouter } from "next/navigation" import { useEffect, useRef } from "react" - +import { useRouter } from "next/navigation" import { useAccountState, useConnectModal, @@ -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 }