Skip to content

Commit 6b2e210

Browse files
committed
fix(web): isolate browser views and shortcuts across panes
1 parent fd226af commit 6b2e210

6 files changed

Lines changed: 113 additions & 10 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ import { PullRequestDetailPanel } from "./pullRequest/PullRequestDetailPanel";
239239
import { PullRequestDetailGhost } from "./pullRequest/PullRequestGhosts";
240240
import { PullRequestsUnavailableState } from "./pullRequest/PullRequestsUnavailableState";
241241
import { RightPanelTabs } from "./RightPanelTabs";
242-
import type { WorkspaceTabContextTarget } from "./RightPanelTabs.logic";
242+
import { canCopyWorkspaceTabToSplit, type WorkspaceTabContextTarget } from "./RightPanelTabs.logic";
243243
import { SplitPaneGrid, type PaneFocusPulse, type PaneTabBarDropPreview } from "./SplitPaneGrid";
244244
import { AgentsPanel } from "./AgentsPanel";
245245
import { LinkPullRequestDialogHost } from "./pullRequest/LinkPullRequestDialog";
@@ -5093,9 +5093,32 @@ export default function ChatView(props: ChatViewProps) {
50935093
[activeThreadRef, activateRightPanelSurface],
50945094
);
50955095

5096+
const canCopyWorkspaceTab = useCallback(
5097+
(tabId: PaneTabId) => {
5098+
if (!activeThreadRef) return false;
5099+
const layout = selectThreadWorkspaceLayout(
5100+
useThreadWorkspaceLayoutStore.getState().byThreadKey,
5101+
activeThreadRef,
5102+
);
5103+
const tab = layout.tabsById[tabId];
5104+
if (tab?._tag !== "Surface") return false;
5105+
const surface = selectThreadRightPanelState(
5106+
useRightPanelStore.getState().byThreadKey,
5107+
activeThreadRef,
5108+
).surfaces.find((candidate) => candidate.id === tab.surfaceId);
5109+
return surface !== undefined && canCopyWorkspaceTabToSplit({ _tag: "Surface", surface });
5110+
},
5111+
[activeThreadRef],
5112+
);
50965113
const mutateWorkspaceLayout = useCallback(
50975114
(transition: ThreadWorkspaceLayoutTransition) => {
50985115
if (!activeThreadRef) return;
5116+
if (
5117+
transition._tag === "SplitTab" &&
5118+
transition.mode === "copy" &&
5119+
!canCopyWorkspaceTab(transition.tabId)
5120+
)
5121+
return;
50995122

51005123
const current = selectThreadWorkspaceLayout(
51015124
useThreadWorkspaceLayoutStore.getState().byThreadKey,
@@ -5122,6 +5145,7 @@ export default function ChatView(props: ChatViewProps) {
51225145
},
51235146
[
51245147
activeThreadRef,
5148+
canCopyWorkspaceTab,
51255149
closeAfterSurfaceConfirmation,
51265150
finishRightPanelSurfaceClose,
51275151
syncFocusedWorkspaceSurface,
@@ -9671,7 +9695,7 @@ export default function ChatView(props: ChatViewProps) {
96719695
}}
96729696
tabDropPreview={tabDropPreview}
96739697
adjacentGroups={adjacentPanes}
9674-
canCopyTabToSplit={(target) => target._tag === "Surface"}
9698+
canCopyTabToSplit={canCopyWorkspaceTabToSplit}
96759699
canMoveTabToSplit={() => pane.tabIds.length > 1}
96769700
onCopyFilePath={copyRightPanelFilePath}
96779701
onRenameDevice={(surfaceId, title) =>
@@ -9742,7 +9766,7 @@ export default function ChatView(props: ChatViewProps) {
97429766
<SplitPaneGrid
97439767
tree={visibleWorkspacePaneTree}
97449768
canCopyDraggedTabFromSolePane={(draggedTab) =>
9745-
threadWorkspaceLayout.tabsById[draggedTab.sourceTabId]?._tag === "Surface"
9769+
canCopyWorkspaceTab(draggedTab.sourceTabId)
97469770
}
97479771
focusPulse={workspacePaneFocusPulse}
97489772
renderPane={renderWorkspacePane}

‎apps/web/src/components/RightPanelTabs.logic.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,74 @@ import { describe, expect, test } from "vite-plus/test";
22

33
import {
44
buildWorkspaceTabContextMenuItems,
5+
canCopyWorkspaceTabToSplit,
56
resolveWorkspaceTabLayoutAction,
67
resolveWorkspaceTabSplitAction,
78
} from "./RightPanelTabs.logic";
9+
import { canDropPaneTab } from "./SplitPaneGrid.logic";
10+
import { createPaneTree, splitPane } from "~/splitPaneTree";
811

912
const NO_ADJACENT_PANES = { up: null, down: null, left: null, right: null } as const;
1013

1114
describe("workspace tab context menus", () => {
15+
test("browser menus allow moving without offering a second presentation of the same resource", () => {
16+
const items = buildWorkspaceTabContextMenuItems({
17+
target: {
18+
_tag: "Surface",
19+
surface: { id: "browser:tab-1", kind: "preview", resourceId: "tab-1" },
20+
},
21+
surfaceCount: 2,
22+
surfaceIndex: 0,
23+
adjacentGroups: NO_ADJACENT_PANES,
24+
moveToGroupAvailable: true,
25+
copyToSplitAvailable: true,
26+
moveToSplitAvailable: true,
27+
});
28+
expect(items).toContainEqual({ id: "split-right", label: "Split Right", disabled: true });
29+
expect(items).toContainEqual({ id: "split-down", label: "Split Down", disabled: true });
30+
expect(items.at(-1)).toMatchObject({
31+
id: "split-and-move",
32+
children: expect.arrayContaining([
33+
{ id: "move-right", label: "Split Right", disabled: false },
34+
]),
35+
});
36+
});
37+
38+
test("a sole browser tab can move to another pane but cannot copy itself through an edge drop", () => {
39+
const tree = splitPane(
40+
createPaneTree({ paneId: "pane:source", tabIds: ["pane-tab:browser"] }),
41+
{
42+
sourcePaneId: "pane:source",
43+
targetPaneId: "pane:target",
44+
splitId: "pane-split:1",
45+
direction: "right",
46+
},
47+
);
48+
const draggedTab = { sourcePaneId: "pane:source", sourceTabId: "pane-tab:browser" } as const;
49+
const canCopyFromSolePane = canCopyWorkspaceTabToSplit({
50+
_tag: "Surface",
51+
surface: { id: "browser:tab-1", kind: "preview", resourceId: "tab-1" },
52+
});
53+
expect(
54+
canDropPaneTab({
55+
tree,
56+
draggedTab,
57+
targetPaneId: "pane:source",
58+
zone: "right",
59+
canCopyFromSolePane,
60+
}),
61+
).toBe(false);
62+
expect(
63+
canDropPaneTab({
64+
tree,
65+
draggedTab,
66+
targetPaneId: "pane:target",
67+
zone: "right",
68+
canCopyFromSolePane,
69+
}),
70+
).toBe(true);
71+
});
72+
1273
test("offers copy and move split actions for a surface tab", () => {
1374
const items = buildWorkspaceTabContextMenuItems({
1475
target: {

‎apps/web/src/components/RightPanelTabs.logic.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export type WorkspaceTabContextTarget =
77
| { readonly _tag: "Thread" }
88
| { readonly _tag: "Surface"; readonly surface: RightPanelSurface };
99

10+
export function canCopyWorkspaceTabToSplit(target: WorkspaceTabContextTarget): boolean {
11+
// A browser resource has one presentation owner, even when several panes are visible.
12+
return target._tag === "Surface" && target.surface.kind !== "preview";
13+
}
14+
1015
export type TabContextMenuAction =
1116
| "copy-path"
1217
| "close"
@@ -41,6 +46,8 @@ export function buildWorkspaceTabContextMenuItems(input: {
4146
readonly moveToSplitAvailable: boolean;
4247
}): readonly ContextMenuItem<TabContextMenuAction>[] {
4348
const items: ContextMenuItem<TabContextMenuAction>[] = [];
49+
const copyToSplitAvailable =
50+
input.copyToSplitAvailable && canCopyWorkspaceTabToSplit(input.target);
4451

4552
if (input.target._tag === "Surface") {
4653
if (input.target.surface.kind === "file") {
@@ -70,10 +77,10 @@ export function buildWorkspaceTabContextMenuItems(input: {
7077
? directionalGroupItems(input.adjacentGroups)
7178
: [];
7279

73-
if (input.copyToSplitAvailable || input.moveToSplitAvailable || moveGroupItems.length > 0) {
80+
if (copyToSplitAvailable || input.moveToSplitAvailable || moveGroupItems.length > 0) {
7481
items.push(
75-
{ id: "split-right", label: "Split Right", disabled: !input.copyToSplitAvailable },
76-
{ id: "split-down", label: "Split Down", disabled: !input.copyToSplitAvailable },
82+
{ id: "split-right", label: "Split Right", disabled: !copyToSplitAvailable },
83+
{ id: "split-down", label: "Split Down", disabled: !copyToSplitAvailable },
7784
{
7885
id: "split-and-move",
7986
label: "Split & Move",

‎apps/web/src/components/preview/PreviewView.tsx‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type ScopedThreadRef,
1414
} from "@t3tools/contracts";
1515
import { normalizePreviewUrl } from "@t3tools/shared/preview";
16-
import { useCallback, useEffect, useRef, useState } from "react";
16+
import { useCallback, useEffect, useId, useRef, useState } from "react";
1717

1818
import {
1919
BROWSER_HISTORY_MAX_ENTRIES_PER_PROJECT,
@@ -25,6 +25,7 @@ import {
2525
import { type ComposerImageAttachment, useComposerDraftStore } from "~/composerDraftStore";
2626
import { capturePreviewAnnotationScreenshot } from "~/lib/previewAnnotation";
2727
import { ensureLocalApi } from "~/localApi";
28+
import { focusedPreviewActionTarget } from "~/lib/previewFocus";
2829
import {
2930
rememberPreviewUrl,
3031
updatePreviewServerSnapshot,
@@ -104,6 +105,7 @@ export function PreviewView({
104105
visible,
105106
onSendAnnotation,
106107
}: Props) {
108+
const emptyPreviewActionTarget = useId();
107109
const [focusUrlNonce, setFocusUrlNonce] = useState<number | undefined>(undefined);
108110
const [pickActive, setPickActive] = useState(false);
109111
const activeRecordingTabIds = useActiveBrowserRecordingTabIds();
@@ -678,11 +680,11 @@ export function PreviewView({
678680
};
679681
}, [runtimeTabId]);
680682

681-
// Subscribe only while visible; `toggle-panel` is owned by ChatView's
682-
// URL-aware handler regardless of whether the panel is currently mounted.
683+
const previewActionTarget = runtimeTabId ?? emptyPreviewActionTarget;
683684
useEffect(() => {
684685
if (!visible) return;
685686
return subscribePreviewAction((action) => {
687+
if (focusedPreviewActionTarget() !== previewActionTarget) return;
686688
switch (action) {
687689
case "refresh":
688690
handleRefresh();
@@ -703,11 +705,12 @@ export function PreviewView({
703705
return;
704706
}
705707
});
706-
}, [handleRefresh, handleResetZoom, handleZoomIn, handleZoomOut, visible]);
708+
}, [handleRefresh, handleResetZoom, handleZoomIn, handleZoomOut, previewActionTarget, visible]);
707709

708710
return (
709711
<div
710712
className="flex min-h-0 flex-1 flex-col bg-background"
713+
data-preview-tab={previewActionTarget}
711714
data-thread-key={scopedThreadKey(threadRef)}
712715
>
713716
<PreviewChromeRow

‎apps/web/src/lib/previewFocus.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ export function isPreviewFocused(): boolean {
1313
if (activeElement.tagName.toLowerCase() === "webview") return true;
1414
return activeElement.closest("[data-preview-panel-mode]") !== null;
1515
}
16+
17+
export function focusedPreviewActionTarget(): string | null {
18+
const activeElement = document.activeElement;
19+
if (!(activeElement instanceof HTMLElement) || !activeElement.isConnected) return null;
20+
// Hosted webviews live outside the pane DOM; their runtime tab id matches the preview chrome.
21+
return activeElement.closest("[data-preview-tab]")?.getAttribute("data-preview-tab") ?? null;
22+
}

‎docs/user/thread-workspace.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Use **Split editor right** in the workspace toolbar to add an empty pane, then u
1111
choose what opens there. A tab's context menu can copy it into a split, move it into a new split, or
1212
move it to an adjacent pane. Drag tabs within a row to reorder them, onto another tab row to move
1313
them, onto a pane edge to create a split, or onto a pane center to swap pane layouts.
14+
Browser tabs can move between panes; open another browser tab to show a second browser alongside one.
1415

1516
The **+** menu can open multiple independent Diff, Files, Agents, or Linked pull requests tabs. Tabs
1617
backed by resources, such as browser sessions and terminals, remain independent per resource.

0 commit comments

Comments
 (0)