Add right-panel bulk close and tab context menu actions (#3116)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-06-16 16:39:40 -07:00
1 parent c2ca9de33a
commit e56bb200fa
4 files changed
+266 -22

No files matched your search

+109 -20
View File
@@ -3100,26 +3100,19 @@ function ChatViewContent(props: ChatViewProps) {
threadKey === routeThreadKey ? null : routeThreadKey,
);
};
const closeRightPanelSurface = useCallback(
(surface: RightPanelSurface) => {
const cleanupRightPanelSurfaces = useCallback(
(surfaces: readonly RightPanelSurface[]) => {
if (!activeThreadRef) return;
if (surface.kind === "preview" && surface.resourceId) {
usePreviewStateStore.getState().removeSession(activeThreadRef, surface.resourceId);
const api = readEnvironmentApi(activeThreadRef.environmentId);
void api?.preview
.close({ threadId: activeThreadRef.threadId, tabId: surface.resourceId })
.catch(() => undefined);
}
useRightPanelStore.getState().closeSurface(activeThreadRef, surface.id);
const nextActiveSurface = selectActiveRightPanelSurface(
useRightPanelStore.getState().byThreadKey,
activeThreadRef,
);
if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
usePreviewStateStore.getState().setActiveTab(activeThreadRef, nextActiveSurface.resourceId);
}
if (surface.kind === "terminal") {
const api = readEnvironmentApi(activeThreadRef.environmentId);
const api = readEnvironmentApi(activeThreadRef.environmentId);
for (const surface of surfaces) {
if (surface.kind === "preview" && surface.resourceId) {
usePreviewStateStore.getState().removeSession(activeThreadRef, surface.resourceId);
void api?.preview
.close({ threadId: activeThreadRef.threadId, tabId: surface.resourceId })
.catch(() => undefined);
}
if (surface.kind !== "terminal") continue;
for (const terminalId of surface.terminalIds) {
void api?.terminal
.close({
@@ -3130,7 +3123,8 @@ function ChatViewContent(props: ChatViewProps) {
.catch(() => undefined);
}
}
if (surface.kind === "diff" && diffOpen) {
if (diffOpen && surfaces.some((surface) => surface.kind === "diff")) {
void navigate({
to: "/$environmentId/$threadId",
params: { environmentId, threadId },
@@ -3141,6 +3135,93 @@ function ChatViewContent(props: ChatViewProps) {
},
[activeThreadRef, diffOpen, environmentId, navigate, threadId],
);
const syncActivePreviewSurface = useCallback(() => {
if (!activeThreadRef) return;
const nextActiveSurface = selectActiveRightPanelSurface(
useRightPanelStore.getState().byThreadKey,
activeThreadRef,
);
if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
usePreviewStateStore.getState().setActiveTab(activeThreadRef, nextActiveSurface.resourceId);
}
}, [activeThreadRef]);
const closeRightPanelSurface = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
cleanupRightPanelSurfaces([surface]);
useRightPanelStore.getState().closeSurface(activeThreadRef, surface.id);
syncActivePreviewSurface();
},
[activeThreadRef, cleanupRightPanelSurfaces, syncActivePreviewSurface],
);
const closeOtherRightPanelSurfaces = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
const surfaces = rightPanelState.surfaces.filter((entry) => entry.id !== surface.id);
cleanupRightPanelSurfaces(surfaces);
useRightPanelStore.getState().closeOtherSurfaces(activeThreadRef, surface.id);
syncActivePreviewSurface();
},
[
activeThreadRef,
cleanupRightPanelSurfaces,
rightPanelState.surfaces,
syncActivePreviewSurface,
],
);
const closeRightPanelSurfacesToRight = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
const surfaceIndex = rightPanelState.surfaces.findIndex((entry) => entry.id === surface.id);
if (surfaceIndex < 0) return;
const surfaces = rightPanelState.surfaces.slice(surfaceIndex + 1);
cleanupRightPanelSurfaces(surfaces);
useRightPanelStore.getState().closeSurfacesToRight(activeThreadRef, surface.id);
syncActivePreviewSurface();
},
[
activeThreadRef,
cleanupRightPanelSurfaces,
rightPanelState.surfaces,
syncActivePreviewSurface,
],
);
const closeAllRightPanelSurfaces = useCallback(() => {
if (!activeThreadRef) return;
cleanupRightPanelSurfaces(rightPanelState.surfaces);
useRightPanelStore.getState().closeAllSurfaces(activeThreadRef);
}, [activeThreadRef, cleanupRightPanelSurfaces, rightPanelState.surfaces]);
const copyRightPanelFilePath = useCallback((relativePath: string) => {
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to copy path",
description: "Clipboard API unavailable.",
}),
);
return;
}
void navigator.clipboard.writeText(relativePath).then(
() => {
toastManager.add({
type: "success",
title: "Path copied",
description: relativePath,
});
},
(error) => {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to copy path",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
},
);
}, []);
const persistThreadSettingsForNextTurn = useCallback(
async (input: {
threadId: ThreadId;
@@ -4851,6 +4932,10 @@ function ChatViewContent(props: ChatViewProps) {
terminalLabelsById={activeTerminalLabelsById}
onActivate={activateRightPanelSurface}
onCloseSurface={closeRightPanelSurface}
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
onCloseAllSurfaces={closeAllRightPanelSurfaces}
onCopyFilePath={copyRightPanelFilePath}
onAddBrowser={createBrowserSurface}
onAddTerminal={addTerminalSurface}
onAddDiff={addDiffSurface}
@@ -4929,6 +5014,10 @@ function ChatViewContent(props: ChatViewProps) {
terminalLabelsById={activeTerminalLabelsById}
onActivate={activateRightPanelSurface}
onCloseSurface={closeRightPanelSurface}
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
onCloseAllSurfaces={closeAllRightPanelSurfaces}
onCopyFilePath={copyRightPanelFilePath}
onAddBrowser={createBrowserSurface}
onAddTerminal={addTerminalSurface}
onAddDiff={addDiffSurface}
+76 -2
View File
@@ -1,11 +1,20 @@
import type { PreviewSessionSnapshot } from "@t3tools/contracts";
import type { ContextMenuItem, PreviewSessionSnapshot } from "@t3tools/contracts";
import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
import { ClipboardList, FileDiff, Files, Globe2, Plus, TerminalSquare, X } from "lucide-react";
import { type ReactElement, type ReactNode, useEffect, useRef, useState } from "react";
import {
type MouseEvent as ReactMouseEvent,
type ReactElement,
type ReactNode,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { isElectron } from "~/env";
import type { RightPanelSurface } from "~/rightPanelStore";
import { cn } from "~/lib/utils";
import { readLocalApi } from "~/localApi";
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
import { ScrollArea } from "~/components/ui/scroll-area";
@@ -25,6 +34,10 @@ interface RightPanelTabsProps {
terminalLabelsById: ReadonlyMap<string, string>;
onActivate: (surface: RightPanelSurface) => void;
onCloseSurface: (surface: RightPanelSurface) => void;
onCloseOtherSurfaces: (surface: RightPanelSurface) => void;
onCloseSurfacesToRight: (surface: RightPanelSurface) => void;
onCloseAllSurfaces: () => void;
onCopyFilePath: (relativePath: string) => void;
onAddBrowser: () => void;
onAddTerminal: () => void;
onAddDiff: () => void;
@@ -41,6 +54,8 @@ const SURFACE_DISABLED_REASONS = {
diff: "Diff is only available for server threads in Git repositories.",
} as const;
type TabContextMenuAction = "copy-path" | "close" | "close-others" | "close-to-right" | "close-all";
function DisabledReasonTooltip(props: { reason: string; trigger: ReactElement }) {
return (
<Tooltip>
@@ -257,6 +272,64 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
const { resolvedTheme } = useTheme();
const tabListRef = useRef<HTMLDivElement>(null);
const handleTabContextMenu = useCallback(
async (event: ReactMouseEvent, surface: RightPanelSurface) => {
event.preventDefault();
event.stopPropagation();
const api = readLocalApi();
if (!api) return;
const surfaceIndex = props.surfaces.findIndex((entry) => entry.id === surface.id);
if (surfaceIndex < 0) return;
const items: ContextMenuItem<TabContextMenuAction>[] = [];
if (surface.kind === "file") {
items.push({ id: "copy-path", label: "Copy path" });
}
items.push(
{ id: "close", label: "Close" },
{
id: "close-others",
label: "Close others",
disabled: props.surfaces.length <= 1,
},
{
id: "close-to-right",
label: "Close to the right",
disabled: surfaceIndex >= props.surfaces.length - 1,
},
{
id: "close-all",
label: "Close all",
disabled: props.surfaces.length === 0,
},
);
const action = await api.contextMenu.show(items, { x: event.clientX, y: event.clientY });
switch (action) {
case "copy-path":
if (surface.kind === "file") props.onCopyFilePath(surface.relativePath);
break;
case "close":
props.onCloseSurface(surface);
break;
case "close-others":
props.onCloseOtherSurfaces(surface);
break;
case "close-to-right":
props.onCloseSurfacesToRight(surface);
break;
case "close-all":
props.onCloseAllSurfaces();
break;
case null:
break;
}
},
[props],
);
useEffect(() => {
const activeTab = tabListRef.current?.querySelector<HTMLElement>("[data-active-tab='true']");
activeTab?.scrollIntoView({ block: "nearest", inline: "nearest" });
@@ -291,6 +364,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
<div
key={surface.id}
data-active-tab={active}
onContextMenu={(event) => void handleTabContextMenu(event, surface)}
className={cn(
"group flex h-7 min-w-25 max-w-44 shrink-0 items-center gap-1.5 rounded-md px-2 text-sm",
active
+41
View File
@@ -304,6 +304,47 @@ describe("rightPanelStore", () => {
});
});
it("closing other surfaces keeps the selected surface active", () => {
useRightPanelStore.getState().openBrowser(refA, "tab-a");
useRightPanelStore.getState().openFile(refA, "src/index.ts");
useRightPanelStore.getState().openTerminal(refA, "term-1");
useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
isOpen: true,
activeSurfaceId: "file:src/index.ts",
surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
});
});
it("closing surfaces to the right activates the selected surface when active was removed", () => {
useRightPanelStore.getState().openBrowser(refA, "tab-a");
useRightPanelStore.getState().openFile(refA, "src/index.ts");
useRightPanelStore.getState().openTerminal(refA, "term-1");
useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
isOpen: true,
activeSurfaceId: "browser:tab-a",
surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
});
});
it("closing all surfaces leaves the panel open and empty", () => {
useRightPanelStore.getState().openBrowser(refA, "tab-a");
useRightPanelStore.getState().openFile(refA, "src/index.ts");
useRightPanelStore.getState().closeAllSurfaces(refA);
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
isOpen: true,
activeSurfaceId: null,
surfaces: [],
});
});
it("reconciles browser surfaces without deleting other surface kinds", () => {
useRightPanelStore.getState().openTerminal(refA, "term-1");
useRightPanelStore.getState().openBrowser(refA, "tab-a");
+40
View File
@@ -58,6 +58,9 @@ interface RightPanelStoreState {
closeTerminal: (ref: ScopedThreadRef, surfaceId: string, terminalId: string) => void;
activateSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
closeSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
closeOtherSurfaces: (ref: ScopedThreadRef, surfaceId: string) => void;
closeSurfacesToRight: (ref: ScopedThreadRef, surfaceId: string) => void;
closeAllSurfaces: (ref: ScopedThreadRef) => void;
reconcileBrowserSurfaces: (ref: ScopedThreadRef, tabIds: readonly string[]) => void;
reconcileFileSurfaces: (ref: ScopedThreadRef, workspaceAvailable: boolean) => void;
show: (ref: ScopedThreadRef) => void;
@@ -334,6 +337,43 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
return { ...current, surfaces, activeSurfaceId: fallback?.id ?? null };
}),
})),
closeOtherSurfaces: (ref, surfaceId) =>
set((state) => ({
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) => {
const surface = current.surfaces.find((entry) => entry.id === surfaceId);
if (!surface || current.surfaces.length === 1) return current;
return {
...current,
isOpen: true,
surfaces: [surface],
activeSurfaceId: surface.id,
};
}),
})),
closeSurfacesToRight: (ref, surfaceId) =>
set((state) => ({
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) => {
const index = current.surfaces.findIndex((surface) => surface.id === surfaceId);
if (index < 0 || index === current.surfaces.length - 1) return current;
const surfaces = current.surfaces.slice(0, index + 1);
const activeStillExists = surfaces.some(
(surface) => surface.id === current.activeSurfaceId,
);
return {
...current,
surfaces,
activeSurfaceId: activeStillExists ? current.activeSurfaceId : surfaceId,
};
}),
})),
closeAllSurfaces: (ref) =>
set((state) => ({
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) =>
current.surfaces.length === 0
? current
: { ...current, isOpen: true, surfaces: [], activeSurfaceId: null },
),
})),
reconcileBrowserSurfaces: (ref, tabIds) =>
set((state) => ({
byThreadKey: updateThread(state.byThreadKey, scopedThreadKey(ref), (current) => {