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(desktop): keep the sidebar back button from bouncing to the agent root (#23401)

Inneibd7691d86a26
diff --git a/src/features/Electron/ActiveConversationBridge/navigation.test.ts b/src/features/Electron/ActiveConversationBridge/navigation.test.ts
index ff4c5d6c837..1fc04113b08 100644
--- a/src/features/Electron/ActiveConversationBridge/navigation.test.ts
+++ b/src/features/Electron/ActiveConversationBridge/navigation.test.ts
@@ -1,7 +1,13 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { createMemoryRouter } from 'react-router';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import {
+ getOrCreateTabRouter,
+ resetTabRouterManager,
+} from '@/features/Electron/TabHost/tabRouterManager';
import { initialState as initialChatState } from '@/store/chat/initialState';
import { useChatStore } from '@/store/chat/store';
+import { useElectronStore } from '@/store/electron';
import { useProjectDirectoryStore } from '@/store/projectWorkingDirectory';
import { resolveActiveConversationCoordinate } from './coordinate';
@@ -24,6 +30,50 @@ describe('active conversation navigation', () => {
useProjectDirectoryStore.setState({ projectTopicsMap: {} }, false);
});
+ afterEach(() => {
+ resetTabRouterManager();
+ useElectronStore.setState({ activeTabId: null });
+ });
+
+ const seedActiveTabRouter = (url: string) => {
+ useElectronStore.setState({ activeTabId: 't1' });
+ getOrCreateTabRouter('t1', url, (initialUrl) =>
+ createMemoryRouter([{ element: null, path: '*' }], { initialEntries: [initialUrl] }),
+ );
+ };
+
+ it('ignores a topic change made after the tab router has already left the conversation', () => {
+ const coordinate = resolveActiveConversationCoordinate({
+ params: { aid: 'agent-a', topicId: 'topic-a' },
+ resolvedAgentId: 'agent-a',
+ url: '/agent/agent-a/topic-a',
+ });
+ seedActiveTabRouter('/page/doc-1');
+ const navigate = vi.fn();
+ const unsubscribe = subscribeActiveConversationNavigation(() => coordinate, navigate);
+
+ useChatStore.setState({ activeTopicId: null as any }, false);
+
+ expect(navigate).not.toHaveBeenCalled();
+ unsubscribe();
+ });
+
+ it('rewrites the url when the tab router is still on the conversation', () => {
+ const coordinate = resolveActiveConversationCoordinate({
+ params: { aid: 'agent-a', topicId: 'topic-a' },
+ resolvedAgentId: 'agent-a',
+ url: '/agent/agent-a/topic-a?thread=thread-a',
+ });
+ seedActiveTabRouter('/agent/agent-a/topic-a?thread=thread-a');
+ const navigate = vi.fn();
+ const unsubscribe = subscribeActiveConversationNavigation(() => coordinate, navigate);
+
+ useChatStore.setState({ activeThreadId: undefined, activeTopicId: null as any }, false);
+
+ expect(navigate).toHaveBeenCalledWith('/agent/agent-a', { replace: true });
+ unsubscribe();
+ });
+
it('turns one atomic topic and thread store change into one active-tab navigation', () => {
const coordinate = resolveActiveConversationCoordinate({
params: { aid: 'agent-a', topicId: 'topic-a' },
diff --git a/src/features/Electron/ActiveConversationBridge/navigation.ts b/src/features/Electron/ActiveConversationBridge/navigation.ts
index 797cd95d030..f4f2a2b236d 100644
--- a/src/features/Electron/ActiveConversationBridge/navigation.ts
+++ b/src/features/Electron/ActiveConversationBridge/navigation.ts
@@ -1,13 +1,28 @@
import { appNavigate } from '@/features/Electron/navigation/appNavigate';
+import { getTabRouter } from '@/features/Electron/TabHost/tabRouterManager';
+import { normalizeTabUrl } from '@/features/Electron/titlebar/TabBar/url';
import { useChatStore } from '@/store/chat';
+import { useElectronStore } from '@/store/electron';
import { type ActiveConversationCoordinate, buildActiveConversationUrl } from './coordinate';
type Navigate = (url: string, options: { replace: true }) => void;
+const readActiveTabRouterUrl = (): string | undefined => {
+ const { activeTabId } = useElectronStore.getState();
+ const location = activeTabId ? getTabRouter(activeTabId)?.state.location : undefined;
+ return location && `${location.pathname}${location.search}`;
+};
+
+const toComparableUrl = (url: string) => {
+ const { pathname, search } = new URL(url, 'https://desktop.local');
+ return normalizeTabUrl(`${pathname}${search}`);
+};
+
export const subscribeActiveConversationNavigation = (
getCoordinate: () => ActiveConversationCoordinate,
navigate: Navigate = appNavigate,
+ getRouterUrl: () => string | undefined = readActiveTabRouterUrl,
) =>
useChatStore.subscribe((state, previousState) => {
if (
@@ -24,6 +39,16 @@ export const subscribeActiveConversationNavigation = (
)
return;
+ // The coordinate follows the tab url, which TabLocationReporter updates one
+ // effect after the router moves. A page that just mounted can change the topic
+ // in that gap; rewriting from the stale coordinate would yank the tab back.
+ const routerUrl = getRouterUrl();
+ if (
+ routerUrl !== undefined &&
+ toComparableUrl(routerUrl) !== toComparableUrl(`${coordinate.pathname}${coordinate.search}`)
+ )
+ return;
+
if (state.activeTopicId === undefined && coordinate.topicId) {
useChatStore.setState(
{ activeTopicId: coordinate.topicId },