@devries-cameron20 / t3code Mirror · read-only / commits / c07eddfbed
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 08:00 UTC

refactor(web): move right panel surface closing out of ChatView (#20315)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 17:39 UTC c07eddfbed54b79db05fdf9698a9f087f87563bb
3 files changed +159 -123
apps/web/src/components/ChatView.tsx
+11 -123
@@ -240,7 +240,6 @@ import { BrowserSettingsReadError, openUrlInPreview } from "../browser/openFileI
240 import { resolveDiscoveredServerUrl } from "../browser/browserTargetResolver";
241 import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";
242 import { addBrowserSurface } from "./preview/addBrowserSurface";
243 -import { closePreviewSession } from "./preview/closePreviewSession";
243 import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
244 import { usePreviewSession } from "./preview/usePreviewSession";
245 import { subscribePreviewAction } from "./preview/previewActionBus";
@@ -462,6 +461,7 @@ import {
461 import { QueuedRunsControl, type QueuedRunsControlHandle } from "./chat/QueuedRunsControl";
462 import { useThreadLinkedPullRequest } from "./pullRequest/useThreadLinkedPullRequest";
463 import { useProactivePanels } from "../hooks/useProactivePanels";
464 +import { useRightPanelSurfaceClose } from "../hooks/useRightPanelSurfaceClose";
465 import { useAgentOpenedPreviews } from "./preview/useAgentOpenedPreviews";
466 import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";
467 import { ThreadStatusLine } from "./chat/ThreadStatusLine";
@@ -487,7 +487,6 @@ import {
487 import type { ComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch";
488 import {
489 MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
490 - agentControlledBrowserCloseConfirmation,
490 branchMismatchKey,
491 buildExpiredTerminalContextToastCopy,
492 buildLocalDraftThread,
@@ -1040,7 +1039,6 @@ export default function ChatView(props: ChatViewProps) {
1039 reportFailure: false,
1040 });
1041 const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
1043 - const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
1042 const { environments } = useEnvironments();
1043 const primaryEnvironment = usePrimaryEnvironment();
1044 const retryEnvironment = useAtomCommand(environmentCatalog.retryNow, { reportFailure: false });
@@ -4336,126 +4334,16 @@ export default function ChatView(props: ChatViewProps) {
4334 useRightPanelStore.getState().setMaximized(activeThreadRef, !rightPanelMaximized);
4335 };
4336 const toggleRightPanelMaximizedStable = useStableCallback(toggleRightPanelMaximized);
4339 - const cleanupRightPanelSurfaces = (surfaces: readonly RightPanelSurface[]) => {
4340 - if (!activeThreadRef) return;
4341 - for (const surface of surfaces) {
4342 - if (canOperatePreview && surface.kind === "preview" && surface.resourceId) {
4343 - void closePreviewSession({
4344 - closePreview,
4345 - snapshot: activePreviewState.sessions[surface.resourceId] ?? null,
4346 - tabId: surface.resourceId,
4347 - threadRef: activeThreadRef,
4348 - });
4349 - }
4350 - if (
4351 - surface.kind === "terminal" &&
4352 - readEnvironmentScope(activeThreadRef.environmentId, AuthTerminalOperateScope)
4353 - ) {
4354 - for (const terminalId of surface.terminalIds) {
4355 - storeCloseTerminal(activeThreadRef, terminalId);
4356 - terminalActions.closeTerminalSession(terminalId);
4357 - }
4358 - }
4359 - }
4360 - };
4361 - const closeAfterAgentBrowserConfirmation = (
4362 - surfaces: readonly RightPanelSurface[],
4363 - closeSurfaces: () => void,
4364 - ) => {
4365 - const message = agentControlledBrowserCloseConfirmation(
4366 - surfaces,
4367 - activePreviewState.desktopByTabId,
4368 - );
4369 - if (!message) {
4370 - closeSurfaces();
4371 - return;
4372 - }
4373 - const localApi = readLocalApi();
4374 - if (!localApi) return;
4375 - void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
4376 - (confirmed) => {
4377 - if (confirmed) closeSurfaces();
4378 - },
4379 - () => undefined,
4380 - );
4381 - };
4382 - const syncActivePreviewSurface = () => {
4383 - if (!activeThreadRef) return;
4384 - const nextActiveSurface = selectActiveRightPanelSurface(
4385 - useRightPanelStore.getState().byThreadKey,
4386 - activeThreadRef,
4387 - );
4388 - if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
4389 - setActivePreviewTab(activeThreadRef, nextActiveSurface.resourceId);
4390 - }
4391 - };
4392 - const finishRightPanelSurfaceClose = (surfaces: readonly RightPanelSurface[]) => {
4393 - if (!activeThreadRef) return;
4394 - const store = useRightPanelStore.getState();
4395 - const activeId = selectThreadRightPanelState(
4396 - store.byThreadKey,
4397 - activeThreadRef,
4398 - ).activeSurfaceId;
4399 - const ordered = surfaces.toSorted(
4400 - (left, right) => Number(left.id === activeId) - Number(right.id === activeId),
4401 - );
4402 - for (const surface of ordered) {
4403 - cleanupRightPanelSurfaces([surface]);
4404 - store.closeSurface(activeThreadRef, surface.id);
4405 - }
4406 - syncActivePreviewSurface();
4407 - };
4408 - const closeRightPanelSurface = (surface: RightPanelSurface) => {
4409 - if (!activeThreadRef) return;
4410 - const finishClose = () => finishRightPanelSurfaceClose([surface]);
4411 - if (surface.kind === "preview") {
4412 - closeAfterAgentBrowserConfirmation([surface], finishClose);
4413 - return;
4414 - }
4415 - if (
4416 - surface.kind !== "terminal" ||
4417 - !readEnvironmentScope(activeThreadRef.environmentId, AuthTerminalOperateScope)
4418 - ) {
4419 - finishClose();
4420 - return;
4421 - }
4422 - const activeLabel =
4423 - activeTerminalLabelsById.get(surface.activeTerminalId) ??
4424 - getTerminalLabel(surface.activeTerminalId);
4425 - const otherLabels = surface.terminalIds
4426 - .filter((terminalId) => terminalId !== surface.activeTerminalId)
4427 - .map(
4428 - (terminalId) => activeTerminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId),
4429 - );
4430 - void confirmTerminalClose([activeLabel, ...otherLabels]).then((confirmed) => {
4431 - if (confirmed) {
4432 - finishClose();
4433 - }
4434 - });
4435 - };
4436 - const closeRightPanelSurfaceStable = useStableCallback(closeRightPanelSurface);
4437 - const closeOtherRightPanelSurfaces = (surface: RightPanelSurface) => {
4438 - if (!activeThreadRef) return;
4439 - const surfaces = rightPanelState.surfaces.filter((entry) => entry.id !== surface.id);
4440 - const finishClose = () => finishRightPanelSurfaceClose(surfaces);
4441 - closeAfterAgentBrowserConfirmation(surfaces, finishClose);
4442 - };
4443 - const closeOtherRightPanelSurfacesStable = useStableCallback(closeOtherRightPanelSurfaces);
4444 - const closeRightPanelSurfacesToRight = (surface: RightPanelSurface) => {
4445 - if (!activeThreadRef) return;
4446 - const surfaceIndex = rightPanelState.surfaces.findIndex((entry) => entry.id === surface.id);
4447 - if (surfaceIndex < 0) return;
4448 - const surfaces = rightPanelState.surfaces.slice(surfaceIndex + 1);
4449 - const finishClose = () => finishRightPanelSurfaceClose(surfaces);
4450 - closeAfterAgentBrowserConfirmation(surfaces, finishClose);
4451 - };
4452 - const closeRightPanelSurfacesToRightStable = useStableCallback(closeRightPanelSurfacesToRight);
4453 - const closeAllRightPanelSurfaces = () => {
4454 - if (!activeThreadRef) return;
4455 - const finishClose = () => finishRightPanelSurfaceClose(rightPanelState.surfaces);
4456 - closeAfterAgentBrowserConfirmation(rightPanelState.surfaces, finishClose);
4457 - };
4458 - const closeAllRightPanelSurfacesStable = useStableCallback(closeAllRightPanelSurfaces);
4337 + const {
4338 + closeSurface: closeRightPanelSurface,
4339 + closeOtherSurfaces: closeOtherRightPanelSurfacesStable,
4340 + closeSurfacesToRight: closeRightPanelSurfacesToRightStable,
4341 + closeAllSurfaces: closeAllRightPanelSurfacesStable,
4342 + } = useRightPanelSurfaceClose({
4343 + threadRef: activeThreadRef,
4344 + terminalLabelsById: activeTerminalLabelsById,
4345 + });
4346 + const closeRightPanelSurfaceStable = closeRightPanelSurface;
4347 const moveRightPanelSurface = (surfaceId: string, toIndex: number) => {
4348 if (activeThreadRef)
4349 useRightPanelStore.getState().moveSurface(activeThreadRef, surfaceId, toIndex);
apps/web/src/hooks/useRightPanelSurfaceClose.ts new
+147
@@ -0,0 +1,147 @@
1 +import {
2 + AuthPreviewOperateScope,
3 + AuthTerminalOperateScope,
4 + type ScopedThreadRef,
5 +} from "@t3tools/contracts";
6 +import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
7 +import { agentControlledBrowserCloseConfirmation } from "../components/ChatView.logic";
8 +import { closePreviewSession } from "../components/preview/closePreviewSession";
9 +import { useThreadTerminalActions } from "../components/terminal/useThreadTerminalActions";
10 +import { confirmTerminalClose } from "../lib/terminalCloseConfirm";
11 +import { readLocalApi } from "../localApi";
12 +import { setActivePreviewTab, useThreadPreviewState } from "../previewStateStore";
13 +import {
14 + selectActiveRightPanelSurface,
15 + selectThreadRightPanelState,
16 + useRightPanelStore,
17 + type RightPanelSurface,
18 +} from "../rightPanelStore";
19 +import { previewEnvironment } from "../state/preview";
20 +import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
21 +import { useAtomCommand } from "../state/use-atom-command";
22 +import { useTerminalUiStateStore } from "../terminalUiStateStore";
23 +import { useStableCallback } from "./useStableCallback";
24 +
25 +/**
26 + * Closes right panel surfaces for a thread. A surface's live resources go with
27 + * it: a browser's preview session and a terminal tab's sessions. Closing a
28 + * browser an agent is driving, or a terminal tab, asks first.
29 + */
30 +export function useRightPanelSurfaceClose(input: {
31 + readonly threadRef: ScopedThreadRef | null;
32 + readonly terminalLabelsById: ReadonlyMap<string, string>;
33 +}) {
34 + const { threadRef, terminalLabelsById } = input;
35 + const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
36 + const canOperatePreview = useEnvironmentScope(
37 + threadRef?.environmentId ?? null,
38 + AuthPreviewOperateScope,
39 + );
40 + const previewState = useThreadPreviewState(threadRef);
41 + const terminalActions = useThreadTerminalActions(threadRef);
42 + const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal);
43 + const surfaces = useRightPanelStore(
44 + (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
45 + );
46 +
47 + const releaseSurfaceResources = (ref: ScopedThreadRef, surface: RightPanelSurface) => {
48 + // Without preview access only the local surface goes away. The
49 + // server record is in-memory bookkeeping, not a live view, and stays
50 + // listed until a client with the scope closes it or the server restarts.
51 + if (canOperatePreview && surface.kind === "preview" && surface.resourceId) {
52 + void closePreviewSession({
53 + closePreview,
54 + snapshot: previewState.sessions[surface.resourceId] ?? null,
55 + tabId: surface.resourceId,
56 + threadRef: ref,
57 + });
58 + }
59 + if (
60 + surface.kind === "terminal" &&
61 + readEnvironmentScope(ref.environmentId, AuthTerminalOperateScope)
62 + ) {
63 + for (const terminalId of surface.terminalIds) {
64 + storeCloseTerminal(ref, terminalId);
65 + terminalActions.closeTerminalSession(terminalId);
66 + }
67 + }
68 + };
69 + const closeAfterAgentBrowserConfirmation = (
70 + toClose: readonly RightPanelSurface[],
71 + closeSurfaces: () => void,
72 + ) => {
73 + const message = agentControlledBrowserCloseConfirmation(toClose, previewState.desktopByTabId);
74 + if (!message) {
75 + closeSurfaces();
76 + return;
77 + }
78 + const localApi = readLocalApi();
79 + if (!localApi) return;
80 + void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
81 + (confirmed) => {
82 + if (confirmed) closeSurfaces();
83 + },
84 + () => undefined,
85 + );
86 + };
87 + // The active surface closes last so the panel does not flash through it.
88 + const finishClose = (toClose: readonly RightPanelSurface[]) => {
89 + if (!threadRef) return;
90 + const store = useRightPanelStore.getState();
91 + const activeId = selectThreadRightPanelState(store.byThreadKey, threadRef).activeSurfaceId;
92 + const ordered = toClose.toSorted(
93 + (left, right) => Number(left.id === activeId) - Number(right.id === activeId),
94 + );
95 + for (const surface of ordered) {
96 + releaseSurfaceResources(threadRef, surface);
97 + store.closeSurface(threadRef, surface.id);
98 + }
99 + const nextActiveSurface = selectActiveRightPanelSurface(
100 + useRightPanelStore.getState().byThreadKey,
101 + threadRef,
102 + );
103 + if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
104 + setActivePreviewTab(threadRef, nextActiveSurface.resourceId);
105 + }
106 + };
107 + const labelFor = (terminalId: string) =>
108 + terminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
109 + const closeSurfaces = (toClose: readonly RightPanelSurface[]) => {
110 + if (!threadRef) return;
111 + closeAfterAgentBrowserConfirmation(toClose, () => finishClose(toClose));
112 + };
113 +
114 + const closeSurface = useStableCallback((surface: RightPanelSurface) => {
115 + if (!threadRef) return;
116 + if (surface.kind === "preview") {
117 + closeSurfaces([surface]);
118 + return;
119 + }
120 + if (
121 + surface.kind !== "terminal" ||
122 + !readEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope)
123 + ) {
124 + finishClose([surface]);
125 + return;
126 + }
127 + const otherLabels = surface.terminalIds
128 + .filter((terminalId) => terminalId !== surface.activeTerminalId)
129 + .map(labelFor);
130 + void confirmTerminalClose([labelFor(surface.activeTerminalId), ...otherLabels]).then(
131 + (confirmed) => {
132 + if (confirmed) finishClose([surface]);
133 + },
134 + );
135 + });
136 + const closeOtherSurfaces = useStableCallback((surface: RightPanelSurface) =>
137 + closeSurfaces(surfaces.filter((entry) => entry.id !== surface.id)),
138 + );
139 + const closeSurfacesToRight = useStableCallback((surface: RightPanelSurface) => {
140 + const surfaceIndex = surfaces.findIndex((entry) => entry.id === surface.id);
141 + if (surfaceIndex < 0) return;
142 + closeSurfaces(surfaces.slice(surfaceIndex + 1));
143 + });
144 + const closeAllSurfaces = useStableCallback(() => closeSurfaces(surfaces));
145 +
146 + return { closeSurface, closeOtherSurfaces, closeSurfacesToRight, closeAllSurfaces };
147 +}
vite.config.ts
+1
@@ -334,6 +334,7 @@ export default defineConfig({
334 "apps/web/src/components/preview/useAgentOpenedPreviews.ts",
335 "apps/web/src/components/terminal/**",
336 "apps/web/src/hooks/useProactivePanels.ts",
337 + "apps/web/src/hooks/useRightPanelSurfaceClose.ts",
338 ],
339 rules: {
340 "react/exhaustive-effect-dependencies": "error",