Skip to content

Commit 6ec3dd0

Browse files
Add right-panel bulk close and tab context actions
- Support closing other tabs, tabs to the right, and all tabs - Add tab context menu actions for file path copy and bulk closing - Keep preview state in sync when surfaces are removed
1 parent 689a882 commit 6ec3dd0

4 files changed

Lines changed: 266 additions & 22 deletions

File tree

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

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,26 +3098,19 @@ function ChatViewContent(props: ChatViewProps) {
30983098
threadKey === routeThreadKey ? null : routeThreadKey,
30993099
);
31003100
};
3101-
const closeRightPanelSurface = useCallback(
3102-
(surface: RightPanelSurface) => {
3101+
const cleanupRightPanelSurfaces = useCallback(
3102+
(surfaces: readonly RightPanelSurface[]) => {
31033103
if (!activeThreadRef) return;
3104-
if (surface.kind === "preview" && surface.resourceId) {
3105-
usePreviewStateStore.getState().removeSession(activeThreadRef, surface.resourceId);
3106-
const api = readEnvironmentApi(activeThreadRef.environmentId);
3107-
void api?.preview
3108-
.close({ threadId: activeThreadRef.threadId, tabId: surface.resourceId })
3109-
.catch(() => undefined);
3110-
}
3111-
useRightPanelStore.getState().closeSurface(activeThreadRef, surface.id);
3112-
const nextActiveSurface = selectActiveRightPanelSurface(
3113-
useRightPanelStore.getState().byThreadKey,
3114-
activeThreadRef,
3115-
);
3116-
if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
3117-
usePreviewStateStore.getState().setActiveTab(activeThreadRef, nextActiveSurface.resourceId);
3118-
}
3119-
if (surface.kind === "terminal") {
3120-
const api = readEnvironmentApi(activeThreadRef.environmentId);
3104+
3105+
const api = readEnvironmentApi(activeThreadRef.environmentId);
3106+
for (const surface of surfaces) {
3107+
if (surface.kind === "preview" && surface.resourceId) {
3108+
usePreviewStateStore.getState().removeSession(activeThreadRef, surface.resourceId);
3109+
void api?.preview
3110+
.close({ threadId: activeThreadRef.threadId, tabId: surface.resourceId })
3111+
.catch(() => undefined);
3112+
}
3113+
if (surface.kind !== "terminal") continue;
31213114
for (const terminalId of surface.terminalIds) {
31223115
void api?.terminal
31233116
.close({
@@ -3128,7 +3121,8 @@ function ChatViewContent(props: ChatViewProps) {
31283121
.catch(() => undefined);
31293122
}
31303123
}
3131-
if (surface.kind === "diff" && diffOpen) {
3124+
3125+
if (diffOpen && surfaces.some((surface) => surface.kind === "diff")) {
31323126
void navigate({
31333127
to: "/$environmentId/$threadId",
31343128
params: { environmentId, threadId },
@@ -3139,6 +3133,93 @@ function ChatViewContent(props: ChatViewProps) {
31393133
},
31403134
[activeThreadRef, diffOpen, environmentId, navigate, threadId],
31413135
);
3136+
const syncActivePreviewSurface = useCallback(() => {
3137+
if (!activeThreadRef) return;
3138+
const nextActiveSurface = selectActiveRightPanelSurface(
3139+
useRightPanelStore.getState().byThreadKey,
3140+
activeThreadRef,
3141+
);
3142+
if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
3143+
usePreviewStateStore.getState().setActiveTab(activeThreadRef, nextActiveSurface.resourceId);
3144+
}
3145+
}, [activeThreadRef]);
3146+
const closeRightPanelSurface = useCallback(
3147+
(surface: RightPanelSurface) => {
3148+
if (!activeThreadRef) return;
3149+
cleanupRightPanelSurfaces([surface]);
3150+
useRightPanelStore.getState().closeSurface(activeThreadRef, surface.id);
3151+
syncActivePreviewSurface();
3152+
},
3153+
[activeThreadRef, cleanupRightPanelSurfaces, syncActivePreviewSurface],
3154+
);
3155+
const closeOtherRightPanelSurfaces = useCallback(
3156+
(surface: RightPanelSurface) => {
3157+
if (!activeThreadRef) return;
3158+
const surfaces = rightPanelState.surfaces.filter((entry) => entry.id !== surface.id);
3159+
cleanupRightPanelSurfaces(surfaces);
3160+
useRightPanelStore.getState().closeOtherSurfaces(activeThreadRef, surface.id);
3161+
syncActivePreviewSurface();
3162+
},
3163+
[
3164+
activeThreadRef,
3165+
cleanupRightPanelSurfaces,
3166+
rightPanelState.surfaces,
3167+
syncActivePreviewSurface,
3168+
],
3169+
);
3170+
const closeRightPanelSurfacesToRight = useCallback(
3171+
(surface: RightPanelSurface) => {
3172+
if (!activeThreadRef) return;
3173+
const surfaceIndex = rightPanelState.surfaces.findIndex((entry) => entry.id === surface.id);
3174+
if (surfaceIndex < 0) return;
3175+
const surfaces = rightPanelState.surfaces.slice(surfaceIndex + 1);
3176+
cleanupRightPanelSurfaces(surfaces);
3177+
useRightPanelStore.getState().closeSurfacesToRight(activeThreadRef, surface.id);
3178+
syncActivePreviewSurface();
3179+
},
3180+
[
3181+
activeThreadRef,
3182+
cleanupRightPanelSurfaces,
3183+
rightPanelState.surfaces,
3184+
syncActivePreviewSurface,
3185+
],
3186+
);
3187+
const closeAllRightPanelSurfaces = useCallback(() => {
3188+
if (!activeThreadRef) return;
3189+
cleanupRightPanelSurfaces(rightPanelState.surfaces);
3190+
useRightPanelStore.getState().closeAllSurfaces(activeThreadRef);
3191+
}, [activeThreadRef, cleanupRightPanelSurfaces, rightPanelState.surfaces]);
3192+
const copyRightPanelFilePath = useCallback((relativePath: string) => {
3193+
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
3194+
toastManager.add(
3195+
stackedThreadToast({
3196+
type: "error",
3197+
title: "Failed to copy path",
3198+
description: "Clipboard API unavailable.",
3199+
}),
3200+
);
3201+
return;
3202+
}
3203+
3204+
void navigator.clipboard.writeText(relativePath).then(
3205+
() => {
3206+
toastManager.add({
3207+
type: "success",
3208+
title: "Path copied",
3209+
description: relativePath,
3210+
});
3211+
},
3212+
(error) => {
3213+
toastManager.add(
3214+
stackedThreadToast({
3215+
type: "error",
3216+
title: "Failed to copy path",
3217+
description: error instanceof Error ? error.message : "An error occurred.",
3218+
}),
3219+
);
3220+
},
3221+
);
3222+
}, []);
31423223
const persistThreadSettingsForNextTurn = useCallback(
31433224
async (input: {
31443225
threadId: ThreadId;
@@ -4836,6 +4917,10 @@ function ChatViewContent(props: ChatViewProps) {
48364917
terminalLabelsById={activeTerminalLabelsById}
48374918
onActivate={activateRightPanelSurface}
48384919
onCloseSurface={closeRightPanelSurface}
4920+
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
4921+
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
4922+
onCloseAllSurfaces={closeAllRightPanelSurfaces}
4923+
onCopyFilePath={copyRightPanelFilePath}
48394924
onAddBrowser={createBrowserSurface}
48404925
onAddTerminal={addTerminalSurface}
48414926
onAddDiff={addDiffSurface}
@@ -4910,6 +4995,10 @@ function ChatViewContent(props: ChatViewProps) {
49104995
terminalLabelsById={activeTerminalLabelsById}
49114996
onActivate={activateRightPanelSurface}
49124997
onCloseSurface={closeRightPanelSurface}
4998+
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
4999+
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
5000+
onCloseAllSurfaces={closeAllRightPanelSurfaces}
5001+
onCopyFilePath={copyRightPanelFilePath}
49135002
onAddBrowser={createBrowserSurface}
49145003
onAddTerminal={addTerminalSurface}
49155004
onAddDiff={addDiffSurface}

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

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import type { PreviewSessionSnapshot } from "@t3tools/contracts";
1+
import type { ContextMenuItem, PreviewSessionSnapshot } from "@t3tools/contracts";
22
import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
33
import { ClipboardList, FileDiff, Files, Globe2, Plus, TerminalSquare, X } from "lucide-react";
4-
import { type ReactElement, type ReactNode, useEffect, useRef, useState } from "react";
4+
import {
5+
type MouseEvent as ReactMouseEvent,
6+
type ReactElement,
7+
type ReactNode,
8+
useCallback,
9+
useEffect,
10+
useRef,
11+
useState,
12+
} from "react";
513

614
import { isElectron } from "~/env";
715
import type { RightPanelSurface } from "~/rightPanelStore";
816
import { cn } from "~/lib/utils";
17+
import { readLocalApi } from "~/localApi";
918
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
1019
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
1120
import { ScrollArea } from "~/components/ui/scroll-area";
@@ -25,6 +34,10 @@ interface RightPanelTabsProps {
2534
terminalLabelsById: ReadonlyMap<string, string>;
2635
onActivate: (surface: RightPanelSurface) => void;
2736
onCloseSurface: (surface: RightPanelSurface) => void;
37+
onCloseOtherSurfaces: (surface: RightPanelSurface) => void;
38+
onCloseSurfacesToRight: (surface: RightPanelSurface) => void;
39+
onCloseAllSurfaces: () => void;
40+
onCopyFilePath: (relativePath: string) => void;
2841
onAddBrowser: () => void;
2942
onAddTerminal: () => void;
3043
onAddDiff: () => void;
@@ -41,6 +54,8 @@ const SURFACE_DISABLED_REASONS = {
4154
diff: "Diff is only available for server threads in Git repositories.",
4255
} as const;
4356

57+
type TabContextMenuAction = "copy-path" | "close" | "close-others" | "close-to-right" | "close-all";
58+
4459
function DisabledReasonTooltip(props: { reason: string; trigger: ReactElement }) {
4560
return (
4661
<Tooltip>
@@ -257,6 +272,64 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
257272
const { resolvedTheme } = useTheme();
258273
const tabListRef = useRef<HTMLDivElement>(null);
259274

275+
const handleTabContextMenu = useCallback(
276+
async (event: ReactMouseEvent, surface: RightPanelSurface) => {
277+
event.preventDefault();
278+
event.stopPropagation();
279+
280+
const api = readLocalApi();
281+
if (!api) return;
282+
283+
const surfaceIndex = props.surfaces.findIndex((entry) => entry.id === surface.id);
284+
if (surfaceIndex < 0) return;
285+
286+
const items: ContextMenuItem<TabContextMenuAction>[] = [];
287+
if (surface.kind === "file") {
288+
items.push({ id: "copy-path", label: "Copy path" });
289+
}
290+
items.push(
291+
{ id: "close", label: "Close" },
292+
{
293+
id: "close-others",
294+
label: "Close others",
295+
disabled: props.surfaces.length <= 1,
296+
},
297+
{
298+
id: "close-to-right",
299+
label: "Close to the right",
300+
disabled: surfaceIndex >= props.surfaces.length - 1,
301+
},
302+
{
303+
id: "close-all",
304+
label: "Close all",
305+
disabled: props.surfaces.length === 0,
306+
},
307+
);
308+
309+
const action = await api.contextMenu.show(items, { x: event.clientX, y: event.clientY });
310+
switch (action) {
311+
case "copy-path":
312+
if (surface.kind === "file") props.onCopyFilePath(surface.relativePath);
313+
break;
314+
case "close":
315+
props.onCloseSurface(surface);
316+
break;
317+
case "close-others":
318+
props.onCloseOtherSurfaces(surface);
319+
break;
320+
case "close-to-right":
321+
props.onCloseSurfacesToRight(surface);
322+
break;
323+
case "close-all":
324+
props.onCloseAllSurfaces();
325+
break;
326+
case null:
327+
break;
328+
}
329+
},
330+
[props],
331+
);
332+
260333
useEffect(() => {
261334
const activeTab = tabListRef.current?.querySelector<HTMLElement>("[data-active-tab='true']");
262335
activeTab?.scrollIntoView({ block: "nearest", inline: "nearest" });
@@ -291,6 +364,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
291364
<div
292365
key={surface.id}
293366
data-active-tab={active}
367+
onContextMenu={(event) => void handleTabContextMenu(event, surface)}
294368
className={cn(
295369
"group flex h-7 min-w-25 max-w-44 shrink-0 items-center gap-1.5 rounded-md px-2 text-sm",
296370
active

‎apps/web/src/rightPanelStore.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,47 @@ describe("rightPanelStore", () => {
304304
});
305305
});
306306

307+
it("closing other surfaces keeps the selected surface active", () => {
308+
useRightPanelStore.getState().openBrowser(refA, "tab-a");
309+
useRightPanelStore.getState().openFile(refA, "src/index.ts");
310+
useRightPanelStore.getState().openTerminal(refA, "term-1");
311+
312+
useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
313+
314+
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
315+
isOpen: true,
316+
activeSurfaceId: "file:src/index.ts",
317+
surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
318+
});
319+
});
320+
321+
it("closing surfaces to the right activates the selected surface when active was removed", () => {
322+
useRightPanelStore.getState().openBrowser(refA, "tab-a");
323+
useRightPanelStore.getState().openFile(refA, "src/index.ts");
324+
useRightPanelStore.getState().openTerminal(refA, "term-1");
325+
326+
useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
327+
328+
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
329+
isOpen: true,
330+
activeSurfaceId: "browser:tab-a",
331+
surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
332+
});
333+
});
334+
335+
it("closing all surfaces leaves the panel open and empty", () => {
336+
useRightPanelStore.getState().openBrowser(refA, "tab-a");
337+
useRightPanelStore.getState().openFile(refA, "src/index.ts");
338+
339+
useRightPanelStore.getState().closeAllSurfaces(refA);
340+
341+
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
342+
isOpen: true,
343+
activeSurfaceId: null,
344+
surfaces: [],
345+
});
346+
});
347+
307348
it("reconciles browser surfaces without deleting other surface kinds", () => {
308349
useRightPanelStore.getState().openTerminal(refA, "term-1");
309350
useRightPanelStore.getState().openBrowser(refA, "tab-a");

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ interface RightPanelStoreState {
5858
closeTerminal: (ref: ScopedThreadRef, surfaceId: string, terminalId: string) => void;
5959
activateSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
6060
closeSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
61+
closeOtherSurfaces: (ref: ScopedThreadRef, surfaceId: string) => void;
62+
closeSurfacesToRight: (ref: ScopedThreadRef, surfaceId: string) => void;
63+
closeAllSurfaces: (ref: ScopedThreadRef) => void;
6164
reconcileBrowserSurfaces: (ref: ScopedThreadRef, tabIds: readonly string[]) => void;
6265
reconcileFileSurfaces: (ref: ScopedThreadRef, workspaceAvailable: boolean) => void;
6366
show: (ref: ScopedThreadRef) => void;
@@ -334,6 +337,43 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
334337
return { ...current, surfaces, activeSurfaceId: fallback?.id ?? null };
335338
}),
336339
})),
340+
closeOtherSurfaces: (ref, surfaceId) =>
341+
set((state) => ({
342+
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) => {
343+
const surface = current.surfaces.find((entry) => entry.id === surfaceId);
344+
if (!surface || current.surfaces.length === 1) return current;
345+
return {
346+
...current,
347+
isOpen: true,
348+
surfaces: [surface],
349+
activeSurfaceId: surface.id,
350+
};
351+
}),
352+
})),
353+
closeSurfacesToRight: (ref, surfaceId) =>
354+
set((state) => ({
355+
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) => {
356+
const index = current.surfaces.findIndex((surface) => surface.id === surfaceId);
357+
if (index < 0 || index === current.surfaces.length - 1) return current;
358+
const surfaces = current.surfaces.slice(0, index + 1);
359+
const activeStillExists = surfaces.some(
360+
(surface) => surface.id === current.activeSurfaceId,
361+
);
362+
return {
363+
...current,
364+
surfaces,
365+
activeSurfaceId: activeStillExists ? current.activeSurfaceId : surfaceId,
366+
};
367+
}),
368+
})),
369+
closeAllSurfaces: (ref) =>
370+
set((state) => ({
371+
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) =>
372+
current.surfaces.length === 0
373+
? current
374+
: { ...current, isOpen: true, surfaces: [], activeSurfaceId: null },
375+
),
376+
})),
337377
reconcileBrowserSurfaces: (ref, tabIds) =>
338378
set((state) => ({
339379
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) => {

0 commit comments

Comments
 (0)