:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): always show at least one browser tab (#90105)
Adam Leith18b522ed751a
diff --git a/products/desktop/apps/code/src/renderer/di/container.ts b/products/desktop/apps/code/src/renderer/di/container.tsindex 0eaa700a3104..ce0356f4b2a7 100644--- a/products/desktop/apps/code/src/renderer/di/container.ts+++ b/products/desktop/apps/code/src/renderer/di/container.ts@@ -208,7 +208,8 @@ const browserTabsClient: BrowserTabsClient = {reset: () => trpcClient.browserTabs.reset.mutate(),openTab: (input) => trpcClient.browserTabs.openTab.mutate(input),setTabTarget: (input) => trpcClient.browserTabs.setTabTarget.mutate(input),- close: (tabId) => trpcClient.browserTabs.close.mutate({ tabId }),+ close: (tabId, newTabId) =>+ trpcClient.browserTabs.close.mutate({ tabId, newTabId }),closeMany: (input) => trpcClient.browserTabs.closeMany.mutate(input),setOrder: (input) => trpcClient.browserTabs.setOrder.mutate(input),setActiveTab: (input) => trpcClient.browserTabs.setActiveTab.mutate(input),diff --git a/products/desktop/apps/web/src/web-browser-tabs-store.ts b/products/desktop/apps/web/src/web-browser-tabs-store.tsindex 05bc67881ce6..1fe776ad580f 100644--- a/products/desktop/apps/web/src/web-browser-tabs-store.ts+++ b/products/desktop/apps/web/src/web-browser-tabs-store.ts@@ -2,6 +2,8 @@ import {type BrowserWindow,closeTab,closeTabs,+ DEFAULT_TAB_HREF,+ ensureWindowHasTab,openTab,resetTabs,setTabOrder,@@ -46,7 +48,18 @@ class WebBrowserTabsStore extends TypedEventEmitter<SnapshotChangeEvents> {super();this.setMaxListeners(0);const loaded = load();- const seeded = this.ensurePrimaryWindow(loaded);+ const withPrimaryWindow = this.ensurePrimaryWindow(loaded);+ const primaryWindow = withPrimaryWindow.windows.find(+ (window) => window.isPrimary,+ );+ const seeded = primaryWindow+ ? ensureWindowHasTab(withPrimaryWindow, {+ windowId: primaryWindow.id,+ href: DEFAULT_TAB_HREF,+ makeId,+ now,+ })+ : withPrimaryWindow;this.snapshot = seeded;if (seeded !== loaded) this.persist();}@@ -75,7 +88,7 @@ class WebBrowserTabsStore extends TypedEventEmitter<SnapshotChangeEvents> {reset(): TabsSnapshot {return this.commit(- resetTabs(this.snapshot, { href: "/spaces", makeId, now }),+ resetTabs(this.snapshot, { href: DEFAULT_TAB_HREF, makeId, now }),);}@@ -115,13 +128,28 @@ class WebBrowserTabsStore extends TypedEventEmitter<SnapshotChangeEvents> {return this.commit(setTabTarget(this.snapshot, { ...input, now }));}- close(tabId: string): TabsSnapshot {- const { snapshot } = closeTab(this.snapshot, tabId);+ close(tabId: string, newTabId: string): TabsSnapshot {+ const { snapshot } = closeTab(this.snapshot, tabId, {+ href: DEFAULT_TAB_HREF,+ makeId: () => newTabId,+ now,+ });return this.commit(snapshot);}- closeMany(tabIds: string[], focusTabId?: string | null): TabsSnapshot {- return this.commit(closeTabs(this.snapshot, tabIds, focusTabId));+ closeMany(+ tabIds: string[],+ newTabId: string,+ focusTabId?: string | null,+ ): TabsSnapshot {+ return this.commit(+ closeTabs(+ this.snapshot,+ tabIds,+ { href: DEFAULT_TAB_HREF, makeId: () => newTabId, now },+ focusTabId,+ ),+ );}setOrder(input: { windowId: string; tabIds: string[] }): TabsSnapshot {diff --git a/products/desktop/apps/web/src/web-container.ts b/products/desktop/apps/web/src/web-container.tsindex 19bea98fd52d..6fd0187d0b0c 100644--- a/products/desktop/apps/web/src/web-container.ts+++ b/products/desktop/apps/web/src/web-container.ts@@ -619,10 +619,15 @@ const webBrowserTabsClient: BrowserTabsClient = {openTab: (input) => Promise.resolve(webBrowserTabsStore.openTab(input)),setTabTarget: (input) =>Promise.resolve(webBrowserTabsStore.setTabTarget(input)),- close: (tabId) => Promise.resolve(webBrowserTabsStore.close(tabId)),+ close: (tabId, newTabId) =>+ Promise.resolve(webBrowserTabsStore.close(tabId, newTabId)),closeMany: (input) =>Promise.resolve(- webBrowserTabsStore.closeMany(input.tabIds, input.focusTabId),+ webBrowserTabsStore.closeMany(+ input.tabIds,+ input.newTabId,+ input.focusTabId,+ ),),setOrder: (input) => Promise.resolve(webBrowserTabsStore.setOrder(input)),setActiveTab: (input) =>diff --git a/products/desktop/apps/web/src/web-host-router.ts b/products/desktop/apps/web/src/web-host-router.tsindex c5e986548e9c..e66101a5099c 100644--- a/products/desktop/apps/web/src/web-host-router.ts+++ b/products/desktop/apps/web/src/web-host-router.ts@@ -465,19 +465,26 @@ const browserTabsRouter = router({.output(tabsSnapshotSchema).mutation(({ input }) => webBrowserTabsStore.setTabTarget(input)),close: publicProcedure- .input(z.object({ tabId: z.string() }))+ .input(z.object({ tabId: z.string(), newTabId: z.string() })).output(tabsSnapshotSchema)- .mutation(({ input }) => webBrowserTabsStore.close(input.tabId)),+ .mutation(({ input }) =>+ webBrowserTabsStore.close(input.tabId, input.newTabId),+ ),closeMany: publicProcedure.input(z.object({tabIds: z.array(z.string()),+ newTabId: z.string(),focusTabId: z.string().nullable().default(null),}),).output(tabsSnapshotSchema).mutation(({ input }) =>- webBrowserTabsStore.closeMany(input.tabIds, input.focusTabId),+ webBrowserTabsStore.closeMany(+ input.tabIds,+ input.newTabId,+ input.focusTabId,+ ),),setOrder: publicProcedure.input(z.object({ windowId: z.string(), tabIds: z.array(z.string()) }))diff --git a/products/desktop/packages/host-router/src/routers/browser-tabs.router.ts b/products/desktop/packages/host-router/src/routers/browser-tabs.router.tsindex 671438e47620..d00421c2875b 100644--- a/products/desktop/packages/host-router/src/routers/browser-tabs.router.ts+++ b/products/desktop/packages/host-router/src/routers/browser-tabs.router.ts@@ -42,13 +42,19 @@ export const browserTabsRouter = router({close: publicProcedure.input(closeTabInput).output(browserTabsSnapshotOutput)- .mutation(({ ctx, input }) => svc(ctx.container).close(input.tabId)),+ .mutation(({ ctx, input }) =>+ svc(ctx.container).close(input.tabId, input.newTabId),+ ),closeMany: publicProcedure.input(closeTabsInput).output(browserTabsSnapshotOutput).mutation(({ ctx, input }) =>- svc(ctx.container).closeMany(input.tabIds, input.focusTabId),+ svc(ctx.container).closeMany(+ input.tabIds,+ input.newTabId,+ input.focusTabId,+ ),),setOrder: publicProcedurediff --git a/products/desktop/packages/shared/src/browser-tabs.test.ts b/products/desktop/packages/shared/src/browser-tabs.test.tsindex 1a9af48bcbfe..adc5afd3daca 100644--- a/products/desktop/packages/shared/src/browser-tabs.test.ts+++ b/products/desktop/packages/shared/src/browser-tabs.test.ts@@ -1,7 +1,8 @@import { describe, expect, it } from "vitest";import {- closeTab,- closeTabs,+ closeTabs as closeTabsTransform,+ closeTab as closeTabTransform,+ DEFAULT_TAB_HREF,decideTabNavigation,openTab,POSITION_GAP,@@ -66,6 +67,27 @@ function openAt(s: TabsSnapshot, windowId: string, href: string) {});}+function closeTab(snapshot: TabsSnapshot, tabId: string) {+ return closeTabTransform(snapshot, tabId, {+ href: DEFAULT_TAB_HREF,+ makeId,+ now,+ });+}++function closeTabs(+ snapshot: TabsSnapshot,+ tabIds: string[],+ focusTabId?: string | null,+) {+ return closeTabsTransform(+ snapshot,+ tabIds,+ { href: DEFAULT_TAB_HREF, makeId, now },+ focusTabId,+ );+}+/** A navigation to `href` while `activeTab` is focused. */function navigate(input: {href: string;@@ -388,12 +410,18 @@ describe("closeTab", () => {expect(r.snapshot.windows.map((w) => w.id)).toEqual(["w1"]);});- it("shows the landing (null active) when the primary's last tab closes", () => {+ it("opens a fresh tab when the primary's last tab closes", () => {const t = open(snapshot(), "w1", "dash-a");const r = closeTab(t.snapshot, t.tabId);expect(r.closedWindowId).toBeNull();- expect(r.snapshot.windows[0].activeTabId).toBeNull();- expect(r.snapshot.tabs).toHaveLength(0);+ expect(r.nextActiveTabId).not.toBe(t.tabId);+ expect(r.snapshot.windows[0].activeTabId).toBe(r.nextActiveTabId);+ expect(r.snapshot.tabs).toEqual([+ expect.objectContaining({+ id: r.nextActiveTabId,+ href: DEFAULT_TAB_HREF,+ }),+ ]);});});describe("closeTabs", () => {@@ -450,11 +478,13 @@ describe("closeTabs", () => {expect(r.windows[0].activeTabId).toBe(ids[0]);});- it("lands the primary window on channels when all tabs close", () => {+ it("keeps a fresh tab in the primary window when all tabs close", () => {const { s, ids } = openMany(2);const r = closeTabs(s, ids);- expect(r.tabs).toHaveLength(0);- expect(r.windows[0].activeTabId).toBeNull();+ expect(r.tabs).toEqual([+ expect.objectContaining({ href: DEFAULT_TAB_HREF }),+ ]);+ expect(r.windows[0].activeTabId).toBe(r.tabs[0].id);});it("drops an emptied secondary window", () => {diff --git a/products/desktop/packages/shared/src/browser-tabs.ts b/products/desktop/packages/shared/src/browser-tabs.tsindex ae94a73e648f..75fd5dd2f9df 100644--- a/products/desktop/packages/shared/src/browser-tabs.ts+++ b/products/desktop/packages/shared/src/browser-tabs.ts@@ -6,10 +6,17 @@ import type {/** Spacing between adjacent tab positions, leaving room to insert without reindex. */export const POSITION_GAP = 1000;+export const DEFAULT_TAB_HREF = "/activity";type Clock = () => number;type IdFactory = () => string;+export type NewTabOptions = {+ href: string;+ makeId: IdFactory;+ now: Clock;+};+export type OpenTabResult = {snapshot: TabsSnapshot;tabId: string;@@ -17,7 +24,7 @@ export type OpenTabResult = {export type CloseTabResult = {snapshot: TabsSnapshot;- /** Tab focused after the close, or null for the channels landing. */+ /** Tab focused after the close, or null when no primary tab changed. */nextActiveTabId: string | null;/** Set when closing the last tab of a secondary window should close it. */closedWindowId: string | null;@@ -34,6 +41,28 @@ export function primaryWindow(snapshot: TabsSnapshot) {return snapshot.windows.find((w) => w.isPrimary) ?? snapshot.windows[0];}+export function ensureWindowHasTab(+ snapshot: TabsSnapshot,+ input: NewTabOptions & { windowId: string },+): TabsSnapshot {+ if (!snapshot.windows.some((window) => window.id === input.windowId)) {+ return snapshot;+ }+ if (snapshot.tabs.some((tab) => tab.windowId === input.windowId)) {+ return snapshot;+ }+ return openTab(snapshot, {+ windowId: input.windowId,+ href: input.href,+ viewState: null,+ dashboardId: null,+ taskId: null,+ channelId: null,+ makeId: input.makeId,+ now: input.now,+ }).snapshot;+}+function setActiveTab(snapshot: TabsSnapshot,windowId: string,@@ -248,12 +277,13 @@ export function setTabTarget(/*** Close a tab. Focus moves to the nearest sibling. Closing the last tab of a- * secondary window signals that the window should close; closing the last tab- * of the primary window leaves it on the channels landing (activeTabId null).+ * secondary window signals that the window should close. Closing the last tab+ * of the primary window opens a fresh tab so the main window never has none.*/export function closeTab(snapshot: TabsSnapshot,tabId: string,+ newTab: NewTabOptions,): CloseTabResult {const tab = snapshot.tabs.find((t) => t.id === tabId);if (!tab) {@@ -278,14 +308,24 @@ export function closeTab(closedWindowId: tab.windowId,};}- // Primary window → channels landing.+ const withoutClosedTab = setActiveTab(+ { ...snapshot, tabs: removedTabs },+ tab.windowId,+ null,+ );+ const replacement = openTab(withoutClosedTab, {+ windowId: tab.windowId,+ href: newTab.href,+ viewState: null,+ dashboardId: null,+ taskId: null,+ channelId: null,+ makeId: newTab.makeId,+ now: newTab.now,+ });return {- snapshot: setActiveTab(- { ...snapshot, tabs: removedTabs },- tab.windowId,- null,- ),- nextActiveTabId: null,+ snapshot: replacement.snapshot,+ nextActiveTabId: replacement.tabId,closedWindowId: null,};}@@ -304,8 +344,8 @@ export function closeTab(/*** Close several tabs at once — the bulk primitive behind "close other tabs" /* "close tabs to the right/left". Composes {@link closeTab} so the per-window- * succession rules (survivor focus, secondary-window drop, primary lands on- * channels) live in exactly one place.+ * succession rules (survivor focus, secondary-window drop, primary replacement)+ * live in exactly one place.** `focusTabId` is the bulk close's anchor (the right-clicked tab, which always* survives these operations). When a window's active tab is among those closed,@@ -316,6 +356,7 @@ export function closeTab(export function closeTabs(snapshot: TabsSnapshot,tabIds: string[],+ newTab: NewTabOptions,focusTabId?: string | null,): TabsSnapshot {const ids = new Set(tabIds);@@ -330,7 +371,7 @@ export function closeTabs(let next = snapshot;for (const id of ids) {- next = closeTab(next, id).snapshot;+ next = closeTab(next, id, newTab).snapshot;}if (focusTabId) {diff --git a/products/desktop/packages/shared/src/flags.ts b/products/desktop/packages/shared/src/flags.tsindex 7700c1c7f187..61be9bedd6bc 100644--- a/products/desktop/packages/shared/src/flags.ts+++ b/products/desktop/packages/shared/src/flags.ts@@ -18,13 +18,6 @@ export const PROJECT_BLUEBIRD_FLAG = "project-bluebird";* project-bluebird. The key predates the rename, matching the live flag.*/export const CHANNELS_LAYOUT_FLAG = "code-spaces-layout";-/**- * Gates the browser-tab strip inside the spaces layout. Off is the same code- * path with a single tab (the tab is the window), not a second implementation,- * so per-tab history and view state behave as their window-global predecessors.- * Requires the spaces layout.- */-export const SPACES_TABS_FLAG = "posthog-desktop-spaces-tabs";// Gates the Loops feature: the sidebar Loops space and the per-channel Loops tab.export const LOOPS_FLAG = "loops";export const DESKTOP_HOME_FLAG = "desktop-home-flag";diff --git a/products/desktop/packages/shared/src/index.ts b/products/desktop/packages/shared/src/index.tsindex d6bc8d6ec7ee..94c7318b3d8f 100644--- a/products/desktop/packages/shared/src/index.ts+++ b/products/desktop/packages/shared/src/index.ts@@ -52,7 +52,10 @@ export {type CloseTabResult,closeTab,closeTabs,+ DEFAULT_TAB_HREF,decideTabNavigation,+ ensureWindowHasTab,+ type NewTabOptions,type OpenTabResult,openTab,POSITION_GAP,diff --git a/products/desktop/packages/ui/src/features/browser-tabs/AGENTS.md b/products/desktop/packages/ui/src/features/browser-tabs/AGENTS.mdindex e71398e1e12a..b1916c10a8d4 100644--- a/products/desktop/packages/ui/src/features/browser-tabs/AGENTS.md+++ b/products/desktop/packages/ui/src/features/browser-tabs/AGENTS.md@@ -24,10 +24,9 @@ The identity fields (`dashboardId | taskId | channelId + channelSection |appView`) survive **only as a label and icon cache**, written alongside the href.Never compare them to decide where a tab is.-The strip is gated inside the spaces layout by `useSpacesTabs`-(`SPACES_TABS_FLAG`). Off is the same code path with a single tab, not a second-implementation: with one tab, the tab is the window, so per-tab history and view-state behave the way the window-global versions did.+The strip is always present in the app shell. `useSpacesTabs` tells shortcut and+navigation owners when the spaces layout owns browser-tab behavior. It is not a+rollout gate.## Components & styling@@ -112,8 +111,8 @@ differ. Desktop ships first.### Closing- Closing the active tab focuses its neighbour.-- Closing the last tab of a **secondary** window closes the window; closing the- last tab of the **primary** window lands on `/activity`, which opens a fresh tab.+- Closing the last tab of a **secondary** window closes the window. Closing the+ last tab of the **primary** window replaces it with a fresh `/activity` tab.### Keyboard- **⌘1-9 switches tabs**, the browser way: 1-8 pick that position, 9 picks the@@ -210,6 +209,7 @@ retarget its originating background tab as described below. `railHistoryStore`### Cross-window & persistence- Tabs, order, windows, and each tab's `href` + `viewState` persist to sqlite;the full session (all windows + their tabs + active tab) is restored on launch.+ Desktop and web both seed `/activity` when the primary window has no tabs.- `rewriteSavedLocation` (in `@posthog/shared`) runs over persisted hrefs onload, so a snapshot written before the routes were flattened does not restoretabs onto `/website/*` routes that no longer exist.diff --git a/products/desktop/packages/ui/src/features/browser-tabs/BrowserTabStrip.tsx b/products/desktop/packages/ui/src/features/browser-tabs/BrowserTabStrip.tsxindex 6863a0e98b6d..06b542f4ea18 100644--- a/products/desktop/packages/ui/src/features/browser-tabs/BrowserTabStrip.tsx+++ b/products/desktop/packages/ui/src/features/browser-tabs/BrowserTabStrip.tsx@@ -2,6 +2,7 @@ import { useService } from "@posthog/di/react";import {closeTab as closeTabLocal,closeTabs as closeTabsLocal,+ DEFAULT_TAB_HREF,decideTabNavigation,openTab as openTabLocal,primaryWindow,@@ -86,7 +87,6 @@ import { useOpenBrowserTab } from "./useOpenBrowserTab";*/const canvasInfo = new Map<string, { name: string; templateId: string }>();const taskInfo = new Map<string, string>();-const BLANK_TAB_HREF = "/activity";/** Bounded insert (most-recent kept) so the caches don't grow unbounded over a* long session. */@@ -752,7 +752,7 @@ export function BrowserTabStrip() {}}} else {- navigate({ to: BLANK_TAB_HREF, state });+ navigate({ to: DEFAULT_TAB_HREF, state });}},[channelReportsEnabled, navigate, router.history],@@ -788,9 +788,17 @@ export function BrowserTabStrip() {useDraftStore.getState().actions.setDraft(getTaskInputSessionId(tabId), null);- const next = applyLocalTransform((s) => closeTabLocal(s, tabId).snapshot);+ const newTabId = crypto.randomUUID();+ const next = applyLocalTransform(+ (s) =>+ closeTabLocal(s, tabId, {+ href: DEFAULT_TAB_HREF,+ makeId: () => newTabId,+ now: Date.now,+ }).snapshot,+ );applyCloseResult(next);- void persistWrite(() => client.close(tabId));+ void persistWrite(() => client.close(tabId, newTabId));};// Unpinning re-homes the tab at the front of the unpinned block. Apply the@@ -814,12 +822,22 @@ export function BrowserTabStrip() {for (const tabId of tabIds) {draftActions.setDraft(getTaskInputSessionId(tabId), null);}+ const newTabId = crypto.randomUUID();const next = applyLocalTransform((s) =>- closeTabsLocal(s, tabIds, anchorTabId),+ closeTabsLocal(+ s,+ tabIds,+ {+ href: DEFAULT_TAB_HREF,+ makeId: () => newTabId,+ now: Date.now,+ },+ anchorTabId,+ ),);applyCloseResult(next);void persistWrite(() =>- client.closeMany({ tabIds, focusTabId: anchorTabId }),+ client.closeMany({ tabIds, newTabId, focusTabId: anchorTabId }),);};@@ -856,10 +874,10 @@ export function BrowserTabStrip() {const landOnDefault = (tabId?: string): void => {const state = tabId ? (prev: object) => ({ ...prev, tabId }) : undefined;- navigate({ to: BLANK_TAB_HREF, state });+ navigate({ to: DEFAULT_TAB_HREF, state });};- const handleNewTab = (): void => openBrowserTab(BLANK_TAB_HREF);+ const handleNewTab = (): void => openBrowserTab(DEFAULT_TAB_HREF);// Cmd/Ctrl+T opens a new browser tab. Bound here (not globally) so it only// fires where the strip is mounted; the new-task shortcut owns Cmd/Ctrl+N.diff --git a/products/desktop/packages/ui/src/features/browser-tabs/browserTabsClient.ts b/products/desktop/packages/ui/src/features/browser-tabs/browserTabsClient.tsindex 5e60f572e5f8..372ba8fbc0e8 100644--- a/products/desktop/packages/ui/src/features/browser-tabs/browserTabsClient.ts+++ b/products/desktop/packages/ui/src/features/browser-tabs/browserTabsClient.ts@@ -29,9 +29,10 @@ export interface BrowserTabsClient {setTabTarget(input: TabLocationInput & { tabId: string; activate?: boolean },): Promise<TabsSnapshot>;- close(tabId: string): Promise<TabsSnapshot>;+ close(tabId: string, newTabId: string): Promise<TabsSnapshot>;closeMany(input: {tabIds: string[];+ newTabId: string;focusTabId?: string | null;}): Promise<TabsSnapshot>;setOrder(input: {diff --git a/products/desktop/packages/ui/src/features/browser-tabs/useSpacesTabs.ts b/products/desktop/packages/ui/src/features/browser-tabs/useSpacesTabs.tsnew file mode 100644index 000000000000..e4bb7da318b4--- /dev/null+++ b/products/desktop/packages/ui/src/features/browser-tabs/useSpacesTabs.ts@@ -0,0 +1,6 @@+import { useChannelsLayout } from "@posthog/ui/features/canvas/hooks/useChannelsLayout";++/** Whether the tab strip owns navigation inside the spaces layout. */+export function useSpacesTabs(): boolean {+ return useChannelsLayout();+}diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.test.tsxindex ca6689fb9b88..1df92ba6bc0a 100644--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.test.tsx+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.test.tsx@@ -12,7 +12,7 @@ const mocks = vi.hoisted(() => ({vi.mock("@posthog/ui/features/canvas/hooks/useChannelsLayout", () => ({useChannelsLayout: () => mocks.channelsLayout,}));-vi.mock("@posthog/ui/features/feature-flags/useSpacesTabs", () => ({+vi.mock("@posthog/ui/features/browser-tabs/useSpacesTabs", () => ({useSpacesTabs: () => mocks.spacesTabs,}));vi.mock("@posthog/ui/features/canvas/hooks/useStarredChannelSlots", () => ({diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.tsxindex 408679cdbd26..a287883b3684 100644--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.tsx+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelHotkeys.tsx@@ -1,4 +1,5 @@import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";+import { useSpacesTabs } from "@posthog/ui/features/browser-tabs/useSpacesTabs";import { useChannelsLayout } from "@posthog/ui/features/canvas/hooks/useChannelsLayout";import { useStarredChannelSlots } from "@posthog/ui/features/canvas/hooks/useStarredChannelSlots";import {@@ -8,7 +9,6 @@ import {import { useCurrentChannelStore } from "@posthog/ui/features/canvas/stores/currentChannelStore";import { requestSidebarSearchFocus } from "@posthog/ui/features/canvas/stores/sidebarSearchStore";import { SHORTCUTS } from "@posthog/ui/features/command/keyboard-shortcuts";-import { useSpacesTabs } from "@posthog/ui/features/feature-flags/useSpacesTabs";import { useSidebarStore } from "@posthog/ui/features/sidebar/sidebarStore";import { navigateToChannel } from "@posthog/ui/router/navigationBridge";import { track } from "@posthog/ui/shell/analytics";diff --git a/products/desktop/packages/ui/src/features/canvas/components/NavRail.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/NavRail.test.tsxindex 2bcd63655cdf..2f80ff15777a 100644--- a/products/desktop/packages/ui/src/features/canvas/components/NavRail.test.tsx+++ b/products/desktop/packages/ui/src/features/canvas/components/NavRail.test.tsx@@ -42,7 +42,7 @@ vi.mock(vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({useFeatureFlag: (key: string) => mocks.featureFlags.get(key) ?? false,}));-vi.mock("@posthog/ui/features/feature-flags/useSpacesTabs", () => ({+vi.mock("@posthog/ui/features/browser-tabs/useSpacesTabs", () => ({useSpacesTabs: () => true,}));vi.mock("@posthog/ui/features/canvas/hooks/useChannelsLayout", () => ({diff --git a/products/desktop/packages/ui/src/features/canvas/components/NavRail.tsx b/products/desktop/packages/ui/src/features/canvas/components/NavRail.tsxindex caa85d63d612..ff55ed94dbee 100644--- a/products/desktop/packages/ui/src/features/canvas/components/NavRail.tsx+++ b/products/desktop/packages/ui/src/features/canvas/components/NavRail.tsx@@ -13,6 +13,7 @@ import {import { DESKTOP_HOME_FLAG, LOOPS_FLAG } from "@posthog/shared";import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";import { useOpenBrowserTab } from "@posthog/ui/features/browser-tabs/useOpenBrowserTab";+import { useSpacesTabs } from "@posthog/ui/features/browser-tabs/useSpacesTabs";import { ActivityHoverCard } from "@posthog/ui/features/canvas/components/ActivityHoverCard";import {pickRailDestination,@@ -32,7 +33,6 @@ import { useChannelReportsEnabled } from "@posthog/ui/features/feature-flags/useimport { useContextLayerFlag } from "@posthog/ui/features/feature-flags/useContextLayerFlag";import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";import { useReportsInboxEnabled } from "@posthog/ui/features/feature-flags/useReportsInboxEnabled";-import { useSpacesTabs } from "@posthog/ui/features/feature-flags/useSpacesTabs";import { useInboxDecisionCount } from "@posthog/ui/features/inbox/hooks/useInboxDecisionCount";import { openSettings } from "@posthog/ui/features/settings/hooks/useOpenSettings";import { ProjectSwitcher } from "@posthog/ui/features/sidebar/components/ProjectSwitcher";diff --git a/products/desktop/packages/ui/src/features/feature-flags/useSpacesTabs.ts b/products/desktop/packages/ui/src/features/feature-flags/useSpacesTabs.tsdeleted file mode 100644index d0f39d6d2d0e..000000000000--- a/products/desktop/packages/ui/src/features/feature-flags/useSpacesTabs.ts+++ /dev/null@@ -1,17 +0,0 @@-import { SPACES_TABS_FLAG } from "@posthog/shared";-import { useChannelsLayout } from "@posthog/ui/features/canvas/hooks/useChannelsLayout";-import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";--/**- * Whether the tab strip is shown inside the spaces layout. Read this rather- * than the raw flag, so the spaces-layout requirement lives in one place.- *- * Only the strip is gated. The per-tab model underneath it is unconditional:- * with the strip hidden the window simply holds one tab, which behaves the way- * the window-global state did before it.- */-export function useSpacesTabs(): boolean {- const spacesLayout = useChannelsLayout();- const enabled = useFeatureFlag(SPACES_TABS_FLAG, import.meta.env.DEV);- return spacesLayout && enabled;-}diff --git a/products/desktop/packages/ui/src/router/routes/__root.tsx b/products/desktop/packages/ui/src/router/routes/__root.tsxindex 54dc1213484f..e306d0c5b551 100644--- a/products/desktop/packages/ui/src/router/routes/__root.tsx+++ b/products/desktop/packages/ui/src/router/routes/__root.tsx@@ -43,7 +43,6 @@ import { useNewTaskDeepLink } from "@posthog/ui/features/deep-links/useNewTaskDeimport { useOpenTargetDeepLink } from "@posthog/ui/features/deep-links/useOpenTargetDeepLink";import { useTaskDeepLink } from "@posthog/ui/features/deep-links/useTaskDeepLink";import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";-import { useSpacesTabs } from "@posthog/ui/features/feature-flags/useSpacesTabs";import { useInboxDeepLink } from "@posthog/ui/features/inbox/hooks/useInboxDeepLink";import { useIntegrations } from "@posthog/ui/features/integrations/useIntegrations";import { useLoopDeepLink } from "@posthog/ui/features/loops/hooks/useLoopDeepLink";@@ -207,10 +206,6 @@ function RootLayout() {// The new channels layout has exactly one gate: its feature flag (no// sidebar toggle). When on it subsumes the channels alpha entirely.const channelsLayout = useChannelsLayout();- // Tabs exist in the legacy layout already; the flag gates only bringing them- // into the spaces layout.- const spacesTabs = useSpacesTabs();- const showTabStrip = channelsLayout ? spacesTabs : true;const { hasSidebar } = useRailSurface();// When the sidebar is collapsed (Cmd+B) the title bar's left block shrinks to// fit its own controls so the tab strip flushes left with the content pane.@@ -416,7 +411,7 @@ function RootLayout() {moved to the rail to make room for it (see NavRail). The strip isalso the only global owner of Cmd+W, so the fallback has to holdthat key wherever the strip isn't mounted. */}- {showTabStrip ? <BrowserTabStrip /> : <TabShortcutFallback enabled />}+ <BrowserTabStrip />{/* Gated so an empty right-side group can't claim a no-drag rectin the title bar for nothing — every pixel without controlsshould drag the window. */}diff --git a/products/desktop/packages/ui/src/shell/GlobalEventHandlers.tsx b/products/desktop/packages/ui/src/shell/GlobalEventHandlers.tsxindex 5aa62e882f34..cb000fbd60ce 100644--- a/products/desktop/packages/ui/src/shell/GlobalEventHandlers.tsx+++ b/products/desktop/packages/ui/src/shell/GlobalEventHandlers.tsx@@ -8,6 +8,7 @@ import { useService, useServiceOptional } from "@posthog/di/react";import { useHostTRPC } from "@posthog/host-router/react";import { PROJECT_BLUEBIRD_FLAG } from "@posthog/shared";import type { Task } from "@posthog/shared/domain-types";+import { useSpacesTabs } from "@posthog/ui/features/browser-tabs/useSpacesTabs";import { useChannelsLayout } from "@posthog/ui/features/canvas/hooks/useChannelsLayout";import { toggleActivityPanel } from "@posthog/ui/features/canvas/toggleActivityPanel";import { getDefaultReviewMode } from "@posthog/ui/features/code-review/getDefaultReviewMode";@@ -15,7 +16,6 @@ import { useReviewNavigationStore } from "@posthog/ui/features/code-review/revieimport { SHORTCUTS } from "@posthog/ui/features/command/keyboard-shortcuts";import { useChannelReportsEnabled } from "@posthog/ui/features/feature-flags/useChannelReportsEnabled";import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";-import { useSpacesTabs } from "@posthog/ui/features/feature-flags/useSpacesTabs";import { useFolders } from "@posthog/ui/features/folders/useFolders";import { toggleRightPanel } from "@posthog/ui/features/navigation/rightPanelSide";import { usePanelLayoutStore } from "@posthog/ui/features/panels/panelLayoutStore";diff --git a/products/desktop/packages/workspace-server/src/services/browser-tabs/schemas.ts b/products/desktop/packages/workspace-server/src/services/browser-tabs/schemas.tsindex 0961d3e515d2..89bd8130634c 100644--- a/products/desktop/packages/workspace-server/src/services/browser-tabs/schemas.ts+++ b/products/desktop/packages/workspace-server/src/services/browser-tabs/schemas.ts@@ -5,13 +5,6 @@ import {} from "@posthog/shared";import { z } from "zod";-/**- * Where `+` lands, and where an empty strip reseeds. The space index is the- * app's "pick something" screen, so a new tab shows the spaces rather than an- * empty placeholder.- */-export const NEW_TAB_HREF = "/spaces";-/** tRPC output: the full durable tab/window snapshot. */export const browserTabsSnapshotOutput = tabsSnapshotSchema;@@ -41,10 +34,14 @@ export const setTabTargetInput = z.object({activate: z.boolean().optional(),});-export const closeTabInput = z.object({ tabId: z.string() });+export const closeTabInput = z.object({+ tabId: z.string(),+ newTabId: z.string(),+});export const closeTabsInput = z.object({tabIds: z.array(z.string()),+ newTabId: z.string(),// The bulk close's anchor (the right-clicked tab, which always survives);// focus falls to it when the active tab is among those closed.focusTabId: z.string().nullable().default(null),diff --git a/products/desktop/packages/workspace-server/src/services/browser-tabs/service.test.ts b/products/desktop/packages/workspace-server/src/services/browser-tabs/service.test.tsindex e5aa68f18b2b..67c09f8405e9 100644--- a/products/desktop/packages/workspace-server/src/services/browser-tabs/service.test.ts+++ b/products/desktop/packages/workspace-server/src/services/browser-tabs/service.test.ts@@ -1,7 +1,10 @@-import type { BrowserTab, TabsSnapshot } from "@posthog/shared";+import {+ type BrowserTab,+ DEFAULT_TAB_HREF,+ type TabsSnapshot,+} from "@posthog/shared";import { describe, expect, it } from "vitest";import type { IBrowserTabsRepository } from "../../db/repositories/browser-tabs-repository";-import { NEW_TAB_HREF } from "./schemas";import { BrowserTabsService } from "./service";const blankTab = (overrides: Partial<BrowserTab> = {}): BrowserTab => ({@@ -58,12 +61,28 @@ describe("BrowserTabsService boot invariants", () => {expect(snapshot.tabs.length).toBeGreaterThanOrEqual(1);expect(snapshot.tabs[0]?.windowId).toBe(primary?.id);// On the new-tab page, not a location-less tab that would render nothing.- expect(snapshot.tabs[0]?.href).toBe(NEW_TAB_HREF);+ expect(snapshot.tabs[0]?.href).toBe(DEFAULT_TAB_HREF);// The healed snapshot is persisted so the invariant survives a restart.expect(repo.saved).toEqual(snapshot);},);+ it("keeps one tab after the last primary tab closes", () => {+ const repo = new FakeRepository({ windows: [], tabs: [] });+ const service = new BrowserTabsService(repo);+ const initialTabId = service.getSnapshot().tabs[0].id;++ const snapshot = service.close(initialTabId, "replacement-tab");++ expect(snapshot.tabs).toEqual([+ expect.objectContaining({+ id: "replacement-tab",+ href: DEFAULT_TAB_HREF,+ }),+ ]);+ expect(snapshot.windows[0].activeTabId).toBe("replacement-tab");+ });+it("leaves an already-populated snapshot untouched", () => {const initial: TabsSnapshot = {windows: [@@ -134,7 +153,7 @@ describe("BrowserTabsService boot invariants", () => {expect(reset.tabs).toEqual([expect.objectContaining({windowId: "win-1",- href: NEW_TAB_HREF,+ href: DEFAULT_TAB_HREF,viewState: null,taskId: null,appView: null,diff --git a/products/desktop/packages/workspace-server/src/services/browser-tabs/service.ts b/products/desktop/packages/workspace-server/src/services/browser-tabs/service.tsindex eeb04cbe2f31..80c850312634 100644--- a/products/desktop/packages/workspace-server/src/services/browser-tabs/service.ts+++ b/products/desktop/packages/workspace-server/src/services/browser-tabs/service.ts@@ -2,6 +2,8 @@ import {type BrowserWindow,closeTab,closeTabs,+ DEFAULT_TAB_HREF,+ ensureWindowHasTab,openTab,resetTabs,setTabOrder,@@ -15,11 +17,7 @@ import {import { inject, injectable } from "inversify";import { BROWSER_TABS_REPOSITORY } from "../../db/identifiers";import type { IBrowserTabsRepository } from "../../db/repositories/browser-tabs-repository";-import {- BrowserTabsEvent,- type BrowserTabsEvents,- NEW_TAB_HREF,-} from "./schemas";+import { BrowserTabsEvent, type BrowserTabsEvents } from "./schemas";const makeId = () => crypto.randomUUID();const now = () => Date.now();@@ -48,8 +46,12 @@ export interface IBrowserTabsService {activate?: boolean;},): TabsSnapshot;- close(tabId: string): TabsSnapshot;- closeMany(tabIds: string[], focusTabId?: string | null): TabsSnapshot;+ close(tabId: string, newTabId: string): TabsSnapshot;+ closeMany(+ tabIds: string[],+ newTabId: string,+ focusTabId?: string | null,+ ): TabsSnapshot;setOrder(input: { windowId: string; tabIds: string[] }): TabsSnapshot;setActiveTab(input: { windowId: string; tabId: string | null }): TabsSnapshot;snapshotChangeEvents(@@ -95,25 +97,16 @@ export class BrowserTabsServicereturn { ...snapshot, windows: [primary, ...snapshot.windows] };}- /**- * The strip must never boot empty: seed a tab on the space index when none- * survived. That page is also what `+` opens, so an empty strip and a fresh- * tab land on the same screen.- */+ /** Keep a primary tab available when persisted state is empty or damaged. */private ensureAtLeastOneTab(snapshot: TabsSnapshot): TabsSnapshot {- if (snapshot.tabs.length > 0) return snapshot;- const primary = snapshot.windows.find((w) => w.isPrimary);+ const primary = snapshot.windows.find((window) => window.isPrimary);if (!primary) return snapshot;- return openTab(snapshot, {+ return ensureWindowHasTab(snapshot, {windowId: primary.id,- href: NEW_TAB_HREF,- viewState: null,- dashboardId: null,- taskId: null,- channelId: null,+ href: DEFAULT_TAB_HREF,makeId,now,- }).snapshot;+ });}/** Creation targets heal a stale window id (a mirror seeded before a schema@@ -142,7 +135,7 @@ export class BrowserTabsServicereset(): TabsSnapshot {return this.commit(- resetTabs(this.snapshot, { href: NEW_TAB_HREF, makeId, now }),+ resetTabs(this.snapshot, { href: DEFAULT_TAB_HREF, makeId, now }),);}@@ -184,13 +177,28 @@ export class BrowserTabsServicereturn this.commit(setTabTarget(this.snapshot, { ...input, now }));}- close(tabId: string): TabsSnapshot {- const { snapshot } = closeTab(this.snapshot, tabId);+ close(tabId: string, newTabId: string): TabsSnapshot {+ const { snapshot } = closeTab(this.snapshot, tabId, {+ href: DEFAULT_TAB_HREF,+ makeId: () => newTabId,+ now,+ });return this.commit(snapshot);}- closeMany(tabIds: string[], focusTabId?: string | null): TabsSnapshot {- return this.commit(closeTabs(this.snapshot, tabIds, focusTabId));+ closeMany(+ tabIds: string[],+ newTabId: string,+ focusTabId?: string | null,+ ): TabsSnapshot {+ return this.commit(+ closeTabs(+ this.snapshot,+ tabIds,+ { href: DEFAULT_TAB_HREF, makeId: () => newTabId, now },+ focusTabId,+ ),+ );}setOrder(input: { windowId: string; tabIds: string[] }): TabsSnapshot {