🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(topic): open a new topic after archiving the active one (#21647)
AmAzing-815bac2060ad
diff --git a/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.test.tsx b/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.test.tsxindex 22af87b8f4e..e37a007ec68 100644--- a/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.test.tsx+++ b/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.test.tsx@@ -242,6 +242,62 @@ describe('useTopicItemDropdownMenu', () => {},);+ it.each([+ ['/project/project-1/topic/topic-1', true],+ ['/project/project-1/topic/topic-other', false],+ ])(+ 'after archiving a Project row at %s, navigates home only for the current Topic',+ async (pathname, shouldNavigate) => {+ scopeFixture.enabled = true;+ locationFixture.pathname = pathname;+ const { result } = renderHook(() =>+ useTopicItemDropdownMenu({ id: 'topic-1', title: 'Topic' }),+ );++ await result.current.toggleCompleted();++ expect(completeMock).toHaveBeenCalledWith('topic-1');+ expect(refreshProjectMock).toHaveBeenCalledOnce();+ if (shouldNavigate) expect(navigateMock).toHaveBeenCalledWith('/project/project-1');+ else expect(navigateMock).not.toHaveBeenCalled();+ },+ );++ it('stays on the Topic the user opened while the archive was in flight', async () => {+ scopeFixture.enabled = true;+ locationFixture.pathname = '/project/project-1/topic/topic-1';+ let finishArchive!: () => void;+ completeMock.mockReturnValueOnce(new Promise<void>((resolve) => (finishArchive = resolve)));+ const { rerender, result } = renderHook(() =>+ useTopicItemDropdownMenu({ id: 'topic-1', title: 'Topic' }),+ );++ const archiving = result.current.toggleCompleted();+ locationFixture.pathname = '/project/project-1/topic/topic-other';+ rerender();+ finishArchive();+ await archiving;++ expect(navigateMock).not.toHaveBeenCalled();+ });++ it('does not navigate once the user left the Project while the archive was in flight', async () => {+ scopeFixture.enabled = true;+ locationFixture.pathname = '/project/project-1/topic/topic-1';+ let finishArchive!: () => void;+ completeMock.mockReturnValueOnce(new Promise<void>((resolve) => (finishArchive = resolve)));+ const { result, unmount } = renderHook(() =>+ useTopicItemDropdownMenu({ id: 'topic-1', title: 'Topic' }),+ );++ const archiving = result.current.toggleCompleted();+ unmount();+ finishArchive();+ await archiving;++ expect(navigateMock).not.toHaveBeenCalled();+ });+it('copies the row Agent link even when another Agent is active', async () => {scopeFixture.enabled = true;const writeText = vi.fn();diff --git a/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.tsx b/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.tsxindex 37b1818f418..6e75a246f9c 100644--- a/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.tsx+++ b/src/features/AgentSidebar/Topic/List/Item/useDropdownMenu.tsx@@ -2,6 +2,7 @@ import { AGENT_CHAT_TOPIC_URL } from '@lobechat/const';import type { ChatTopicStatus } from '@lobechat/types';import { type DropdownItem } from '@lobehub/ui';import { copyToClipboard, Icon, toast } from '@lobehub/ui';+import { useLatest, useUnmountedRef } from 'ahooks';import {Archive,ArchiveRestore,@@ -74,6 +75,10 @@ export const useTopicItemDropdownMenu = ({const activeWorkspaceId = useActiveWorkspaceId();const { pathname } = useActiveLocation();const routeTopicId = pathname.match(/\/project\/[^/]+\/topic\/([^/]+)\/?$/)?.[1];+ // Read after awaits: the user may open another topic, or leave the project+ // (unmounting this sidebar), while an archive is in flight.+ const routeTopicIdRef = useLatest(routeTopicId);+ const unmountedRef = useUnmountedRef();const activeWorkspaceSlug = useActiveWorkspaceSlug();const { allowed: canCreateTopic } = usePermission('create_content');const { allowed: canEditTopic } = usePermission('edit_own_content');@@ -120,8 +125,22 @@ export const useTopicItemDropdownMenu = ({if (!id) return;if (isCompleted) await unmarkTopicCompleted(id);else await markTopicCompleted(id);- if (scope) await refresh();- }, [id, isCompleted, markTopicCompleted, unmarkTopicCompleted, scope, refresh]);+ if (scope) {+ if (!isCompleted && !unmountedRef.current && routeTopicIdRef.current === id)+ navigate(`/project/${scope.projectId}`);+ await refresh();+ }+ }, [+ id,+ isCompleted,+ markTopicCompleted,+ unmarkTopicCompleted,+ scope,+ routeTopicIdRef,+ unmountedRef,+ navigate,+ refresh,+ ]);const handleOpenShareModal = useCallback(() => {if (!id) return;diff --git a/src/store/chat/slices/topic/action.markTopicCompleted.test.ts b/src/store/chat/slices/topic/action.markTopicCompleted.test.tsnew file mode 100644index 00000000000..2b1a81f889b--- /dev/null+++ b/src/store/chat/slices/topic/action.markTopicCompleted.test.ts@@ -0,0 +1,72 @@+import { act } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { topicService } from '@/services/topic';+import { initialState } from '@/store/chat/initialState';+import { topicMapKey } from '@/store/chat/utils/topicMapKey';+import { type ChatTopic } from '@/types/topic';++import { useChatStore } from '../../store';++const seed = (activeTopicId: string) =>+ useChatStore.setState({+ activeAgentId: 'agent-1',+ activeTopicId,+ topicDataMap: {+ [topicMapKey({ agentId: 'agent-1' })]: {+ items: [+ { id: 'current', status: 'active', title: 'current' },+ { id: 'other', status: 'active', title: 'other' },+ ] as ChatTopic[],+ } as any,+ },+ });++describe('markTopicCompleted', () => {+ beforeEach(() => {+ vi.spyOn(topicService, 'updateTopic').mockResolvedValue(undefined as any);+ vi.spyOn(useChatStore.getState(), 'refreshTopic').mockResolvedValue(undefined);+ });++ afterEach(() => {+ vi.restoreAllMocks();+ useChatStore.setState(initialState);+ });++ // Before: archiving the open topic left the user on it, unlike deleting it.+ // After: it opens a new topic, the same way `removeTopic` does.+ it('opens a new topic after archiving the active one', async () => {+ seed('current');+ const switchTopicSpy = vi.spyOn(useChatStore.getState(), 'switchTopic');++ await act(() => useChatStore.getState().markTopicCompleted('current'));++ expect(topicService.updateTopic).toHaveBeenCalledWith(+ 'current',+ expect.objectContaining({ status: 'completed' }),+ );+ expect(switchTopicSpy).toHaveBeenCalledWith(null);+ });++ it('stays on the active topic when archiving another one', async () => {+ seed('current');+ const switchTopicSpy = vi.spyOn(useChatStore.getState(), 'switchTopic');++ await act(() => useChatStore.getState().markTopicCompleted('other'));++ expect(switchTopicSpy).not.toHaveBeenCalled();+ expect(useChatStore.getState().activeTopicId).toBe('current');+ });++ it('stays on the topic when the archive fails', async () => {+ seed('current');+ vi.mocked(topicService.updateTopic).mockRejectedValue(new Error('network'));+ const switchTopicSpy = vi.spyOn(useChatStore.getState(), 'switchTopic');++ await expect(+ act(() => useChatStore.getState().markTopicCompleted('current')),+ ).rejects.toThrow();++ expect(switchTopicSpy).not.toHaveBeenCalled();+ });+});diff --git a/src/store/chat/slices/topic/action.ts b/src/store/chat/slices/topic/action.tsindex 7fdbeb823da..6eb592acb67 100644--- a/src/store/chat/slices/topic/action.ts+++ b/src/store/chat/slices/topic/action.ts@@ -548,10 +548,9 @@ export class ChatTopicActionImpl {};markTopicCompleted = async (id: string): Promise<void> => {- await this.#get().internal_updateTopic(id, {- completedAt: new Date(),- status: 'completed',- });+ await this.#get().internal_updateTopic(id, { completedAt: new Date(), status: 'completed' });+ // Like deleting it, archiving the topic the user is on opens a new one.+ if (this.#get().activeTopicId === id) this.#get().switchTopic(null);};unmarkTopicCompleted = async (id: string): Promise<void> => {