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): raise live tab router cap to 5 and make it adjustable in DevDock (#20547)

Innei3daeb3336c1b
diff --git a/src/features/DevPanel/TabRouters/index.tsx b/src/features/DevPanel/TabRouters/index.tsx
index 9bdf8d58141..1215dcf95bb 100644
--- a/src/features/DevPanel/TabRouters/index.tsx
+++ b/src/features/DevPanel/TabRouters/index.tsx
@@ -5,6 +5,10 @@ import { createStaticStyles, cssVar, cx } from 'antd-style';
import { memo } from 'react';
import { devDockPanelStyles } from '@/features/DevDock/panelStyles';
+import {
+ DEFAULT_LIVE_TAB_ROUTER_CAP,
+ useLiveTabRouterCap,
+} from '@/features/Electron/TabHost/liveTabRouterCap';
import { useElectronStore } from '@/store/electron';
import { type TabRouterRow, useTabRouterDebug } from './useTabRouterDebug';
@@ -15,6 +19,19 @@ const styles = createStaticStyles(({ css }) => ({
text-overflow: ellipsis;
white-space: nowrap;
`,
+ capInput: css`
+ inline-size: 48px;
+ margin-inline-start: 4px;
+ padding-block: 0;
+ padding-inline: 4px;
+ border: 1px solid ${cssVar.colorBorder};
+ border-radius: 4px;
+
+ font: inherit;
+ color: ${cssVar.colorText};
+
+ background: transparent;
+ `,
cold: css`
color: ${cssVar.colorTextQuaternary};
`,
@@ -156,6 +173,7 @@ Row.displayName = 'DevTabRoutersRow';
const TabRouters = memo(() => {
const { cap, liveCount, orphanIds, rows, scopeKey } = useTabRouterDebug();
+ const setCap = useLiveTabRouterCap((s) => s.setCap);
const evictable = rows.filter((row) => row.live && !row.active);
const nextEvictedId =
@@ -169,7 +187,18 @@ const TabRouters = memo(() => {
scope <b>{scopeKey}</b> · tabs <b>{rows.length}</b> · live routers{' '}
<b className={liveCount > cap ? styles.drift : undefined}>
{liveCount}/{cap}
- </b>
+ </b>{' '}
+ · cap
+ <input
+ aria-label={'Live tab router cap'}
+ className={styles.capInput}
+ min={1}
+ step={1}
+ title={`Default ${DEFAULT_LIVE_TAB_ROUTER_CAP}`}
+ type={'number'}
+ value={cap}
+ onChange={(e) => setCap(e.target.valueAsNumber)}
+ />
{orphanIds.length > 0 && (
<span className={styles.drift}> · orphan routers {orphanIds.join(', ')}</span>
)}
diff --git a/src/features/DevPanel/TabRouters/useTabRouterDebug.ts b/src/features/DevPanel/TabRouters/useTabRouterDebug.ts
index b0c04ca8ffb..c5bc9c13d14 100644
--- a/src/features/DevPanel/TabRouters/useTabRouterDebug.ts
+++ b/src/features/DevPanel/TabRouters/useTabRouterDebug.ts
@@ -5,10 +5,8 @@ import { useEffect, useState } from 'react';
// Imported from the concrete modules, not the `TabHost` barrel: that barrel
// re-exports `TabHost.tsx`, which would drag the whole desktop route tree into
// this dev-only chunk.
-import {
- MAX_LIVE_TAB_ROUTERS,
- resolveLiveTabIds,
-} from '@/features/Electron/TabHost/resolveLiveTabIds';
+import { useLiveTabRouterCap } from '@/features/Electron/TabHost/liveTabRouterCap';
+import { resolveLiveTabIds } from '@/features/Electron/TabHost/resolveLiveTabIds';
import {
getTabHistorySnapshot,
getTabRouter,
@@ -56,10 +54,11 @@ export const useTabRouterDebug = (): TabRouterDebug => {
const tabs = useElectronStore((s) => s.tabs);
const activeTabId = useElectronStore((s) => s.activeTabId);
const activeTabScope = useElectronStore((s) => s.activeTabScope);
+ const cap = useLiveTabRouterCap((s) => s.cap);
useTick(1000);
- const shouldBeLive = new Set(resolveLiveTabIds(tabs, activeTabId, MAX_LIVE_TAB_ROUTERS));
+ const shouldBeLive = new Set(resolveLiveTabIds(tabs, activeTabId, cap));
const routerIds = getTabRouterIds();
const liveIds = new Set(routerIds);
@@ -90,7 +89,7 @@ export const useTabRouterDebug = (): TabRouterDebug => {
});
return {
- cap: MAX_LIVE_TAB_ROUTERS,
+ cap,
liveCount: routerIds.length,
orphanIds: routerIds.filter((id) => !tabs.some((tab) => tab.id === id)),
rows,
diff --git a/src/features/Electron/TabHost/TabHost.test.ts b/src/features/Electron/TabHost/TabHost.test.ts
index 9851ecbc1ae..31bce5fef57 100644
--- a/src/features/Electron/TabHost/TabHost.test.ts
+++ b/src/features/Electron/TabHost/TabHost.test.ts
@@ -8,7 +8,7 @@ import { useElectronStore } from '@/store/electron';
import { initialState } from '@/store/electron/initialState';
import { useUserStore } from '@/store/user';
-import { MAX_LIVE_TAB_ROUTERS } from './resolveLiveTabIds';
+import { useLiveTabRouterCap } from './liveTabRouterCap';
import TabHost from './TabHost';
import TabLocationReporter from './TabLocationReporter';
import {
@@ -29,6 +29,8 @@ interface Creation {
url: string;
}
+const LIVE_CAP = 3;
+
let created: Creation[];
const initialUserState = useUserStore.getState();
@@ -82,6 +84,7 @@ const setStore = (tabs: TabItem[], activeTabId: string | null) => {
beforeEach(() => {
created = [];
window.localStorage.clear();
+ useLiveTabRouterCap.setState({ cap: LIVE_CAP });
resetTabRouterManager();
setStore([], null);
useUserStore.setState({
@@ -256,7 +259,7 @@ describe('TabHost', () => {
});
it('disposes a router evicted past the LRU cap and recreates it fresh when reactivated', async () => {
- const baseTabs: TabItem[] = Array.from({ length: MAX_LIVE_TAB_ROUTERS }, (_, index) => ({
+ const baseTabs: TabItem[] = Array.from({ length: LIVE_CAP }, (_, index) => ({
id: `t${index}`,
lastVisited: 10 - index,
url: `/item/t${index}`,
@@ -270,7 +273,7 @@ describe('TabHost', () => {
act(() => useElectronStore.setState({ activeTabId: tab.id }));
await screen.findByTestId(`param-${tab.id}`);
}
- expect(created).toHaveLength(MAX_LIVE_TAB_ROUTERS);
+ expect(created).toHaveLength(LIVE_CAP);
const withEvictor: TabItem[] = [
...baseTabs,
@@ -328,7 +331,7 @@ describe('TabHost', () => {
act(() => {
useElectronStore.setState({ activeTabId: 'f0', tabs: evicted });
});
- for (const tab of fillers.slice(1, MAX_LIVE_TAB_ROUTERS)) {
+ for (const tab of fillers.slice(1, LIVE_CAP)) {
act(() => useElectronStore.setState({ activeTabId: tab.id }));
await screen.findByTestId(`param-${tab.id}`);
}
@@ -350,7 +353,7 @@ describe('TabHost', () => {
it("snapshots a hidden tab's navigated location into the store on LRU eviction", async () => {
const target: TabItem = { id: 'target', lastVisited: 1, url: '/item/target' };
- const fillers: TabItem[] = Array.from({ length: MAX_LIVE_TAB_ROUTERS - 1 }, (_, index) => ({
+ const fillers: TabItem[] = Array.from({ length: LIVE_CAP - 1 }, (_, index) => ({
id: `f${index}`,
lastVisited: 10 + index,
url: `/item/f${index}`,
diff --git a/src/features/Electron/TabHost/TabHost.tsx b/src/features/Electron/TabHost/TabHost.tsx
index 36d214bd5e1..44f1b25ac24 100644
--- a/src/features/Electron/TabHost/TabHost.tsx
+++ b/src/features/Electron/TabHost/TabHost.tsx
@@ -21,7 +21,8 @@ import { useElectronStore } from '@/store/electron';
import { useUserStore } from '@/store/user';
import { labPreferSelectors, preferenceSelectors } from '@/store/user/selectors';
-import { MAX_LIVE_TAB_ROUTERS, resolveLiveTabIds } from './resolveLiveTabIds';
+import { useLiveTabRouterCap } from './liveTabRouterCap';
+import { resolveLiveTabIds } from './resolveLiveTabIds';
import { TabIdContext } from './TabIdContext';
import {
getOrCreateTabRouter,
@@ -145,6 +146,7 @@ const TabHost = ({ createRouter = createTabRouter }: TabHostProps) => {
const focusTabPane = useElectronStore((s) => s.focusTabPane);
const setSplitRatio = useElectronStore((s) => s.setSplitRatio);
const effectiveSplitView = isPreferenceInit && splitViewEnabled ? splitView : null;
+ const liveTabCap = useLiveTabRouterCap((s) => s.cap);
const visibleTabIds = useMemo(
() =>
@@ -166,10 +168,10 @@ const TabHost = ({ createRouter = createTabRouter }: TabHostProps) => {
// Persisted tabs are cold until first shown; only retain already-created routers.
tabs.filter((tab) => visibleTabIds.includes(tab.id) || getTabRouter(tab.id)),
activeTabId,
- MAX_LIVE_TAB_ROUTERS,
+ liveTabCap,
visibleTabIds,
),
- [tabs, activeTabId, visibleTabIds],
+ [tabs, activeTabId, liveTabCap, visibleTabIds],
);
useEffect(() => {
diff --git a/src/features/Electron/TabHost/liveTabRouterCap.test.ts b/src/features/Electron/TabHost/liveTabRouterCap.test.ts
new file mode 100644
index 00000000000..35b0257cd0b
--- /dev/null
+++ b/src/features/Electron/TabHost/liveTabRouterCap.test.ts
@@ -0,0 +1,24 @@
+import { beforeEach, describe, expect, it } from 'vitest';
+
+import { DEFAULT_LIVE_TAB_ROUTER_CAP, useLiveTabRouterCap } from './liveTabRouterCap';
+
+describe('useLiveTabRouterCap', () => {
+ beforeEach(() => {
+ localStorage.clear();
+ useLiveTabRouterCap.setState({ cap: DEFAULT_LIVE_TAB_ROUTER_CAP });
+ });
+
+ it('persists a valid cap', () => {
+ useLiveTabRouterCap.getState().setCap(7);
+
+ expect(useLiveTabRouterCap.getState().cap).toBe(7);
+ expect(localStorage.getItem('LOBE_LIVE_TAB_ROUTER_CAP')).toBe('7');
+ });
+
+ it.each([0, -1, 2.5, Number.NaN])('ignores invalid cap %s', (value) => {
+ useLiveTabRouterCap.getState().setCap(value);
+
+ expect(useLiveTabRouterCap.getState().cap).toBe(DEFAULT_LIVE_TAB_ROUTER_CAP);
+ expect(localStorage.getItem('LOBE_LIVE_TAB_ROUTER_CAP')).toBeNull();
+ });
+});
diff --git a/src/features/Electron/TabHost/liveTabRouterCap.ts b/src/features/Electron/TabHost/liveTabRouterCap.ts
new file mode 100644
index 00000000000..017e8b16233
--- /dev/null
+++ b/src/features/Electron/TabHost/liveTabRouterCap.ts
@@ -0,0 +1,33 @@
+import { create } from 'zustand';
+
+const STORAGE_KEY = 'LOBE_LIVE_TAB_ROUTER_CAP';
+
+export const DEFAULT_LIVE_TAB_ROUTER_CAP = 5;
+
+const normalizeCap = (value: unknown): number | null => {
+ const cap = Number(value);
+ return Number.isInteger(cap) && cap > 0 ? cap : null;
+};
+
+const readPersisted = (): number => {
+ try {
+ return normalizeCap(localStorage.getItem(STORAGE_KEY)) ?? DEFAULT_LIVE_TAB_ROUTER_CAP;
+ } catch {
+ return DEFAULT_LIVE_TAB_ROUTER_CAP;
+ }
+};
+
+interface LiveTabRouterCapStore {
+ cap: number;
+ setCap: (cap: number) => void;
+}
+
+export const useLiveTabRouterCap = create<LiveTabRouterCapStore>((set) => ({
+ cap: readPersisted(),
+ setCap: (value) => {
+ const cap = normalizeCap(value);
+ if (cap === null) return;
+ set({ cap });
+ if (typeof localStorage !== 'undefined') localStorage.setItem(STORAGE_KEY, String(cap));
+ },
+}));
diff --git a/src/features/Electron/TabHost/resolveLiveTabIds.test.ts b/src/features/Electron/TabHost/resolveLiveTabIds.test.ts
index 98b5d235273..014b97b3592 100644
--- a/src/features/Electron/TabHost/resolveLiveTabIds.test.ts
+++ b/src/features/Electron/TabHost/resolveLiveTabIds.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { MAX_LIVE_TAB_ROUTERS, resolveLiveTabIds } from './resolveLiveTabIds';
+import { resolveLiveTabIds } from './resolveLiveTabIds';
type Tab = { id: string; lastVisited: number };
@@ -48,8 +48,4 @@ describe('resolveLiveTabIds', () => {
expect(resolveLiveTabIds(tabs, 'right', 2, ['left', 'right'])).toEqual(['left', 'right']);
});
-
- it('exposes the default cap', () => {
- expect(MAX_LIVE_TAB_ROUTERS).toBe(3);
- });
});
diff --git a/src/features/Electron/TabHost/resolveLiveTabIds.ts b/src/features/Electron/TabHost/resolveLiveTabIds.ts
index 92b7e24792a..7ed02e841ea 100644
--- a/src/features/Electron/TabHost/resolveLiveTabIds.ts
+++ b/src/features/Electron/TabHost/resolveLiveTabIds.ts
@@ -1,7 +1,5 @@
import { type TabItem } from '@/features/Electron/titlebar/TabBar/types';
-export const MAX_LIVE_TAB_ROUTERS = 3;
-
export const resolveLiveTabIds = (
tabs: Pick<TabItem, 'id' | 'lastVisited'>[],
activeTabId: string | null,