From 314c861b0f73e483bef8418a734e8f553dadc334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E5=AE=87?= Date: Wed, 5 Aug 2026 17:47:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=AA=E4=BA=BA=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E8=B4=A6=E5=8D=95=E8=AF=B7=E6=B1=82=E9=97=A8=E7=A6=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用控制器账号生命周期和账单请求 revision 校验响应,不再耦合钱包 Store owner 初始化。 确保当前账号的最新账单请求总能正确结束加载状态。 --- .../usePlatformProfileCenterController.ts | 20 +++++++++-- ...fileCenterController.walletLedger.test.tsx | 33 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/components/platform-entry/usePlatformProfileCenterController.walletLedger.test.tsx 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); + }); + }); +});