diff --git a/apps/ai-game-creator-shell/src/features/app-shell/useAccountWallet.ts b/apps/ai-game-creator-shell/src/features/app-shell/useAccountWallet.ts index 0cbe9fd66..7b57f9d96 100644 --- a/apps/ai-game-creator-shell/src/features/app-shell/useAccountWallet.ts +++ b/apps/ai-game-creator-shell/src/features/app-shell/useAccountWallet.ts @@ -43,8 +43,13 @@ export function useAccountWallet(currentUserId: string) { const [nativeRechargePayment, setNativeRechargePayment] = useState(null); const rechargeLifecycleRef = useRef(0); + const [walletUiOwnerUserId, setWalletUiOwnerUserId] = useState(currentUserId); const walletOwnerMatchesCurrentUser = Boolean(currentUserId) && ownerUserId === currentUserId; + const walletUiOwnerMatchesCurrentUser = + Boolean(currentUserId) && walletUiOwnerUserId === currentUserId; + const walletUiIsVisible = + walletOwnerMatchesCurrentUser && walletUiOwnerMatchesCurrentUser; const visibleMudPointBalance = walletOwnerMatchesCurrentUser ? mudPointBalance : null; @@ -74,6 +79,7 @@ export function useAccountWallet(currentUserId: string) { useEffect(() => { rechargeLifecycleRef.current += 1; + setWalletUiOwnerUserId(currentUserId); setWalletLedgerOpen(false); setWalletLedger(null); setWalletLedgerLoading(false); @@ -268,12 +274,12 @@ export function useAccountWallet(currentUserId: string) { mudPointBalanceError: visibleMudPointBalanceError, onWalletBalanceMayHaveChanged, resetWalletBalance, - walletLedgerOpen, + walletLedgerOpen: walletUiIsVisible && walletLedgerOpen, setWalletLedgerOpen, - walletLedger, - walletLedgerLoading, - walletLedgerError, - rechargeOpen, + walletLedger: walletUiIsVisible ? walletLedger : null, + walletLedgerLoading: walletUiIsVisible && walletLedgerLoading, + walletLedgerError: walletUiIsVisible ? walletLedgerError : null, + rechargeOpen: walletUiIsVisible && rechargeOpen, rechargeModalCenter: rechargeContent && visibleMudPointBalance ? { @@ -282,10 +288,14 @@ export function useAccountWallet(currentUserId: string) { mudPointBalance: visibleMudPointBalance, } : null, - rechargeLoading, - rechargeError, - submittingRechargeProductId, - nativeRechargePayment, + rechargeLoading: walletUiIsVisible && rechargeLoading, + rechargeError: walletUiIsVisible ? rechargeError : null, + submittingRechargeProductId: walletUiIsVisible + ? submittingRechargeProductId + : null, + nativeRechargePayment: walletUiIsVisible + ? nativeRechargePayment + : null, setNativeRechargePayment, loadWalletLedger, openWalletLedger, diff --git a/apps/ai-game-creator-shell/tests/walletStore.test.ts b/apps/ai-game-creator-shell/tests/walletStore.test.ts index 8d5c5b788..64b162f83 100644 --- a/apps/ai-game-creator-shell/tests/walletStore.test.ts +++ b/apps/ai-game-creator-shell/tests/walletStore.test.ts @@ -245,4 +245,32 @@ describe('useWalletStore', () => { mudPointBalance: detailedBalance(18), }); }); + + test('hides account-owned dialogs and data when the account changes', async () => { + clientApi.getClientProfileRechargeCenter.mockResolvedValue({ + walletBalance: 18, + mudPointBalance: detailedBalance(18), + }); + clientApi.getClientProfileWalletLedger.mockResolvedValue({ entries: [] }); + const { result, rerender } = renderHook( + ({ userId }) => useAccountWallet(userId), + { initialProps: { userId: 'user-a' } }, + ); + await act(async () => undefined); + await act(async () => { + result.current.openWalletLedger(); + result.current.openRecharge(); + }); + expect(result.current.walletLedgerOpen).toBe(true); + expect(result.current.rechargeOpen).toBe(true); + + await act(async () => { + rerender({ userId: 'user-b' }); + }); + + expect(result.current.walletLedgerOpen).toBe(false); + expect(result.current.walletLedger).toBeNull(); + expect(result.current.rechargeOpen).toBe(false); + expect(result.current.nativeRechargePayment).toBeNull(); + }); });