Skip to content

🀯 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.

πŸ› fix(topic): hide the active topic right after archiving it (#20994)

Rdmclin2d82270dd7931
diff --git a/src/store/chat/slices/topic/initialState.ts b/src/store/chat/slices/topic/initialState.ts
index c77d865e9ee..f2cc3dc1352 100644
--- a/src/store/chat/slices/topic/initialState.ts
+++ b/src/store/chat/slices/topic/initialState.ts
@@ -69,6 +69,14 @@ export interface ChatTopicState {
creatingTopicIds: string[];
inSearchingMode?: boolean;
isSearchingTopic: boolean;
+ /**
+ * The active topic, when the user archived it while on it. The sidebar keeps
+ * the active topic visible even when archived (so a search hit or direct URL
+ * still shows the selection), but one just archived must disappear at once.
+ * Cleared whenever `activeTopicId` changes, so reopening it later pins it
+ * again. Maintained by `subscribeJustArchivedTopic`.
+ */
+ justArchivedTopicId?: string;
/**
* Project working directory the *next* topic creation should bind to.
*
diff --git a/src/store/chat/slices/topic/justArchived.test.ts b/src/store/chat/slices/topic/justArchived.test.ts
new file mode 100644
index 00000000000..13b01651c74
--- /dev/null
+++ b/src/store/chat/slices/topic/justArchived.test.ts
@@ -0,0 +1,159 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { useChatStore } from '@/store/chat';
+import { initialState } from '@/store/chat/initialState';
+import { topicMapKey } from '@/store/chat/utils/topicMapKey';
+import type { ChatTopic, ChatTopicStatus } from '@/types/topic';
+
+import { topicSelectors } from './selectors';
+
+const bucketKey = topicMapKey({ agentId: 'agent-1' });
+
+const topic = (id: string, status: ChatTopicStatus, updatedAt: number) =>
+ ({ createdAt: updatedAt, id, status, title: id, updatedAt }) as ChatTopic;
+
+const seed = (items: ChatTopic[], activeTopicId: string) =>
+ useChatStore.setState({
+ activeAgentId: 'agent-1',
+ activeTopicId,
+ topicDataMap: {
+ [bucketKey]: { currentPage: 0, hasMore: false, items, pageSize: 20, total: items.length },
+ },
+ });
+
+const setStatus = (id: string, status: ChatTopicStatus) => {
+ const bucket = useChatStore.getState().topicDataMap[bucketKey];
+ useChatStore.setState({
+ topicDataMap: {
+ [bucketKey]: {
+ ...bucket,
+ items: bucket.items.map((item) => (item.id === id ? { ...item, status } : item)),
+ },
+ },
+ });
+};
+
+const sidebarIds = () =>
+ topicSelectors
+ .displayTopicsForSidebar(
+ 20,
+ 'updatedAt',
+ false,
+ )(useChatStore.getState())
+ ?.map(({ id }) => id);
+
+describe('justArchivedTopicId', () => {
+ beforeEach(() => {
+ useChatStore.setState(initialState);
+ });
+
+ afterEach(() => {
+ useChatStore.setState(initialState);
+ });
+
+ it('hides the active topic from the sidebar as soon as it is archived', () => {
+ seed([topic('current', 'active', 2), topic('other', 'active', 1)], 'current');
+ expect(sidebarIds()).toEqual(['current', 'other']);
+
+ setStatus('current', 'completed');
+
+ expect(useChatStore.getState().justArchivedTopicId).toBe('current');
+ expect(sidebarIds()).toEqual(['other']);
+ });
+
+ it('still shows the archived topic when "show archived" is on', () => {
+ seed([topic('current', 'active', 2), topic('other', 'active', 1)], 'current');
+
+ setStatus('current', 'completed');
+
+ expect(
+ topicSelectors
+ .displayTopicsForSidebar(
+ 20,
+ 'updatedAt',
+ true,
+ )(useChatStore.getState())
+ ?.map(({ id }) => id),
+ ).toEqual(['current', 'other']);
+ });
+
+ it('keeps the just-archived topic pinned past the page when "show archived" is on', () => {
+ useChatStore.setState({
+ topicDetailMap: { current: topic('current', 'active', 1) },
+ });
+ seed([topic('other', 'active', 2)], 'current');
+ useChatStore.setState({
+ topicDetailMap: { current: topic('current', 'completed', 1) },
+ });
+ expect(useChatStore.getState().justArchivedTopicId).toBe('current');
+
+ expect(
+ topicSelectors
+ .displayTopicsForSidebar(
+ 1,
+ 'updatedAt',
+ true,
+ )(useChatStore.getState())
+ ?.map(({ id }) => id),
+ ).toEqual(['other', 'current']);
+ });
+
+ it('stays hidden while the refreshed list drops the row until its detail loads', () => {
+ seed([topic('current', 'active', 2), topic('other', 'active', 1)], 'current');
+ setStatus('current', 'completed');
+
+ // The list refresh excludes completed topics, so the active row goes missing
+ const bucket = useChatStore.getState().topicDataMap[bucketKey];
+ useChatStore.setState({
+ topicDataMap: { [bucketKey]: { ...bucket, items: [topic('other', 'active', 1)] } },
+ });
+ expect(useChatStore.getState().justArchivedTopicId).toBe('current');
+
+ // ...then the active-topic detail fetch loads it back as completed
+ useChatStore.setState({
+ topicDetailMap: { current: topic('current', 'completed', 2) },
+ });
+
+ expect(useChatStore.getState().justArchivedTopicId).toBe('current');
+ expect(sidebarIds()).toEqual(['other']);
+ });
+
+ it('clears when the topic is restored, so it is pinned again', () => {
+ useChatStore.setState({
+ topicDetailMap: { current: topic('current', 'active', 1) },
+ });
+ seed([topic('other', 'active', 2)], 'current');
+ useChatStore.setState({
+ topicDetailMap: { current: topic('current', 'completed', 1) },
+ });
+ expect(sidebarIds()).toEqual(['other']);
+
+ useChatStore.setState({
+ topicDetailMap: { current: topic('current', 'active', 1) },
+ });
+
+ expect(useChatStore.getState().justArchivedTopicId).toBeUndefined();
+ expect(sidebarIds()).toEqual(['other', 'current']);
+ });
+
+ it('clears once the user moves to another topic, so reopening pins it again', () => {
+ seed([topic('current', 'active', 2), topic('other', 'active', 1)], 'current');
+ setStatus('current', 'completed');
+
+ useChatStore.setState({ activeTopicId: 'other' });
+ expect(useChatStore.getState().justArchivedTopicId).toBeUndefined();
+
+ useChatStore.setState({ activeTopicId: 'current' });
+ expect(sidebarIds()).toEqual(['other', 'current']);
+ });
+
+ it('keeps a topic opened already archived pinned in the sidebar', () => {
+ useChatStore.setState({
+ topicDetailMap: { archived: topic('archived', 'completed', 1) },
+ });
+ seed([topic('other', 'active', 2)], 'archived');
+
+ expect(useChatStore.getState().justArchivedTopicId).toBeUndefined();
+ expect(sidebarIds()).toEqual(['other', 'archived']);
+ });
+});
diff --git a/src/store/chat/slices/topic/justArchived.ts b/src/store/chat/slices/topic/justArchived.ts
new file mode 100644
index 00000000000..193874e0a7a
--- /dev/null
+++ b/src/store/chat/slices/topic/justArchived.ts
@@ -0,0 +1,50 @@
+import { shallow } from 'zustand/shallow';
+
+import type { ChatStoreState } from '../../initialState';
+import { topicSelectors } from './selectors';
+
+interface SelectorSubscribableStore {
+ getState: () => ChatStoreState;
+ setState: (partial: Partial<ChatStoreState>, replace?: false, action?: string) => void;
+ subscribe: <U>(
+ selector: (state: ChatStoreState) => U,
+ listener: (selected: U, previous: U) => void,
+ options?: { equalityFn?: (a: U, b: U) => boolean },
+ ) => () => void;
+}
+
+/**
+ * Maintains `justArchivedTopicId`: the active topic is recorded when its status
+ * turns `completed` while it stays active (sidebar menu, topic header, bulk
+ * "archive merged PRs"…), and cleared once the user moves to another topic or
+ * the topic leaves `completed` again (restore, rolled-back optimistic archive).
+ * Watching the transition rather than each archive action covers every write
+ * path, including optimistic overlays.
+ */
+export const subscribeJustArchivedTopic = (store: SelectorSubscribableStore): (() => void) =>
+ store.subscribe(
+ (s) => [s.activeTopicId, topicSelectors.currentActiveTopic(s)?.status] as const,
+ ([topicId, status], [prevTopicId, prevStatus]) => {
+ const { justArchivedTopicId } = store.getState();
+
+ if (topicId !== prevTopicId) {
+ if (justArchivedTopicId)
+ store.setState({ justArchivedTopicId: undefined }, false, 'clearJustArchivedTopic');
+ return;
+ }
+
+ // Restored (or an optimistic archive rolled back): pin it again. An
+ // unknown status is not a restore β€” the list refresh after archiving
+ // drops the row until its detail loads back as `completed`.
+ if (justArchivedTopicId === topicId && status && status !== 'completed') {
+ store.setState({ justArchivedTopicId: undefined }, false, 'clearJustArchivedTopic');
+ return;
+ }
+
+ // `prevStatus` is undefined while the topic is still loading, so a topic
+ // opened already archived keeps its pinned row.
+ if (topicId && status === 'completed' && prevStatus && prevStatus !== 'completed')
+ store.setState({ justArchivedTopicId: topicId }, false, 'markJustArchivedTopic');
+ },
+ { equalityFn: shallow },
+ );
diff --git a/src/store/chat/slices/topic/selectors.ts b/src/store/chat/slices/topic/selectors.ts
index 5bcd0452fec..93be4e31520 100644
--- a/src/store/chat/slices/topic/selectors.ts
+++ b/src/store/chat/slices/topic/selectors.ts
@@ -301,6 +301,17 @@ const sortTopics = (topics: ChatTopic[], sortBy: TopicSortBy): ChatTopic[] => {
return [...topics].sort((a, b) => getTopicSortTime(b, field) - getTopicSortTime(a, field));
};
+// The active topic the sidebar pins into its list β€” except one the user just
+// archived while on it, which must hide at once (see `justArchivedTopicId`).
+// With archived topics shown, it keeps its pin so it survives the page slice.
+const sidebarActiveTopic = (
+ s: ChatStoreState,
+ includeCompleted: boolean,
+): ChatTopic | undefined => {
+ const topic = currentActiveTopic(s);
+ return !includeCompleted && topic && topic.id === s.justArchivedTopicId ? undefined : topic;
+};
+
// Limit topics for sidebar display based on user's page size preference
const displayTopicsForSidebar =
(pageSize: number, sortBy: TopicSortBy = 'updatedAt', includeCompleted = true) =>
@@ -308,7 +319,13 @@ const displayTopicsForSidebar =
const topics = currentTopicsWithoutSystemTriggers(s);
if (!topics) return undefined;
- return selectSidebarTopics(topics, pageSize, sortBy, includeCompleted, currentActiveTopic(s));
+ return selectSidebarTopics(
+ topics,
+ pageSize,
+ sortBy,
+ includeCompleted,
+ sidebarActiveTopic(s, includeCompleted),
+ );
};
export const selectSidebarTopics = (
diff --git a/src/store/chat/store.ts b/src/store/chat/store.ts
index f93037eab3c..ebdc76820a4 100644
--- a/src/store/chat/store.ts
+++ b/src/store/chat/store.ts
@@ -29,6 +29,7 @@ import { type ChatThreadAction } from './slices/thread/action';
import { ChatThreadActionImpl } from './slices/thread/action';
import { type ChatTopicAction } from './slices/topic/action';
import { ChatTopicActionImpl } from './slices/topic/action';
+import { subscribeJustArchivedTopic } from './slices/topic/justArchived';
import { type ChatTranslateAction } from './slices/translate/action';
import { ChatTranslateActionImpl } from './slices/translate/action';
import { type VoiceMessageAction } from './slices/voiceMessage/action';
@@ -100,6 +101,8 @@ export const useChatStore = createWithEqualityFn<ChatStore>()(
shallow,
);
+subscribeJustArchivedTopic(useChatStore);
+
expose('chat', useChatStore);
export const getChatStoreState = () => useChatStore.getState();