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(sidebar): stop auto-revealing topics the user opened or created (#21356)

Inneid1ffc35de345
diff --git a/src/features/AgentSidebar/Topic/List/Item/index.tsx b/src/features/AgentSidebar/Topic/List/Item/index.tsx
index 3a71501f2ec..560ef88b2f1 100644
--- a/src/features/AgentSidebar/Topic/List/Item/index.tsx
+++ b/src/features/AgentSidebar/Topic/List/Item/index.tsx
@@ -47,6 +47,7 @@ import { useChatStore } from '@/store/chat';
import { operationSelectors } from '@/store/chat/selectors';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
+import { markSidebarTopicActivation } from '../../hooks/useScrollActiveTopicIntoView';
import { useTopicNavigation } from '../../hooks/useTopicNavigation';
import ThreadList from '../../TopicListContent/ThreadList';
import { useTopicListScope } from '../../TopicListScope';
@@ -327,6 +328,7 @@ const TopicItemRow = memo<TopicItemRowProps>(
const handleClick = useCallback(
(event?: MouseEvent) => {
if (event && event.detail > 1) return;
+ if (id) markSidebarTopicActivation(id);
if (isDesktop && id) recordTopicClickOrigin(id);
void navRef.current.navigateToTopic(id);
},
@@ -335,6 +337,7 @@ const TopicItemRow = memo<TopicItemRowProps>(
const handleDoubleClick = useCallback(async () => {
if (!id || !activeAgentId || !isDesktop) return;
+ markSidebarTopicActivation(id);
if (scope && href) {
openTopicInNewTab(id, href);
void navRef.current.navigateToTopic(id);
diff --git a/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.test.ts b/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.test.ts
index 9ed0c54675b..22a84bc3e44 100644
--- a/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.test.ts
+++ b/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.test.ts
@@ -5,7 +5,12 @@ import { act, renderHook } from '@testing-library/react';
import type { MutableRefObject } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { useScrollActiveTopicIntoView } from './useScrollActiveTopicIntoView';
+import { useChatStore } from '@/store/chat';
+
+import {
+ markSidebarTopicActivation,
+ useScrollActiveTopicIntoView,
+} from './useScrollActiveTopicIntoView';
const calls: { element: Element; options: unknown }[] = [];
const originalScrollIntoView = Element.prototype.scrollIntoView;
@@ -19,8 +24,24 @@ beforeEach(() => {
afterEach(() => {
Element.prototype.scrollIntoView = originalScrollIntoView;
+ useChatStore.setState({ creatingTopicIds: [] });
});
+const mountWithRow = (topicId: string) => {
+ const container = document.createElement('div');
+ const row = document.createElement('div');
+ row.dataset.topicId = topicId;
+ container.append(row);
+
+ const hook = renderHook(({ ready }) => useScrollActiveTopicIntoView(topicId, ready), {
+ initialProps: { ready: '' },
+ });
+ act(() => {
+ (hook.result.current as MutableRefObject<HTMLDivElement | null>).current = container;
+ });
+ return hook;
+};
+
describe('useScrollActiveTopicIntoView', () => {
it('scrolls the active topic row into the nearest visible position', () => {
const container = document.createElement('div');
@@ -76,6 +97,39 @@ describe('useScrollActiveTopicIntoView', () => {
rerender({ ready: 'topic-2' });
rerender({ ready: 'topic-2:unrelated-group' });
+ expect(calls).toHaveLength(1);
+ });
+ it('does not reveal a topic the user opened from a sidebar row', () => {
+ markSidebarTopicActivation('topic-2');
+ const { rerender } = mountWithRow('topic-2');
+ rerender({ ready: 'topic-2' });
+
+ expect(calls).toHaveLength(0);
+ });
+
+ it('does not reveal a topic this client is creating for its first send', () => {
+ useChatStore.setState({ creatingTopicIds: ['topic-2'] });
+ const { rerender } = mountWithRow('topic-2');
+ rerender({ ready: 'topic-2' });
+
+ expect(calls).toHaveLength(0);
+ });
+
+ it('keeps the skip after the optimistic topic is confirmed', () => {
+ useChatStore.setState({ creatingTopicIds: ['topic-2'] });
+ const { rerender } = mountWithRow('topic-2');
+ rerender({ ready: 'topic-2' });
+ act(() => useChatStore.setState({ creatingTopicIds: [] }));
+ rerender({ ready: 'topic-2:other' });
+
+ expect(calls).toHaveLength(0);
+ });
+
+ it('still reveals an externally navigated topic after a sidebar click on another topic', () => {
+ markSidebarTopicActivation('topic-1');
+ const { rerender } = mountWithRow('topic-2');
+ rerender({ ready: 'topic-2' });
+
expect(calls).toHaveLength(1);
});
});
diff --git a/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.ts b/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.ts
index 51b3af48811..0dfb65d9f26 100644
--- a/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.ts
+++ b/src/features/AgentSidebar/Topic/hooks/useScrollActiveTopicIntoView.ts
@@ -1,18 +1,47 @@
import { useEffect, useRef } from 'react';
+import { useChatStore } from '@/store/chat';
+
+let sidebarActivatedTopicId: string | undefined;
+
+/**
+ * Records that the user opened this topic from a sidebar row. The row is already under the
+ * pointer, and activating it can move the row to another status group, so revealing it
+ * afterwards would drag the list away from where the user is working.
+ */
+export const markSidebarTopicActivation = (topicId: string) => {
+ sidebarActivatedTopicId = topicId;
+};
+
/**
* Reveals each route-selected topic once after its row becomes available.
*
+ * Only activations that come from outside the list (search results, deep links, reload) are
+ * revealed. A row the user clicked, or a topic this client created for its first send, is
+ * already where the user's attention is; revealing it would scroll the sidebar to wherever the
+ * row ends up in the current grouping, which is often the bottom.
+ *
* The list readiness signal may also change for unrelated accordion or ordering updates. Tracking
* only successful reveals prevents those updates from pulling the sidebar back to the active row.
*/
export const useScrollActiveTopicIntoView = (activeTopicId?: string | null, ready?: unknown) => {
const containerRef = useRef<HTMLDivElement>(null);
const revealedTopicIdRef = useRef<string | undefined>(undefined);
+ const isClientCreated = useChatStore(
+ (s) => !!activeTopicId && s.creatingTopicIds.includes(activeTopicId),
+ );
useEffect(() => {
if (!activeTopicId || revealedTopicIdRef.current === activeTopicId) return;
+ const fromSidebar = sidebarActivatedTopicId === activeTopicId;
+ sidebarActivatedTopicId = undefined;
+
+ if (fromSidebar || isClientCreated) {
+ revealedTopicIdRef.current = activeTopicId;
+ return;
+ }
+
const activeRow = containerRef.current?.querySelector<HTMLElement>(
`[data-topic-id="${CSS.escape(activeTopicId)}"]`,
);
@@ -20,7 +49,7 @@ export const useScrollActiveTopicIntoView = (activeTopicId?: string | null, read
activeRow.scrollIntoView({ block: 'nearest' });
revealedTopicIdRef.current = activeTopicId;
- }, [activeTopicId, ready]);
+ }, [activeTopicId, isClientCreated, ready]);
return containerRef;
};