From fd2f9770a225811b1e0cfe1ab47de604a9c85695 Mon Sep 17 00:00:00 2001 From: shaw Date: Fri, 27 Nov 2026 13:51:37 +0800 Subject: [PATCH 1/2] fix(accounts): show proxy account counts on the right and keep them fresh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The proxy selector in the account dialogs showed a bare number next to the proxy name, and AccountsView loaded the counts only once on mount, so they went stale after accounts were created, deleted, imported or rebound. - Show "N 个账号" / "N account(s)" on the right of each proxy row, before the test button, with a tooltip. - Reload proxies with counts whenever the create, edit or bulk-edit dialog opens. A sequence guard drops out-of-order responses and a failed reload keeps the previous list. --- .../src/components/common/ProxySelector.vue | 22 +++-- .../ProxySelector.accountCount.spec.ts | 87 +++++++++++++++++++ .../__tests__/ProxySelector.testing.spec.ts | 21 +---- .../src/i18n/locales/en/admin/resources.ts | 2 + .../src/i18n/locales/zh/admin/resources.ts | 2 + frontend/src/views/admin/AccountsView.vue | 28 ++++-- .../admin/__tests__/AccountsView.lite.spec.ts | 28 +++++- 7 files changed, 156 insertions(+), 34 deletions(-) create mode 100644 frontend/src/components/common/__tests__/ProxySelector.accountCount.spec.ts diff --git a/frontend/src/components/common/ProxySelector.vue b/frontend/src/components/common/ProxySelector.vue index a441d6ceea..9e25ccbdf9 100644 --- a/frontend/src/components/common/ProxySelector.vue +++ b/frontend/src/components/common/ProxySelector.vue @@ -85,13 +85,6 @@
{{ proxy.name }} - - - {{ proxy.account_count }} - - + @@ -624,15 +624,20 @@ const showErrorPassthrough = ref(false) const showTLSFingerprintProfiles = ref(false) // Proxy account counts change whenever accounts are created, deleted, imported -// or rebound, so reload them each time a proxy-picking dialog opens. The -// sequence guard drops responses that arrive after a newer request; a failed -// refresh keeps the previous list so the selector never goes empty. +// or rebound, so reload them each time a proxy-picking dialog opens. Only a +// response newer than the last applied one is used, so a slow older response +// never overwrites fresher counts, while a failed newer request does not +// discard an older success; a failed refresh keeps the previous list. let proxiesLoadSeq = 0 +let proxiesAppliedSeq = 0 const loadProxies = async (): Promise => { const seq = ++proxiesLoadSeq try { const list: AccountProxy[] = await adminAPI.proxies.getAllWithCount() - if (seq === proxiesLoadSeq) proxies.value = list + if (seq > proxiesAppliedSeq) { + proxiesAppliedSeq = seq + proxies.value = list + } } catch (error: unknown) { console.error('Failed to load proxies:', error) } @@ -640,6 +645,12 @@ const loadProxies = async (): Promise => { watch([showCreate, showEdit, showBulkEdit], (open: boolean[], wasOpen: boolean[] | undefined) => { if (open.some((isOpen: boolean, i: number) => isOpen && !wasOpen?.[i])) void loadProxies() }) +// Batch creation can partially succeed and keep the dialog open for the +// remaining items, so refresh counts on every successful creation too. +const handleAccountsCreated = (): void => { + void reload() + void loadProxies() +} const edAcc = ref(null) const tempUnschedAcc = ref(null) diff --git a/frontend/src/views/admin/__tests__/AccountsView.lite.spec.ts b/frontend/src/views/admin/__tests__/AccountsView.lite.spec.ts index 4f82ca754b..652089f369 100644 --- a/frontend/src/views/admin/__tests__/AccountsView.lite.spec.ts +++ b/frontend/src/views/admin/__tests__/AccountsView.lite.spec.ts @@ -229,6 +229,64 @@ describe('admin AccountsView lite account list', () => { wrapper.unmount() }) + it('applies an older proxy response when a newer refresh fails, but never lets it overwrite a newer one', async () => { + type ProxyList = Array<{ id: number; name: string; account_count: number }> + const deferred = () => { + let resolve!: (value: ProxyList) => void + let reject!: (reason: Error) => void + const promise = new Promise((res, rej) => { resolve = res; reject = rej }) + return { promise, resolve, reject } + } + vi.spyOn(console, 'error').mockImplementation(() => {}) + const createModal = (wrapper: ReturnType) => wrapper.findComponent({ name: 'CreateAccountModal' }) + + // Mount request is slow; the dialog-open request fails first. + const mountLoad = deferred() + const openLoad = deferred() + getAllProxies.mockReturnValueOnce(mountLoad.promise).mockReturnValueOnce(openLoad.promise) + let wrapper = mountView() + await flushPromises() + await wrapper.get('[data-test="open-create"]').trigger('click') + openLoad.reject(new Error('offline')) + await flushPromises() + mountLoad.resolve([{ id: 1, name: 'Shared proxy', account_count: 3 }]) + await flushPromises() + expect(createModal(wrapper).props('proxies')).toEqual([{ id: 1, name: 'Shared proxy', account_count: 3 }]) + wrapper.unmount() + + // Mount request is slow; the dialog-open request succeeds first with newer counts. + const slowMount = deferred() + const fastOpen = deferred() + getAllProxies.mockReset().mockReturnValueOnce(slowMount.promise).mockReturnValueOnce(fastOpen.promise) + wrapper = mountView() + await flushPromises() + await wrapper.get('[data-test="open-create"]').trigger('click') + fastOpen.resolve([{ id: 1, name: 'Shared proxy', account_count: 4 }]) + await flushPromises() + slowMount.resolve([{ id: 1, name: 'Shared proxy', account_count: 3 }]) + await flushPromises() + expect(createModal(wrapper).props('proxies')).toEqual([{ id: 1, name: 'Shared proxy', account_count: 4 }]) + wrapper.unmount() + }) + + it('refreshes proxy account counts after accounts are created while the dialog stays open', async () => { + const before = [{ id: 2, name: 'Shared proxy', account_count: 7 }] + const after = [{ id: 2, name: 'Shared proxy', account_count: 9 }] + getAllProxies.mockResolvedValueOnce(before).mockResolvedValueOnce(before).mockResolvedValueOnce(after) + const wrapper = mountView() + await flushPromises() + await wrapper.get('[data-test="open-create"]').trigger('click') + await flushPromises() + const createModal = wrapper.findComponent({ name: 'CreateAccountModal' }) + expect(createModal.props('proxies')).toEqual(before) + + createModal.vm.$emit('created') + await flushPromises() + expect(getAllProxies).toHaveBeenCalledTimes(3) + expect(createModal.props('proxies')).toEqual(after) + wrapper.unmount() + }) + it('syncs the selected row without clearing notes missing from the upstream', async () => { const account = { ...listRow, type: 'apikey', notes: 'local notes' } listAccounts.mockResolvedValue({ items: [account], total: 1, page: 1, page_size: 20, pages: 1 })