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): stop cross-mode key pollution collapsing topic groups (#17088)

Arvin Xucdc40ecffdca
diff --git a/src/hooks/useTopicGroupCollapse.test.ts b/src/hooks/useTopicGroupCollapse.test.ts
new file mode 100644
index 00000000000..5dcff1f9276
--- /dev/null
+++ b/src/hooks/useTopicGroupCollapse.test.ts
@@ -0,0 +1,83 @@
+import { act, renderHook } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useGlobalStore } from '@/store/global';
+
+import { useTopicGroupCollapse } from './useTopicGroupCollapse';
+
+const TIME_GROUPS = ['favorite', 'today', 'yesterday', 'week'];
+const PROJECT_GROUPS = ['favorite', 'project:/repo/a', 'project:/repo/b'];
+
+const setCollapsed = (byMode: Record<string, string[]>) => {
+ useGlobalStore.setState({
+ isStatusInit: true,
+ status: { ...useGlobalStore.getState().status, collapsedTopicGroupKeysByMode: byMode },
+ });
+};
+
+beforeEach(() => {
+ vi.spyOn(useGlobalStore.getState().statusStorage, 'saveToLocalStorage').mockResolvedValue(
+ undefined,
+ );
+ setCollapsed({});
+});
+
+describe('useTopicGroupCollapse', () => {
+ it('expands every group when nothing has been collapsed yet', () => {
+ const { result } = renderHook(() => useTopicGroupCollapse('byTime', TIME_GROUPS));
+
+ expect(result.current.expandedKeys).toEqual(TIME_GROUPS);
+ });
+
+ it('keeps byProject keys from collapsing the byTime groups', () => {
+ setCollapsed({ byProject: ['project:/repo/a'] });
+
+ const { result } = renderHook(() => useTopicGroupCollapse('byTime', TIME_GROUPS));
+
+ expect(result.current.expandedKeys).toEqual(TIME_GROUPS);
+ });
+
+ it('writes collapsed keys into the current mode bucket only', () => {
+ setCollapsed({ byTime: ['week'] });
+
+ const { result } = renderHook(() => useTopicGroupCollapse('byProject', PROJECT_GROUPS));
+
+ act(() => {
+ result.current.setExpandedKeys(['favorite', 'project:/repo/b']);
+ });
+
+ expect(useGlobalStore.getState().status.collapsedTopicGroupKeysByMode).toEqual({
+ byProject: ['project:/repo/a'],
+ byTime: ['week'],
+ });
+ });
+
+ it('expands a group that appears after the collapsed list was persisted', () => {
+ setCollapsed({ byProject: ['project:/repo/a'] });
+
+ const { result } = renderHook(() =>
+ useTopicGroupCollapse('byProject', [...PROJECT_GROUPS, 'project:/repo/new']),
+ );
+
+ expect(result.current.expandedKeys).toEqual([
+ 'favorite',
+ 'project:/repo/b',
+ 'project:/repo/new',
+ ]);
+ });
+
+ it('preserves the collapsed state of groups that are not currently rendered', () => {
+ setCollapsed({ byProject: ['project:/repo/paged-out'] });
+
+ const { result } = renderHook(() => useTopicGroupCollapse('byProject', PROJECT_GROUPS));
+
+ act(() => {
+ result.current.setExpandedKeys(['favorite', 'project:/repo/a']);
+ });
+
+ expect(useGlobalStore.getState().status.collapsedTopicGroupKeysByMode?.byProject).toEqual([
+ 'project:/repo/paged-out',
+ 'project:/repo/b',
+ ]);
+ });
+});
diff --git a/src/hooks/useTopicGroupCollapse.ts b/src/hooks/useTopicGroupCollapse.ts
new file mode 100644
index 00000000000..0fd64075857
--- /dev/null
+++ b/src/hooks/useTopicGroupCollapse.ts
@@ -0,0 +1,41 @@
+import { useCallback, useMemo } from 'react';
+
+import { useGlobalStore } from '@/store/global';
+import { systemStatusSelectors } from '@/store/global/selectors';
+import { type TopicGroupMode } from '@/types/topic';
+
+const EMPTY_KEYS: string[] = [];
+
+/**
+ * Accordion state for the topic sidebar groups, expanded by default.
+ *
+ * The persisted list holds the *collapsed* keys, bucketed per group mode: a group
+ * that only shows up later (a new project directory, a new month bucket) is absent
+ * from the list and therefore starts expanded, and `project:*` keys can never bleed
+ * into byTime, where they would match nothing and collapse every group.
+ */
+export const useTopicGroupCollapse = (mode: TopicGroupMode, groupIds: string[]) => {
+ const collapsedKeys =
+ useGlobalStore(systemStatusSelectors.collapsedTopicGroupKeys(mode)) ?? EMPTY_KEYS;
+ const updateSystemStatus = useGlobalStore((s) => s.updateSystemStatus);
+
+ const expandedKeys = useMemo(
+ () => groupIds.filter((id) => !collapsedKeys.includes(id)),
+ [groupIds, collapsedKeys],
+ );
+
+ const setExpandedKeys = useCallback(
+ (keys: string[]) => {
+ const nextCollapsed = [
+ // groups that aren't rendered right now (paged out, or emptied) keep their state
+ ...collapsedKeys.filter((id) => !groupIds.includes(id)),
+ ...groupIds.filter((id) => !keys.includes(id)),
+ ];
+
+ updateSystemStatus({ collapsedTopicGroupKeysByMode: { [mode]: nextCollapsed } });
+ },
+ [collapsedKeys, groupIds, mode, updateSystemStatus],
+ );
+
+ return { expandedKeys, setExpandedKeys };
+};
diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/ToggleGroups.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/ToggleGroups.tsx
index 8cdd9d31dcd..0e3a212208e 100644
--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/ToggleGroups.tsx
+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/ToggleGroups.tsx
@@ -4,6 +4,7 @@ import { Maximize2, Minimize2 } from 'lucide-react';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
+import { useTopicGroupCollapse } from '@/hooks/useTopicGroupCollapse';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
import { useGlobalStore } from '@/store/global';
@@ -25,14 +26,8 @@ const ToggleGroups = memo(() => {
);
const groupTopics = useChatStore(groupSelector, isEqual);
- const [topicGroupKeys, updateSystemStatus] = useGlobalStore((s) => [
- systemStatusSelectors.topicGroupKeys(s),
- s.updateSystemStatus,
- ]);
-
const groupIds = useMemo(() => groupTopics.map((group) => group.id), [groupTopics]);
- // undefined means "default all expanded", so treat it as fully expanded
- const expandedKeys = topicGroupKeys ?? groupIds;
+ const { expandedKeys, setExpandedKeys } = useTopicGroupCollapse(topicGroupMode, groupIds);
const isAllCollapsed = expandedKeys.length === 0;
// flat mode renders FlatMode (no accordion), so the toggle has nothing to affect;
@@ -44,7 +39,7 @@ const ToggleGroups = memo(() => {
icon={isAllCollapsed ? Maximize2 : Minimize2}
size={'small'}
title={isAllCollapsed ? t('sidebar.expandAll') : t('sidebar.collapseAll')}
- onClick={() => updateSystemStatus({ expandTopicGroupKeys: isAllCollapsed ? groupIds : [] })}
+ onClick={() => setExpandedKeys(isAllCollapsed ? groupIds : [])}
/>
);
});
diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx
index 8e9f5576b4a..c365bb3e00a 100644
--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx
+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx
@@ -3,13 +3,14 @@
import { Accordion, Flexbox } from '@lobehub/ui';
import isEqual from 'fast-deep-equal';
import { MoreHorizontal } from 'lucide-react';
-import { type ComponentType, memo, useEffect, useMemo } from 'react';
+import { type ComponentType, memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router';
import urlJoin from 'url-join';
import NavItem from '@/features/NavPanel/components/NavItem';
import SkeletonList from '@/features/NavPanel/components/SkeletonList';
+import { useTopicGroupCollapse } from '@/hooks/useTopicGroupCollapse';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
import { useGlobalStore } from '@/store/global';
@@ -51,26 +52,15 @@ const GroupedAccordion = memo<GroupedAccordionProps>(({ GroupItem }) => {
);
const groupTopics = useChatStore(groupSelector, isEqual);
- const [topicGroupKeys, updateSystemStatus] = useGlobalStore((s) => [
- systemStatusSelectors.topicGroupKeys(s),
- s.updateSystemStatus,
- ]);
-
- useEffect(() => {
- updateSystemStatus({ expandTopicGroupKeys: undefined });
- }, [topicSortBy, topicGroupMode, updateSystemStatus]);
-
- const expandedKeys = useMemo(
- () => topicGroupKeys || groupTopics.map((group) => group.id),
- [topicGroupKeys, groupTopics],
- );
+ const groupIds = useMemo(() => groupTopics.map((group) => group.id), [groupTopics]);
+ const { expandedKeys, setExpandedKeys } = useTopicGroupCollapse(topicGroupMode, groupIds);
return (
<Flexbox gap={2}>
<Accordion
expandedKeys={expandedKeys}
gap={2}
- onExpandedChange={(keys) => updateSystemStatus({ expandTopicGroupKeys: keys as any })}
+ onExpandedChange={(keys) => setExpandedKeys(keys as string[])}
>
{groupTopics.map((group) => (
<GroupItem
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx
index 54ced5e0234..4ba189fbff3 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx
@@ -3,11 +3,12 @@
import { Accordion, Flexbox } from '@lobehub/ui';
import isEqual from 'fast-deep-equal';
import { MoreHorizontal } from 'lucide-react';
-import { memo, useEffect, useMemo } from 'react';
+import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import NavItem from '@/features/NavPanel/components/NavItem';
import SkeletonList from '@/features/NavPanel/components/SkeletonList';
+import { useTopicGroupCollapse } from '@/hooks/useTopicGroupCollapse';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
import { useGlobalStore } from '@/store/global';
@@ -36,19 +37,8 @@ const ByTimeMode = memo(() => {
);
const groupTopics = useChatStore(groupSelector, isEqual);
- const [topicGroupKeys, updateSystemStatus] = useGlobalStore((s) => [
- systemStatusSelectors.topicGroupKeys(s),
- s.updateSystemStatus,
- ]);
-
- // Reset expanded keys when grouping changes so all groups start expanded
- useEffect(() => {
- updateSystemStatus({ expandTopicGroupKeys: undefined });
- }, [topicSortBy, topicGroupMode, updateSystemStatus]);
-
- const expandedKeys = useMemo(() => {
- return topicGroupKeys || groupTopics.map((group) => group.id);
- }, [topicGroupKeys, groupTopics]);
+ const groupIds = useMemo(() => groupTopics.map((group) => group.id), [groupTopics]);
+ const { expandedKeys, setExpandedKeys } = useTopicGroupCollapse(topicGroupMode, groupIds);
return (
<Flexbox gap={2}>
@@ -56,7 +46,7 @@ const ByTimeMode = memo(() => {
<Accordion
expandedKeys={expandedKeys}
gap={2}
- onExpandedChange={(keys) => updateSystemStatus({ expandTopicGroupKeys: keys as any })}
+ onExpandedChange={(keys) => setExpandedKeys(keys as string[])}
>
{groupTopics.map((group) => (
<GroupItem
diff --git a/src/store/global/initialState.ts b/src/store/global/initialState.ts
index 5f14446a1c8..c0eff4567a5 100644
--- a/src/store/global/initialState.ts
+++ b/src/store/global/initialState.ts
@@ -4,6 +4,7 @@ import { type MigrationSQL, type MigrationTableItem } from '@/types/clientDB';
import { DatabaseLoadingState } from '@/types/clientDB';
import { type LocaleMode } from '@/types/locale';
import { SessionDefaultGroup } from '@/types/session';
+import { type TopicGroupMode } from '@/types/topic';
import { AsyncLocalStorage } from '@/utils/localStorage';
export enum SidebarTabKey {
@@ -127,6 +128,14 @@ export interface SystemStatus {
*/
agentPageSize?: number;
chatInputHeight?: number;
+ /**
+ * Which topicGroups are collapsed, bucketed by group mode. We persist the
+ * collapsed keys rather than the expanded ones so a group that shows up later
+ * (a new project directory, a new month bucket) starts expanded; and bucketing
+ * per mode keeps `project:*` keys from leaking into byTime, where nothing would
+ * match and every group would render collapsed.
+ */
+ collapsedTopicGroupKeysByMode?: Partial<Record<TopicGroupMode, string[]>>;
disabledModelProvidersSortType?: string;
disabledModelsSortType?: string;
/**
@@ -145,8 +154,6 @@ export interface SystemStatus {
expandInputActionbar?: boolean;
// which sessionGroup should expand
expandSessionGroupKeys: string[];
- // which topicGroup should expand
- expandTopicGroupKeys?: string[];
fileManagerViewMode?: 'list' | 'masonry';
filePanelWidth: number;
/**
diff --git a/src/store/global/selectors/systemStatus.ts b/src/store/global/selectors/systemStatus.ts
index 758a3b7eb1c..7dcce70eb85 100644
--- a/src/store/global/selectors/systemStatus.ts
+++ b/src/store/global/selectors/systemStatus.ts
@@ -1,3 +1,5 @@
+import { type TopicGroupMode } from '@/types/topic';
+
import type {
GlobalState,
ModelDetailPanelExpandedKey,
@@ -83,7 +85,10 @@ const sessionGroupKeys =
return value || INITIAL_STATUS.expandSessionGroupKeys;
};
-const topicGroupKeys = (s: GlobalState): string[] | undefined => s.status.expandTopicGroupKeys;
+const collapsedTopicGroupKeys =
+ (mode: TopicGroupMode) =>
+ (s: GlobalState): string[] | undefined =>
+ s.status.collapsedTopicGroupKeysByMode?.[mode];
const agentSidebarSections =
(agentId: string | undefined) =>
@@ -484,7 +489,7 @@ export const systemStatusSelectors = {
systemStatus,
verifyReportPanelWidth,
tokenDisplayFormatShort,
- topicGroupKeys,
+ collapsedTopicGroupKeys,
topicPageSize,
videoPanelWidth,
videoTopicViewMode,