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.

✨ feat(desktop): always open a fresh tab on the "+" button (#16190)

Innei9a7a204baf88
diff --git a/src/features/Electron/navigation/useTabNavigation.ts b/src/features/Electron/navigation/useTabNavigation.ts
index 6a5dbfa2cad..397444dfb47 100644
--- a/src/features/Electron/navigation/useTabNavigation.ts
+++ b/src/features/Electron/navigation/useTabNavigation.ts
@@ -33,17 +33,25 @@ export const useTabNavigation = () => {
if (prevLocationRef.current === currentUrl) return;
prevLocationRef.current = currentUrl;
- const id = normalizeTabUrl(currentUrl);
+ const normalized = normalizeTabUrl(currentUrl);
const { tabs, activeTabId } = useElectronStore.getState();
- const existing = tabs.find((t) => t.id === id);
+ const activeTab = activeTabId ? tabs.find((t) => t.id === activeTabId) : null;
+ if (activeTab && normalizeTabUrl(activeTab.url) === normalized) {
+ // Keep the active tab's url in sync (e.g. query/hash variations that
+ // normalize the same) without re-activating.
+ if (activeTab.url !== currentUrl) updateTab(activeTab.id, currentUrl);
+ return;
+ }
+
+ const existing = tabs.find((t) => normalizeTabUrl(t.url) === normalized);
if (existing) {
- if (existing.id !== activeTabId) activateTab(existing.id);
+ activateTab(existing.id);
return;
}
- if (activeTabId && tabs.some((t) => t.id === activeTabId)) {
- updateTab(activeTabId, currentUrl);
+ if (activeTab) {
+ updateTab(activeTab.id, currentUrl);
} else {
// First launch (or stale activeTabId): make the current page visible as a tab,
// so the tab bar and its "+" entry are always discoverable.
@@ -54,9 +62,11 @@ export const useTabNavigation = () => {
useEffect(() => {
if (!currentRouteMeta || !currentRouteMetaUrl) return;
- const { activeTabId } = useElectronStore.getState();
+ const { activeTabId, tabs } = useElectronStore.getState();
if (!activeTabId) return;
- if (activeTabId !== normalizeTabUrl(currentRouteMetaUrl)) return;
+ const activeTab = tabs.find((t) => t.id === activeTabId);
+ if (!activeTab) return;
+ if (normalizeTabUrl(activeTab.url) !== normalizeTabUrl(currentRouteMetaUrl)) return;
updateTabCache(activeTabId, currentRouteMeta);
}, [currentRouteMeta, currentRouteMetaUrl, updateTabCache]);
diff --git a/src/features/Electron/titlebar/TabBar/hooks/useResolvedTabs.ts b/src/features/Electron/titlebar/TabBar/hooks/useResolvedTabs.ts
index 50a0681657a..caf892fa44c 100644
--- a/src/features/Electron/titlebar/TabBar/hooks/useResolvedTabs.ts
+++ b/src/features/Electron/titlebar/TabBar/hooks/useResolvedTabs.ts
@@ -31,11 +31,14 @@ export const resolveTab = (
isActive: boolean,
t: Translate,
liveDynamic?: DynamicRouteMeta | null,
- liveDynamicTabId?: string | null,
+ liveDynamicUrl?: string | null,
): ResolvedTab => {
const staticMeta = matchRouteMeta(routes, tab.url).static;
- const live = isActive && liveDynamicTabId === tab.id ? liveDynamic : undefined;
+ const live =
+ isActive && liveDynamicUrl && normalizeTabUrl(tab.url) === normalizeTabUrl(liveDynamicUrl)
+ ? liveDynamic
+ : undefined;
const title =
pickMeaningful(live?.title) ??
@@ -68,7 +71,6 @@ export const useResolvedTabs = (): UseResolvedTabsResult => {
const currentRouteMetaUrl = useElectronStore((s) => s.currentRouteMetaUrl);
const translate = t as unknown as Translate;
- const currentRouteMetaTabId = currentRouteMetaUrl ? normalizeTabUrl(currentRouteMetaUrl) : null;
const tabs = useMemo(
() =>
@@ -79,10 +81,10 @@ export const useResolvedTabs = (): UseResolvedTabsResult => {
tab.id === activeTabId,
translate,
currentRouteMeta,
- currentRouteMetaTabId,
+ currentRouteMetaUrl,
),
),
- [tabRefs, activeTabId, currentRouteMeta, currentRouteMetaTabId, translate],
+ [tabRefs, activeTabId, currentRouteMeta, currentRouteMetaUrl, translate],
);
return { activeTabId, tabs };
diff --git a/src/features/Electron/titlebar/TabBar/index.tsx b/src/features/Electron/titlebar/TabBar/index.tsx
index 921cc658161..0368c0446cc 100644
--- a/src/features/Electron/titlebar/TabBar/index.tsx
+++ b/src/features/Electron/titlebar/TabBar/index.tsx
@@ -32,7 +32,6 @@ import TabItem from './TabItem';
const TAB_WIDTH = 180;
const TAB_GAP = 0;
-// The "+" button always opens a fresh Home tab, regardless of the active page.
const NEW_TAB_URL = '/';
// Tabs only reorder along the horizontal axis, so lock the drag transform to X.
@@ -48,7 +47,7 @@ const TabBar = () => {
const scrolledActiveTabIdRef = useRef<string | null>(null);
const { tabs, activeTabId } = useResolvedTabs();
const activateTab = useElectronStore((s) => s.activateTab);
- const addTab = useElectronStore((s) => s.addTab);
+ const addNewTab = useElectronStore((s) => s.addNewTab);
const removeTab = useElectronStore((s) => s.removeTab);
const closeOtherTabs = useElectronStore((s) => s.closeOtherTabs);
const closeLeftTabs = useElectronStore((s) => s.closeLeftTabs);
@@ -177,11 +176,10 @@ const TabBar = () => {
const handleNewTab = useCallback(() => {
if (!canCreate) return;
- // Always open a fresh Home tab. If a Home tab already exists, addTab just
- // activates it instead of stacking duplicates.
- addTab(NEW_TAB_URL, undefined, true);
+ // Always open a fresh Home tab, even if a Home tab already exists.
+ addNewTab(NEW_TAB_URL);
startTransition(() => navigate(NEW_TAB_URL));
- }, [canCreate, addTab, navigate]);
+ }, [canCreate, addNewTab, navigate]);
useWatchBroadcast('createNewTab', () => {
handleNewTab();
diff --git a/src/store/electron/actions/__tests__/tabPages.test.ts b/src/store/electron/actions/__tests__/tabPages.test.ts
index 1de075e84a6..a115d0b7efa 100644
--- a/src/store/electron/actions/__tests__/tabPages.test.ts
+++ b/src/store/electron/actions/__tests__/tabPages.test.ts
@@ -23,16 +23,18 @@ describe('tabPages actions', () => {
});
describe('addTab', () => {
- it('uses the normalized URL as the tab id', () => {
+ it('creates a tab with a stable generated id and activates it', () => {
const { result } = renderHook(() => useElectronStore());
+ let createdId = '';
act(() => {
- result.current.addTab('/agent/abc?b=2&a=1');
+ createdId = result.current.addTab('/agent/abc?b=2&a=1');
});
expect(result.current.tabs).toHaveLength(1);
- expect(result.current.tabs[0].id).toBe('/agent/abc?a=1&b=2');
- expect(result.current.activeTabId).toBe('/agent/abc?a=1&b=2');
+ expect(result.current.tabs[0].id).toBe(createdId);
+ expect(result.current.tabs[0].url).toBe('/agent/abc?b=2&a=1');
+ expect(result.current.activeTabId).toBe(createdId);
});
it('dedupes tabs that resolve to the same normalized URL', () => {
@@ -58,8 +60,26 @@ describe('tabPages actions', () => {
});
});
+ describe('addNewTab', () => {
+ it('always creates a fresh tab even when the URL already has a tab', () => {
+ const { result } = renderHook(() => useElectronStore());
+
+ act(() => {
+ result.current.addNewTab('/');
+ result.current.addNewTab('/');
+ result.current.addNewTab('/');
+ });
+
+ expect(result.current.tabs).toHaveLength(3);
+ const ids = result.current.tabs.map((t) => t.id);
+ expect(new Set(ids).size).toBe(3);
+ expect(result.current.tabs.every((t) => t.url === '/')).toBe(true);
+ expect(result.current.activeTabId).toBe(result.current.tabs[2].id);
+ });
+ });
+
describe('updateTab', () => {
- it('drops cached data when the tab navigates to a different page', () => {
+ it('drops cached data when the tab navigates to a different page but keeps the id stable', () => {
const { result } = renderHook(() => useElectronStore());
const agentTab = buildTab('/agent/abc', { title: 'Claude Code' });
@@ -72,9 +92,10 @@ describe('tabPages actions', () => {
});
const updatedTab = result.current.tabs[0];
- expect(updatedTab.id).toBe('/');
+ expect(updatedTab.id).toBe(agentTab.id);
+ expect(updatedTab.url).toBe('/');
expect(updatedTab.cached).toBeUndefined();
- expect(result.current.activeTabId).toBe('/');
+ expect(result.current.activeTabId).toBe(agentTab.id);
});
it('keeps cached data when the normalized URL is unchanged', () => {
diff --git a/src/store/electron/actions/tabPages.ts b/src/store/electron/actions/tabPages.ts
index a46c503b6da..06eac7ec67d 100644
--- a/src/store/electron/actions/tabPages.ts
+++ b/src/store/electron/actions/tabPages.ts
@@ -1,3 +1,5 @@
+import { nanoid } from 'nanoid';
+
import { guardedMergeCache } from '@/features/Electron/titlebar/TabBar/resolveRouteMeta';
import { getTabPages, saveTabPages } from '@/features/Electron/titlebar/TabBar/storage';
import { type TabItem } from '@/features/Electron/titlebar/TabBar/types';
@@ -7,6 +9,8 @@ import { type StoreSetter } from '@/store/types';
import { type ElectronStore } from '../store';
+const generateTabId = (): string => `tab_${nanoid(8)}`;
+
// ======== Types ======== //
export interface TabPagesState {
@@ -46,33 +50,23 @@ export class TabPagesActionImpl {
};
addTab = (url: string, cached?: DynamicRouteMeta, activate = true): string => {
- const id = normalizeTabUrl(url);
+ const normalized = normalizeTabUrl(url);
const { tabs } = this.#get();
- const existing = tabs.find((t) => t.id === id);
+ const existing = tabs.find((t) => normalizeTabUrl(t.url) === normalized);
if (existing) {
if (activate) {
- this.#set({ activeTabId: id }, false, 'activateExistingTab');
+ this.#set({ activeTabId: existing.id }, false, 'activateExistingTab');
this.#persist();
}
- return id;
+ return existing.id;
}
- const newTab: TabItem = {
- cached,
- id,
- lastVisited: Date.now(),
- url,
- };
+ return this.#createTab(url, cached, activate);
+ };
- const newTabs = [...tabs, newTab];
- this.#set(
- { activeTabId: activate ? id : this.#get().activeTabId, tabs: newTabs },
- false,
- 'addTab',
- );
- this.#persist();
- return id;
+ addNewTab = (url: string, cached?: DynamicRouteMeta): string => {
+ return this.#createTab(url, cached, true);
};
getActiveTab = (): TabItem | null => {
@@ -157,27 +151,24 @@ export class TabPagesActionImpl {
};
updateTab = (id: string, url: string): string => {
- const { tabs, activeTabId } = this.#get();
+ const { tabs } = this.#get();
const index = tabs.findIndex((t) => t.id === id);
if (index < 0) return id;
- const nextId = normalizeTabUrl(url);
const prev = tabs[index];
+ const sameTarget = normalizeTabUrl(url) === normalizeTabUrl(prev.url);
const newTabs = [...tabs];
newTabs[index] = {
...prev,
- cached: nextId === prev.id ? prev.cached : undefined,
- id: nextId,
+ cached: sameTarget ? prev.cached : undefined,
lastVisited: Date.now(),
url,
};
- const newActiveTabId = activeTabId === id ? nextId : activeTabId;
-
- this.#set({ activeTabId: newActiveTabId, tabs: newTabs }, false, 'updateTab');
+ this.#set({ tabs: newTabs }, false, 'updateTab');
this.#persist();
- return nextId;
+ return id;
};
updateTabCache = (id: string, cached: DynamicRouteMeta): void => {
@@ -195,6 +186,25 @@ export class TabPagesActionImpl {
this.#persist();
};
+ #createTab = (url: string, cached: DynamicRouteMeta | undefined, activate: boolean): string => {
+ const { tabs, activeTabId } = this.#get();
+ const id = generateTabId();
+ const newTab: TabItem = {
+ cached,
+ id,
+ lastVisited: Date.now(),
+ url,
+ };
+
+ this.#set(
+ { activeTabId: activate ? id : activeTabId, tabs: [...tabs, newTab] },
+ false,
+ 'addTab',
+ );
+ this.#persist();
+ return id;
+ };
+
#persist = (): void => {
const { tabs, activeTabId } = this.#get();
saveTabPages(tabs, activeTabId);