Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(tabs): switch to the background tab holding a selected entity (#9548)
Eric Hayes13b1cb1d8137
diff --git a/apps/web/src/components/app/createPreviewSelectionGuard.test.tsx b/apps/web/src/components/app/createPreviewSelectionGuard.test.tsxindex aa46d76030..ac7227634f 100644--- a/apps/web/src/components/app/createPreviewSelectionGuard.test.tsx+++ b/apps/web/src/components/app/createPreviewSelectionGuard.test.tsx@@ -2,6 +2,7 @@ import {NavigationStack,useNavigationStack,} from '@app/components/navigation-stack/NavigationStack';+import { setGlobalTabManager } from '@app/signal/tabLayout';import { toast } from '@core/component/Toast/Toast';import { render } from '@solidjs/testing-library';import {@@ -11,12 +12,13 @@ import {createSignal,type ParentProps,} from 'solid-js';-import { beforeEach, expect, it, vi } from 'vitest';+import { beforeEach, expect, it, onTestFinished, vi } from 'vitest';import { createPreviewSelectionGuard } from './createPreviewSelectionGuard';import type { PreviewPanelSelection } from './previewTarget';import { createContentInstanceRegistry } from './split-layout/contentInstanceRegistry';import { SplitLayoutContext } from './split-layout/context';import type { SplitManager } from './split-layout/layoutManager';+import type { TabManager } from './tab-layout/tab-manager';let manager: Pick<SplitManager,@@ -28,7 +30,7 @@ const activate = vi.hoisted(() => vi.fn());vi.mock('@app/signal/splitLayout', () => ({ globalSplitManager: vi.fn() }));vi.mock('@core/component/Toast/Toast', () => ({ toast: { alert: vi.fn() } }));vi.mock('./split-layout/layoutUtils', () => ({- useSplitPanelOrThrow: () => ({ handle: { activate } }),+ useSplitPanelOrThrow: () => ({ handle: { id: 'split-1', activate } }),}));vi.mock('@core/constant/allBlocks', () => ({resolveBlockAlias: (type: string) => (type === 'task' ? 'md' : type),@@ -143,6 +145,51 @@ it('does not move focus or toast for a conflicting restored selection', () => {first.unmount();});+it('brings up the background tab holding a selection instead of its hidden split', () => {+ const revealSplit = vi.fn(() => true);+ setGlobalTabManager({ revealSplit } as unknown as TabManager);+ onTestFinished(() => setGlobalTabManager(undefined));+ const channel = { type: 'channel', id: 'channel' } as const;+ const background = setup();+ const live = setup();+ background.stack.reset(channel);++ expect(live.selectionGuard.canSelect(channel)).toBe(false);+ expect(revealSplit).toHaveBeenCalledWith('split-1');+ expect(activate).not.toHaveBeenCalled();+ live.unmount();+ background.unmount();+});++it('leaves a selection already in the URL passive as its view mounts', () => {+ const channel = { type: 'channel', id: 'channel' } as const;+ const holder = setup();+ holder.stack.reset(channel);+ const [selection, setSelection] =+ createSignal<PreviewPanelSelection>(channel);++ function RouteHost() {+ const selectPreview = createPreviewSelectionGuard();+ createEffect(() => selectPreview(selection()));+ return null;+ }+ const mounted = render(() => (+ <Layout>+ <RouteHost />+ </Layout>+ ));+ expect(activate).not.toHaveBeenCalled();+ expect(toast.alert).not.toHaveBeenCalled();++ // Later route changes are navigation again.+ setSelection({ type: 'email', id: 'free' });+ setSelection(channel);+ expect(activate).toHaveBeenCalledOnce();+ expect(toast.alert).toHaveBeenCalledOnce();+ mounted.unmount();+ holder.unmount();+});+it('uses the change reason independently of mount timing', () => {const channel = { type: 'channel', id: 'channel' } as const;const first = setup([channel]);diff --git a/apps/web/src/components/app/createPreviewSelectionGuard.ts b/apps/web/src/components/app/createPreviewSelectionGuard.tsindex 696c3e6ce5..d940ab9833 100644--- a/apps/web/src/components/app/createPreviewSelectionGuard.ts+++ b/apps/web/src/components/app/createPreviewSelectionGuard.ts@@ -1,4 +1,5 @@import type { NavigationStackChangeReason } from '@app/components/navigation-stack/NavigationStack';+import { globalTabManager } from '@app/signal/tabLayout';import { goToChannelMessage } from '@block-channel/utils/link';import { toast } from '@core/component/Toast/Toast';import { fireAndForget } from '@core/util/fireAndForget';@@ -34,11 +35,16 @@ export function createPreviewSelectionGuard(): PreviewSelectionGuard {const panel = useSplitPanelOrThrow();const owner = Symbol('inline-preview');const [current, setCurrent] = createSignal<ContentIdentity>();+ const activate = () => {+ // A background desktop tab stays mounted and keeps its selection, so+ // showing that selection means bringing the tab up.+ if (globalTabManager()?.revealSplit(panel.handle.id)) return;++ panel.handle.activate();+ };const unregister = manager.registerOpenViews(() => {const content = current();- return content- ? [{ owner, content, activate: () => panel.handle.activate() }]- : [];+ return content ? [{ owner, content, activate }] : [];});onCleanup(unregister);@@ -98,7 +104,16 @@ export function createPreviewSelectionGuard(): PreviewSelectionGuard {}return true;});- const select: SelectPreview = (selection, reason) => {+ // A host's route effect makes the first call as its view mounts. That+ // selection was already in the URL (a restored tab or layout), so a conflict+ // yields quietly: moving focus would bounce the user out of the tab they just+ // opened.+ let viewMounting = true;+ const select: SelectPreview = (+ selection,+ reason = viewMounting ? 'restore' : 'navigate'+ ) => {+ viewMounting = false;if (!canSelect(selection, reason)) return false;setCurrent(identity(targetFor(selection)));return true;diff --git a/apps/web/src/components/app/tab-layout/tab-manager.ts b/apps/web/src/components/app/tab-layout/tab-manager.tsindex 7ef775978f..2a2802d149 100644--- a/apps/web/src/components/app/tab-layout/tab-manager.ts+++ b/apps/web/src/components/app/tab-layout/tab-manager.ts@@ -436,6 +436,23 @@ export function createTabManager(options: TabManagerOptions) {fireAndForget(activateTab(id), SURFACE);},+ /**+ * Brings up the background tab holding a parked split, focused on that+ * split. False when no background tab holds it, so the split is live.+ */+ revealSplit(id: SplitId): boolean {+ const holder = untrack(tabs).find(+ (tab) =>+ tab.id !== untrack(activeTabId) &&+ parkedPanes(tab).some((pane) => (pane as string) === id)+ );+ if (holder?.stored.kind !== 'parked') return false;++ setTab({ ...holder, stored: { ...holder.stored, activeSplit: id } });+ fireAndForget(activateTab(holder.id), SURFACE);+ return true;+ },+closeTab(id: DesktopTabId): void {const list = untrack(tabs);if (list.length <= 1) return;diff --git a/apps/web/src/components/app/tab-layout/tests/tab-layers.test.tsx b/apps/web/src/components/app/tab-layout/tests/tab-layers.test.tsxindex 79ab44b309..89bcc4ee75 100644--- a/apps/web/src/components/app/tab-layout/tests/tab-layers.test.tsx+++ b/apps/web/src/components/app/tab-layout/tests/tab-layers.test.tsx@@ -17,10 +17,16 @@ import {notFoundRoute,} from '@app/routes/routes';import { setGlobalTabManager } from '@app/signal/tabLayout';-import { SplitLayoutContext } from '@components/app/split-layout/context';+import { createPreviewSelectionGuard } from '@components/app/createPreviewSelectionGuard';+import {+ SplitLayoutContext,+ SplitPanelContext,+ type SplitPanelContextType,+} from '@components/app/split-layout/context';import {createSplitLayout,type SplitManager,+ splitOf,} from '@components/app/split-layout/layoutManager';import { SplitLayout } from '@components/app/split-layout/SplitLayout';import { createAppPanePolicy } from '@components/app/split-layout/split-router/app-pane-policy';@@ -28,6 +34,7 @@ import {resolveContentLocation,splitContentFromLocation,} from '@components/app/split-layout/split-router/legacy-route';+import { toast } from '@core/component/Toast/Toast';import type { BlockOrchestrator } from '@core/orchestrator';import { cleanup, render } from '@solidjs/testing-library';import { onCleanup } from 'solid-js';@@ -103,7 +110,27 @@ function setup(url: string) {</SplitRouterProvider>);});- return { router, tabManager };+ return { router, tabManager, manager };+}++/** A view's preview guard, as a host mounted in the router's first open pane creates it. */+function guardInFirstPane(router: SplitRouter, manager: SplitManager) {+ const handle = manager.getSplit(splitOf(router.panes()[0]!));+ let guard!: ReturnType<typeof createPreviewSelectionGuard>;+ function Host() {+ guard = createPreviewSelectionGuard();+ return null;+ }+ render(() => (+ <SplitLayoutContext.Provider value={{ manager }}>+ <SplitPanelContext.Provider+ value={{ handle } as unknown as SplitPanelContextType}+ >+ <Host />+ </SplitPanelContext.Provider>+ </SplitLayoutContext.Provider>+ ));+ return guard;}async function flush(router: SplitRouter) {@@ -128,6 +155,7 @@ beforeEach(() => {afterEach(() => {vi.unstubAllGlobals();+ vi.restoreAllMocks();setGlobalTabManager(undefined);cleanup();panels.mounted = 0;@@ -166,6 +194,47 @@ describe('desktop tab layers', () => {expect(layer(second)?.inert).toBe(true);});+ it('selecting a preview held by a background tab switches to that tab', async () => {+ const alert = vi.spyOn(toast, 'alert').mockReturnValue(0);+ const { router, tabManager, manager } = setup('/channels');+ await flush(router);+ const first = tabManager.activeTabId();+ const channel = { type: 'channel', id: 'channel-two' } as const;+ expect(guardInFirstPane(router, manager)(channel, 'navigate')).toBe(true);++ tabManager.newTab();+ await flush(router);+ expect(tabManager.activeTabId()).not.toBe(first);+ const liveGuard = guardInFirstPane(router, manager);+ const activeSplit = manager.activeSplitId();++ expect(liveGuard.canSelect(channel)).toBe(false);+ await flush(router);++ expect(tabManager.activeTabId()).toBe(first);+ expect(layer(first)?.inert).toBe(false);+ expect(manager.activeSplitId()).not.toBe(activeSplit);+ expect(alert).toHaveBeenCalledOnce();+ });++ it('a tab mounting onto an entity another tab holds does not switch back', async () => {+ const alert = vi.spyOn(toast, 'alert').mockReturnValue(0);+ const { router, tabManager, manager } = setup('/channels');+ await flush(router);+ const channel = { type: 'channel', id: 'channel-two' } as const;+ expect(guardInFirstPane(router, manager)(channel, 'navigate')).toBe(true);++ tabManager.newTab();+ await flush(router);+ const second = tabManager.activeTabId();++ expect(guardInFirstPane(router, manager)(channel)).toBe(false);+ await flush(router);++ expect(tabManager.activeTabId()).toBe(second);+ expect(alert).not.toHaveBeenCalled();+ });+it('a stacked tab parks and comes back stacked without remounting', async () => {const { router, tabManager } = setup('/channels/channel-one/~/home?layout=c2pp'diff --git a/apps/web/src/components/app/tab-layout/tests/tab-manager.test.ts b/apps/web/src/components/app/tab-layout/tests/tab-manager.test.tsindex 0662dfeaf5..7eaf7db77d 100644--- a/apps/web/src/components/app/tab-layout/tests/tab-manager.test.ts+++ b/apps/web/src/components/app/tab-layout/tests/tab-manager.test.ts@@ -11,8 +11,9 @@ import {appRoutes,createTestPolicy,} from '@app/lib/split-router/tests/fixtures';+import type { SplitId } from '@components/app/split-layout/layoutManager';import { createRoot } from 'solid-js';-import { afterEach, describe, expect, it } from 'vitest';+import { afterEach, describe, expect, it, vi } from 'vitest';import {createTabManager,MAX_PARKED_TABS,@@ -27,7 +28,11 @@ const fakeHost = (): TabLayoutHost => ({activateSplit: () => {},});-function setup(url: string, userId = `user-${Math.random()}`) {+function setup(+ url: string,+ userId = `user-${Math.random()}`,+ host = fakeHost()+) {const history = createMemoryHistory(url);let created = 0;const { router, tabManager, dispose } = createRoot((dispose) => {@@ -40,7 +45,7 @@ function setup(url: string, userId = `user-${Math.random()}`) {});const tabManager = createTabManager({router,- manager: fakeHost(),+ manager: host,userId,});return { router, tabManager, dispose };@@ -116,6 +121,35 @@ describe('desktop tab manager', () => {expect(router.canGo(router.panes()[0]!, -1)).toBe(true);});+ it('reveals the tab holding a parked split and focuses that split', async () => {+ const activateSplit = vi.fn();+ const host: TabLayoutHost = {+ ...fakeHost(),+ getSplit: (id) => ({ id }) as ReturnType<TabLayoutHost['getSplit']>,+ activateSplit,+ };+ const { router, tabManager } = track(setup('/mail/t1', undefined, host));+ const [first] = tabManager.tabs();+ const parkedSplit = router.panes()[0]! as string as SplitId;++ expect(tabManager.revealSplit(parkedSplit)).toBe(false);++ tabManager.newTab();+ await flush(router);+ const liveSplit = router.panes()[0]! as string as SplitId;+ expect(tabManager.revealSplit(liveSplit)).toBe(false);+ expect(tabManager.activeTabId()).not.toBe(first!.id);++ expect(tabManager.revealSplit(parkedSplit)).toBe(true);+ await flush(router);+ expect(tabManager.activeTabId()).toBe(first!.id);+ expect(routeIds(router)).toEqual([['mail', 'mail-thread']]);+ expect(activateSplit).toHaveBeenLastCalledWith(parkedSplit);++ // Back on its own tab, the split is live again rather than held elsewhere.+ expect(tabManager.revealSplit(parkedSplit)).toBe(false);+ });+it('closing the active tab activates its neighbor and restores it', async () => {const { router, tabManager } = track(setup('/mail/t1'));const [first] = tabManager.tabs();diff --git a/docs/agent-guide/navigation.md b/docs/agent-guide/navigation.mdindex 8da8dbfbe0..f3af162355 100644--- a/docs/agent-guide/navigation.md+++ b/docs/agent-guide/navigation.md@@ -31,6 +31,11 @@ a background tab; adding Shift also switches to it. Tabs are not draggable.Tabs persist per user and restore on launch. In the browser and on otherplatforms the strip does not render.+An inline preview in a background tab stays open there, so selecting its entity+from another tab switches to that tab with the entity showing. A tab that opens+onto an entity another tab already holds shows its list instead, without moving+you or showing a toast.+Reference captures (browser fixture with the desktop gates forced):[a three-split tab beside a second tab](media/desktop-tabs/three-split-tab.png)and [tabs restored after a reload](media/desktop-tabs/restored-tabs.png).