This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 430e7553f fix(ai): reload the current conversation history query 
(#6295)
430e7553f is described below

commit 430e7553f4c694d06c12cb5a9efea55672ff1a8c
Author: bai mengxuan <[email protected]>
AuthorDate: Fri Nov 27 12:27:29 2026 +0800

    fix(ai): reload the current conversation history query (#6295)
---
 .../__tests__/ConversationListModal.test.tsx       | 56 ++++++++++++++++
 web/src/pages/ai/hooks/useConversationList.test.ts | 74 ++++++++++++++++++++++
 web/src/pages/ai/hooks/useConversationList.ts      | 11 ++--
 3 files changed, 136 insertions(+), 5 deletions(-)

diff --git 
a/web/src/pages/ai/components/__tests__/ConversationListModal.test.tsx 
b/web/src/pages/ai/components/__tests__/ConversationListModal.test.tsx
index 3733f021d..fe8d8442e 100644
--- a/web/src/pages/ai/components/__tests__/ConversationListModal.test.tsx
+++ b/web/src/pages/ai/components/__tests__/ConversationListModal.test.tsx
@@ -195,6 +195,62 @@ describe('ConversationListModal', () => {
     await waitFor(() => expect(updateConversation).toHaveBeenCalledWith(7, { 
archived: false }));
   });
 
+  it('refreshesTheCurrentScopeWhenAnEarlierArchiveCompletesTest', async () => {
+    const user = userEvent.setup({ pointerEventsCheck: 0 });
+    let archived = false;
+    let finishArchive!: (value: Awaited<ReturnType<typeof 
updateConversation>>) => void;
+    const pendingArchive = new Promise<Awaited<ReturnType<typeof 
updateConversation>>>(
+      (resolve) => {
+        finishArchive = resolve;
+      },
+    );
+    updateMock.mockReturnValue(pendingArchive);
+    listMock.mockImplementation(async (query) => {
+      const items = query?.archived
+        ? [conversation(9, '此前归档会话'), ...(archived ? [conversation(7, 
'正在归档会话')] : [])]
+        : [
+            conversation(8, '仍在活动列表'),
+            ...(!archived ? [conversation(7, '正在归档会话')] : []),
+          ];
+      return page(items, items.length, 1);
+    });
+    renderModal();
+    await screen.findByText('正在归档会话');
+    await user.click(screen.getByTestId('ai-conversation-row-archive-7'));
+    await waitFor(() => expect(updateConversation).toHaveBeenCalledWith(7, { 
archived: true }));
+
+    // The scope remains usable while the real archive request is pending.
+    await user.click(screen.getByText('已归档'));
+    await screen.findByText('此前归档会话');
+    expect(screen.queryByText('仍在活动列表')).not.toBeInTheDocument();
+    const callsBeforeCompletion = listMock.mock.calls.length;
+    await act(async () => {
+      archived = true;
+      finishArchive({
+        id: 7,
+        title: '正在归档会话',
+        owner: 'test-operator',
+        engine: 'claude-code',
+        model: 'claude-sonnet-4-5',
+        mode: 'chat',
+        instanceId: 'open-source-local',
+        runtimeSessionId: null,
+        lastSeq: 2,
+        archived: true,
+        createdAt: '2026-09-20T02:11:00',
+        updatedAt: '2026-09-20T02:12:31',
+      });
+    });
+    await waitFor(() => 
expect(listMock.mock.calls.length).toBeGreaterThan(callsBeforeCompletion));
+    expect(await screen.findByText('此前归档会话')).toBeInTheDocument();
+    expect(screen.queryByText('仍在活动列表')).not.toBeInTheDocument();
+    expect(listMock).toHaveBeenLastCalledWith(expect.objectContaining({ 
archived: true, page: 1 }));
+    
expect(screen.getByTestId('ai-conversation-row-archive-7')).toHaveAttribute(
+      'aria-label',
+      '取消归档 正在归档会话',
+    );
+  });
+
   it('doesNotQueryWhileClosedTest', async () => {
     renderModal({ open: false });
 
diff --git a/web/src/pages/ai/hooks/useConversationList.test.ts 
b/web/src/pages/ai/hooks/useConversationList.test.ts
index 750a7b3ee..0e5c80580 100644
--- a/web/src/pages/ai/hooks/useConversationList.test.ts
+++ b/web/src/pages/ai/hooks/useConversationList.test.ts
@@ -168,6 +168,80 @@ describe('useConversationList', () => {
     });
   });
 
+  it('reloadsTheCurrentQueryFromAnEarlierCallbackTest', async () => {
+    listMock.mockImplementation(async (query) => {
+      const page = query?.page ?? 1;
+      const prefix = query?.search ?? '全部';
+      const items = Array.from({ length: 15 }, (_, index) => {
+        const id = (page - 1) * 15 + index + 1;
+        return listItem(id, `${prefix} ${id}`);
+      });
+      return resultPage(items, 30, page);
+    });
+    const { result } = renderHook(() => useConversationList());
+    await waitFor(() => expect(result.current.items).toHaveLength(15));
+    const reloadBeforeQueryChange = result.current.reload;
+    await act(async () => {
+      result.current.setSearch('归档');
+      result.current.setArchived(true);
+      result.current.setPage(2);
+    });
+    await waitFor(() => expect(result.current.items[0]?.title).toBe('归档 16'));
+    const callsBeforeReload = listMock.mock.calls.length;
+    await act(async () => reloadBeforeQueryChange());
+    await waitFor(() => 
expect(listMock.mock.calls.length).toBeGreaterThan(callsBeforeReload));
+    expect(listMock).toHaveBeenLastCalledWith({
+      page: 2,
+      size: 15,
+      search: '归档',
+      archived: true,
+    });
+    expect(result.current.items[0]?.title).toBe('归档 16');
+    expect(result.current.page).toBe(2);
+    expect(result.current.search).toBe('归档');
+    expect(result.current.archived).toBe(true);
+  });
+
+  it.each(['success', 'failure'])(
+    'keepsTheReloadResultWhenAnEarlierReadSettlesTest: %s',
+    async (outcome) => {
+      let resolveEarlier!: (value: PageResult<AiConversationListItemVO>) => 
void;
+      let rejectEarlier!: (error: Error) => void;
+      const earlier = new 
Promise<PageResult<AiConversationListItemVO>>((resolve, reject) => {
+        resolveEarlier = resolve;
+        rejectEarlier = reject;
+      });
+      listMock
+        .mockReturnValueOnce(earlier)
+        .mockResolvedValue(resultPage([listItem(9, '最新会话')], 1, 1));
+      const { result } = renderHook(() => useConversationList());
+      await waitFor(() => expect(listMock).toHaveBeenCalledTimes(1));
+      await act(async () => result.current.reload());
+      await waitFor(() => expect(result.current.items[0]?.title).toBe('最新会话'));
+      await act(async () => {
+        if (outcome === 'success') {
+          resolveEarlier(resultPage([listItem(7, '旧会话')], 1, 1));
+        } else {
+          rejectEarlier(new Error('旧查询失败'));
+        }
+      });
+      expect(listMock).toHaveBeenCalledTimes(2);
+      expect(result.current.items[0]?.title).toBe('最新会话');
+      expect(result.current.error).toBe('');
+      expect(result.current.loading).toBe(false);
+    },
+  );
+
+  it('doesNotReloadAListAfterItsViewUnmountsTest', async () => {
+    listMock.mockResolvedValue(resultPage([listItem(7, '查看集群状态')], 1, 1));
+    const { result, unmount } = renderHook(() => useConversationList());
+    await waitFor(() => expect(result.current.items).toHaveLength(1));
+    const reloadBeforeUnmount = result.current.reload;
+    unmount();
+    await act(async () => reloadBeforeUnmount());
+    expect(listMock).toHaveBeenCalledTimes(1);
+  });
+
   it('clampsThePageWhenTheServerTotalShrinksTest', async () => {
     const firstPage = Array.from({ length: 15 }, (_, i) => listItem(i + 1, `会话 
${i + 1}`));
     listMock
diff --git a/web/src/pages/ai/hooks/useConversationList.ts 
b/web/src/pages/ai/hooks/useConversationList.ts
index 8f2d49096..5d6caee00 100644
--- a/web/src/pages/ai/hooks/useConversationList.ts
+++ b/web/src/pages/ai/hooks/useConversationList.ts
@@ -71,6 +71,7 @@ export function useConversationList(
   const [archived, setArchivedValue] = useState(false);
   const [loading, setLoading] = useState(false);
   const [error, setError] = useState('');
+  const [reloadVersion, setReloadVersion] = useState(0);
   const requestId = useRef(0);
 
   const load = useCallback(async () => {
@@ -114,7 +115,7 @@ export function useConversationList(
     return () => {
       requestId.current += 1;
     };
-  }, [load]);
+  }, [load, reloadVersion]);
 
   const setSearch = useCallback((next: string) => {
     setSearchValue(next);
@@ -126,11 +127,11 @@ export function useConversationList(
     setPage(1);
   }, []);
 
-  // Re-running `load` is safe even while the previous query is still in 
flight: the requestId ref
-  // makes the older response a no-op.
+  // A mutation can finish after its caller has changed filters or pages. 
Invalidate the current
+  // query instead of running the `load` closure captured when the mutation 
started.
   const reload = useCallback(() => {
-    void load();
-  }, [load]);
+    setReloadVersion((version) => version + 1);
+  }, []);
 
   return {
     items,

Reply via email to