diff --git a/src/components/platform-entry/usePlatformProfileCenterController.ts b/src/components/platform-entry/usePlatformProfileCenterController.ts index 4e15f6926..bd117ba93 100644 --- a/src/components/platform-entry/usePlatformProfileCenterController.ts +++ b/src/components/platform-entry/usePlatformProfileCenterController.ts @@ -355,7 +355,11 @@ export function usePlatformProfileCenterController({ const pendingWechatRechargeOrderIdRef = useRef(null); const confirmingWechatRechargeOrderIdRef = useRef(null); const rechargeCenterReadRevisionRef = useRef(0); + const accountLifecycleRevisionRef = useRef(0); + const walletLedgerReadRevisionRef = useRef(0); const currentUserId = currentUser?.id ?? ''; + const currentUserIdRef = useRef(currentUserId); + currentUserIdRef.current = currentUserId; const walletOwnerUserId = usePlatformWalletStore( (state) => state.ownerUserId, ); @@ -376,6 +380,8 @@ export function usePlatformProfileCenterController({ useEffect(() => { rechargeCenterReadRevisionRef.current += 1; + accountLifecycleRevisionRef.current += 1; + walletLedgerReadRevisionRef.current += 1; pendingWechatRechargeOrderIdRef.current = null; confirmingWechatRechargeOrderIdRef.current = null; setRechargeCenter(null); @@ -418,19 +424,25 @@ export function usePlatformProfileCenterController({ const loadWalletLedger = useCallback(() => { const snapshotOwnerUserId = currentUserId; + const accountRevision = accountLifecycleRevisionRef.current; + const revision = ++walletLedgerReadRevisionRef.current; setWalletLedgerError(null); setIsLoadingWalletLedger(true); void getPlatformProfileWalletLedger() .then((ledger) => { if ( - usePlatformWalletStore.getState().ownerUserId === snapshotOwnerUserId + revision === walletLedgerReadRevisionRef.current && + accountRevision === accountLifecycleRevisionRef.current && + currentUserIdRef.current === snapshotOwnerUserId ) { setWalletLedger(ledger); } }) .catch((error: unknown) => { if ( - usePlatformWalletStore.getState().ownerUserId !== snapshotOwnerUserId + revision !== walletLedgerReadRevisionRef.current || + accountRevision !== accountLifecycleRevisionRef.current || + currentUserIdRef.current !== snapshotOwnerUserId ) { return; } @@ -441,7 +453,9 @@ export function usePlatformProfileCenterController({ }) .finally(() => { if ( - usePlatformWalletStore.getState().ownerUserId === snapshotOwnerUserId + revision === walletLedgerReadRevisionRef.current && + accountRevision === accountLifecycleRevisionRef.current && + currentUserIdRef.current === snapshotOwnerUserId ) { setIsLoadingWalletLedger(false); } diff --git a/src/components/platform-entry/usePlatformProfileCenterController.walletLedger.test.tsx b/src/components/platform-entry/usePlatformProfileCenterController.walletLedger.test.tsx new file mode 100644 index 000000000..3efcfc2da --- /dev/null +++ b/src/components/platform-entry/usePlatformProfileCenterController.walletLedger.test.tsx @@ -0,0 +1,33 @@ +/* @vitest-environment jsdom */ + +import { act, waitFor } from '@testing-library/react'; +import { beforeEach, describe, expect, test, vi } from 'vitest'; + +import { usePlatformWalletStore } from '../../stores/usePlatformWalletStore'; +import { + profileClientMocks, + renderController, + userA, +} from './usePlatformProfileCenterController.testSupport'; + +describe('usePlatformProfileCenterController wallet ledger', () => { + beforeEach(() => { + window.history.replaceState(null, '', '/profile'); + usePlatformWalletStore.getState().resetWalletBalance(); + vi.clearAllMocks(); + }); + + test('settles a valid read without depending on wallet-store owner initialization', async () => { + profileClientMocks.getPlatformProfileWalletLedger.mockResolvedValue({ + entries: [], + }); + const { result } = renderController(userA); + + act(() => result.current.loadWalletLedger()); + + await waitFor(() => { + expect(result.current.walletLedger).toEqual({ entries: [] }); + expect(result.current.isLoadingWalletLedger).toBe(false); + }); + }); +});