The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): ChatView's right panel actions live in useRightPanelActions (#23064)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 21, 2026 at 13:40 UTC
ebe828cd9ddee677d8c9adb3f5c33a772d285978
2 files changed
+359
-217
apps/web/src/components/ChatView.tsx
+46
-217
@@ -1,7 +1,6 @@
1
+import { useRightPanelActions } from "./chat/useRightPanelActions";
2
import { type TerminalLaunchContext, useChatTerminalActions } from "./chat/useChatTerminalActions";
3
import { useComposerSend } from "./chat/useComposerSend";
3
-import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
4
-import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
4
import { ThreadFind, ThreadFindCanvas, type ThreadFindControls } from "./chat/ThreadFindProvider";
5
import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";
6
import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";
@@ -12,7 +11,6 @@ import {
11
peekHeldThreadTimeline,
12
peekRememberedThreadTimeline,
13
rememberReadyThreadTimeline,
15
- resolveExplicitDiffOpenOnActivate,
14
resolveThreadSwitchTimeline,
15
timelineHasEphemeralPreviewUrls,
16
recallCheckoutIsRepo,
@@ -159,24 +157,15 @@ import {
157
selectActiveRightPanelSurface,
158
selectThreadPanelOpen,
159
selectThreadRightPanelState,
162
- type RightPanelSurface,
160
useRightPanelStore,
161
} from "../rightPanelStore";
165
-import {
166
- isPreviewSupportedInRuntime,
167
- setActivePreviewTab,
168
- useThreadPreviewState,
169
-} from "../previewStateStore";
170
-import { BrowserSettingsReadError } from "../browser/openFileInPreview";
162
+import { isPreviewSupportedInRuntime, useThreadPreviewState } from "../previewStateStore";
163
import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";
172
-import { addBrowserSurface } from "./preview/addBrowserSurface";
164
import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
165
import { usePreviewSession } from "./preview/usePreviewSession";
175
-import { subscribePreviewAction } from "./preview/previewActionBus";
166
import { getConfiguredPreviewUrls } from "./preview/previewEmptyStateLogic";
167
168
import {
179
- browserMiniPlayerSource,
169
previewMiniPlayerSourceKey,
170
selectThreadPreviewMiniPlayer,
171
usePreviewMiniPlayerStore,
@@ -286,7 +275,6 @@ import {
275
BranchRestoreConfirmHost,
276
DeviceSetupDialogHost,
277
openBranchRestoreConfirm,
289
- openDeviceSetupDialog,
278
openPullRequestThreadDialog,
279
openRevertConfirm,
280
PullRequestThreadDialogHost,
@@ -380,7 +368,6 @@ import {
368
} from "./ChatView.logic";
369
import { useLocalStorage } from "~/hooks/useLocalStorage";
370
import { useComposerHandleContext } from "../composerHandleContext";
383
-import { previewEnvironment } from "../state/preview";
371
import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
372
import { appAtomRegistry } from "../rpc/atomRegistry";
373
import { assetEnvironment } from "../state/assets";
@@ -509,7 +496,6 @@ export default function ChatView(props: ChatViewProps) {
496
const forkThreadFromRun = useAtomCommand(threadEnvironment.forkFromRun, {
497
reportFailure: false,
498
});
512
- const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
499
const { environments } = useEnvironments();
500
const providersByEnvironment = useEnvironmentProviders();
501
const primaryEnvironment = usePrimaryEnvironment();
@@ -2305,18 +2291,6 @@ export default function ChatView(props: ChatViewProps) {
2291
"terminal.close",
2292
terminalShortcutLabelOptions,
2293
);
2308
- const onToggleDiff = () => {
2309
- if (!isServerThread) {
2310
- return;
2311
- }
2312
- if (!diffOpen) {
2313
- onDiffPanelOpen?.();
2314
- }
2315
- if (activeThreadRef) {
2316
- useRightPanelStore.getState().toggle(activeThreadRef, "diff");
2317
- }
2318
- };
2319
- const onToggleDiffStable = useStableCallback(onToggleDiff);
2294
2295
const {
2296
needsLoadBalancing,
@@ -2498,43 +2472,6 @@ export default function ChatView(props: ChatViewProps) {
2472
activeThreadRef?.environmentId ?? null,
2473
AuthPreviewOperateScope,
2474
);
2501
- const createBrowserSurface = (profileId?: string) => {
2502
- if (!activeThreadRef || !canOperatePreview) return;
2503
- void addBrowserSurface({
2504
- threadRef: activeThreadRef,
2505
- openPreview,
2506
- ...(profileId === undefined ? {} : { profileId }),
2507
- }).then((result) => {
2508
- if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
2509
- const error = squashAtomCommandFailure(result);
2510
- if (error instanceof BrowserSettingsReadError) {
2511
- toastManager.add(
2512
- stackedThreadToast({
2513
- type: "error",
2514
- title: "Unable to open browser",
2515
- description: error.message,
2516
- }),
2517
- );
2518
- }
2519
- });
2520
- };
2521
- const createBrowserSurfaceStable = useStableCallback(createBrowserSurface);
2522
- const addDiffSurface = () => {
2523
- if (!activeThreadRef || !isServerThread || !isGitRepo) return;
2524
- useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");
2525
- useRightPanelStore.getState().open(activeThreadRef, "diff");
2526
- onDiffPanelOpen?.();
2527
- };
2528
- const addDiffSurfaceStable = useStableCallback(addDiffSurface);
2529
- const openChangesFromThreadPanel = () => {
2530
- addDiffSurface();
2531
- };
2532
- const openChangesFromThreadPanelStable = useStableCallback(openChangesFromThreadPanel);
2533
- const addFilesSurface = () => {
2534
- if (!activeThreadRef || !activeProject) return;
2535
- useRightPanelStore.getState().open(activeThreadRef, "files");
2536
- };
2537
- const addFilesSurfaceStable = useStableCallback(addFilesSurface);
2475
const supportsThreadPullRequests =
2476
serverConfig?.environment.capabilities.threadPullRequests === true;
2477
const visiblePullRequests = visibleThreadPullRequests(
@@ -2543,23 +2480,9 @@ export default function ChatView(props: ChatViewProps) {
2480
const visiblePullRequestCount = visiblePullRequests.length;
2481
const pullRequestsSurfaceAvailable =
2482
isServerThread && supportsThreadPullRequests && visiblePullRequestCount > 0;
2546
- const addPullRequestsSurface = () => {
2547
- if (!activeThreadRef || !pullRequestsSurfaceAvailable) return;
2548
- useRightPanelStore.getState().open(activeThreadRef, "pull-requests");
2549
- };
2550
- const addPullRequestsSurfaceStable = useStableCallback(addPullRequestsSurface);
2483
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
2484
activeThreadRef?.environmentId ?? null,
2485
);
2554
- const addDeviceSurface = () => {
2555
- if (!activeThreadRef) return;
2556
- if (!deviceState.onboardingCompleted || deviceState.hostStatus === "disabled") {
2557
- openDeviceSetupDialog(scopedThreadKey(activeThreadRef));
2558
- return;
2559
- }
2560
- useRightPanelStore.getState().open(activeThreadRef, "device");
2561
- };
2562
- const addDeviceSurfaceStable = useStableCallback(addDeviceSurface);
2486
useAgentOpenedPreviews({
2487
threadRef: activeThreadRef,
2488
deviceState,
@@ -2570,11 +2493,6 @@ export default function ChatView(props: ChatViewProps) {
2493
miniPlayerSource: activePreviewMiniPlayer?.source ?? null,
2494
sheetLayout: shouldUsePlanSidebarSheet,
2495
});
2573
- const openFileSurface = (relativePath: string) => {
2574
- if (!activeThreadRef || !activeProject) return;
2575
- useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
2576
- };
2577
- const openFileSurfaceStable = useStableCallback(openFileSurface);
2496
// The thread's own change request, placed against the project it belongs to. Without a
2497
// project there is nothing to resolve it against, so the caller falls back to the browser.
2498
const activeProjectRepository = sourceControlRepositorySelector(
@@ -2620,81 +2538,12 @@ export default function ChatView(props: ChatViewProps) {
2538
visiblePullRequestCount,
2539
onDiffPanelOpen,
2540
});
2623
- const closePreviewPanel = () => {
2624
- if (activeThreadRef) {
2625
- if (activeRightPanelSurface?.kind === "preview" && activeRightPanelSurface.resourceId) {
2626
- usePreviewMiniPlayerStore
2627
- .getState()
2628
- .open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
2629
- } else if (activeRightPanelSurface?.kind === "device" && activeRightPanelSurface.target) {
2630
- usePreviewMiniPlayerStore
2631
- .getState()
2632
- .open(activeThreadRef, { kind: "device", ...activeRightPanelSurface.target });
2633
- }
2634
- useRightPanelStore.getState().setMaximized(activeThreadRef, false);
2635
- useRightPanelStore.getState().close(activeThreadRef);
2636
- }
2637
- };
2638
- const closePreviewPanelStable = useStableCallback(closePreviewPanel);
2639
- const togglePreviewPanel = () => {
2640
- if (!activeThreadRef || !browserAvailable) return;
2641
- if (previewPanelOpen) {
2642
- closePreviewPanel();
2643
- return;
2644
- }
2645
- if (!canOperatePreview) return;
2646
- const activeTabId = activePreviewState.activeTabId;
2647
- if (activeTabId) {
2648
- useRightPanelStore.getState().openBrowser(activeThreadRef, activeTabId);
2649
- } else {
2650
- createBrowserSurface();
2651
- }
2652
- };
2653
- const togglePreviewPanelStable = useStableCallback(togglePreviewPanel);
2654
- const activateRightPanelSurface = (surface: RightPanelSurface) => {
2655
- if (!activeThreadRef) return;
2656
- explicitDiffOpenRef.current = resolveExplicitDiffOpenOnActivate({
2657
- surfaceKind: surface.kind,
2658
- diffOpen,
2659
- activeThreadRef,
2660
- });
2661
- useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
2662
- if (surface.kind === "preview" && surface.resourceId) {
2663
- setActivePreviewTab(activeThreadRef, surface.resourceId);
2664
- }
2665
- if (surface.kind === "terminal") {
2666
- setTerminalFocusRequestId((value) => value + 1);
2667
- }
2668
- if (surface.kind === "diff" && !diffOpen) {
2669
- onDiffPanelOpen?.();
2670
- }
2671
- };
2672
- const activateRightPanelSurfaceStable = useStableCallback(activateRightPanelSurface);
2673
- const toggleRightPanel = () => {
2674
- if (!activeThreadRef) return;
2675
- if (rightPanelOpen) {
2676
- closePreviewPanel();
2677
- return;
2678
- }
2679
- useRightPanelStore.getState().toggleVisibility(activeThreadRef);
2680
- };
2681
- const toggleRightPanelStable = useStableCallback(toggleRightPanel);
2682
- const toggleThreadPanel = () => {
2683
- if (!activeThreadRef) return;
2684
- useRightPanelStore.getState().toggleThreadPanel(activeThreadRef, threadPanelPresentation);
2685
- };
2686
- const toggleThreadPanelStable = useStableCallback(toggleThreadPanel);
2541
useEffect(() => {
2542
if (!canMaximizeRightPanel) return;
2543
if (useRightPanelStore.getState().consumeMaximizeRequest(routeThreadRef)) {
2544
useRightPanelStore.getState().setMaximized(routeThreadRef, true);
2545
}
2546
}, [canMaximizeRightPanel, routeThreadRef]);
2693
- const toggleRightPanelMaximized = () => {
2694
- if (!canMaximizeRightPanel || !activeThreadRef) return;
2695
- useRightPanelStore.getState().setMaximized(activeThreadRef, !rightPanelMaximized);
2696
- };
2697
- const toggleRightPanelMaximizedStable = useStableCallback(toggleRightPanelMaximized);
2547
const {
2548
closeSurface: closeRightPanelSurface,
2549
closeOtherSurfaces: closeOtherRightPanelSurfacesStable,
@@ -2705,54 +2554,6 @@ export default function ChatView(props: ChatViewProps) {
2554
terminalLabelsById: activeTerminalLabelsById,
2555
});
2556
const closeRightPanelSurfaceStable = closeRightPanelSurface;
2708
- const moveRightPanelSurface = (surfaceId: string, toIndex: number) => {
2709
- if (activeThreadRef)
2710
- useRightPanelStore.getState().moveSurface(activeThreadRef, surfaceId, toIndex);
2711
- };
2712
- const moveRightPanelSurfaceStable = useStableCallback(moveRightPanelSurface);
2713
- const copyRightPanelFilePath = (relativePath: string) => {
2714
- if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
2715
- toastManager.add(
2716
- stackedThreadToast({
2717
- type: "error",
2718
- title: "Failed to copy path",
2719
- description: "Clipboard API unavailable.",
2720
- }),
2721
- );
2722
- return;
2723
- }
2724
-
2725
- const clipboardPath = formatFilePathForClipboard(
2726
- relativePath,
2727
- resolvePathLinkTarget(relativePath, gitCwd ?? activeProjectCwd ?? ""),
2728
- );
2729
- void navigator.clipboard.writeText(clipboardPath).then(
2730
- () => {
2731
- toastManager.add({
2732
- type: "success",
2733
- title: "Path copied",
2734
- description: clipboardPath,
2735
- });
2736
- },
2737
- (error) => {
2738
- toastManager.add(
2739
- stackedThreadToast({
2740
- type: "error",
2741
- title: "Failed to copy path",
2742
- description: error instanceof Error ? error.message : "An error occurred.",
2743
- }),
2744
- );
2745
- },
2746
- );
2747
- };
2748
- const copyRightPanelFilePathStable = useStableCallback(copyRightPanelFilePath);
2749
- useEffect(
2750
- () =>
2751
- subscribePreviewAction((action) => {
2752
- if (action === "toggle-panel") togglePreviewPanelStable();
2753
- }),
2754
- [togglePreviewPanelStable],
2755
- );
2557
const persistThreadSettingsForNextTurn = async (input: {
2558
threadId: ThreadId;
2559
createdAt: string;
@@ -3171,12 +2972,50 @@ export default function ChatView(props: ChatViewProps) {
2972
branchPullRequest: activeThreadShell?.branchPullRequest ?? activeThread.branchPullRequest,
2973
})
2974
: null;
3174
- const addPullRequestSurface = () => {
3175
- if (!supportsPullRequests || activeThreadRef === null || pullRequestPanelTarget === null)
3176
- return;
3177
- useRightPanelStore.getState().openPullRequest(activeThreadRef, pullRequestPanelTarget);
3178
- };
3179
- const addPullRequestSurfaceStable = useStableCallback(addPullRequestSurface);
2975
+ const {
2976
+ activateRightPanelSurfaceStable,
2977
+ addDeviceSurfaceStable,
2978
+ addDiffSurfaceStable,
2979
+ addFilesSurfaceStable,
2980
+ addPullRequestSurfaceStable,
2981
+ addPullRequestsSurfaceStable,
2982
+ closePreviewPanelStable,
2983
+ copyRightPanelFilePathStable,
2984
+ createBrowserSurface,
2985
+ createBrowserSurfaceStable,
2986
+ moveRightPanelSurfaceStable,
2987
+ onOpenTurnDiffStable,
2988
+ onToggleDiffStable,
2989
+ openChangesFromThreadPanelStable,
2990
+ openFileSurfaceStable,
2991
+ toggleRightPanelMaximizedStable,
2992
+ toggleRightPanelStable,
2993
+ toggleThreadPanelStable,
2994
+ } = useRightPanelActions({
2995
+ activeThreadRef,
2996
+ activeProject,
2997
+ activeRightPanelSurface,
2998
+ activePreviewState,
2999
+ deviceState,
3000
+ pullRequestPanelTarget,
3001
+ threadPanelPresentation,
3002
+ isServerThread,
3003
+ isGitRepo,
3004
+ browserAvailable,
3005
+ canOperatePreview,
3006
+ diffOpen,
3007
+ previewPanelOpen,
3008
+ rightPanelOpen,
3009
+ canMaximizeRightPanel,
3010
+ rightPanelMaximized,
3011
+ supportsPullRequests,
3012
+ pullRequestsSurfaceAvailable,
3013
+ gitCwd,
3014
+ activeProjectCwd,
3015
+ explicitDiffOpenRef,
3016
+ setTerminalFocusRequestId,
3017
+ onDiffPanelOpen,
3018
+ });
3019
const pullRequestSurfaceAvailable = supportsPullRequests && pullRequestPanelTarget !== null;
3020
const supportsSettlement = serverConfig?.environment.capabilities.threadSettlement === true;
3021
const supportsSnooze = serverConfig?.environment.capabilities.threadSnooze === true;
@@ -4462,16 +4301,6 @@ export default function ChatView(props: ChatViewProps) {
4301
openExpandedImagePreview(routeThreadKey, preview);
4302
};
4303
const onExpandTimelineImageStable = useStableCallback(onExpandTimelineImage);
4465
- const onOpenTurnDiff = (runId: RunId, filePath?: string) => {
4466
- if (!isServerThread || !activeThreadRef) return;
4467
- const diffOpen =
4468
- selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
4469
- explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
4470
- useDiffPanelStore.getState().selectTurn(activeThreadRef, runId, filePath);
4471
- useRightPanelStore.getState().open(activeThreadRef, "diff");
4472
- onDiffPanelOpen?.();
4473
- };
4474
- const onOpenTurnDiffStable = useStableCallback(onOpenTurnDiff);
4304
const onTimelineRollbackCheckpoint = (input: {
4305
readonly checkpointId: string;
4306
readonly scopeId: string;
apps/web/src/components/chat/useRightPanelActions.ts
new
+313
@@ -0,0 +1,313 @@
1
+import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2
+import type { Dispatch } from "react";
3
+import type { ThreadPreviewState } from "../../previewStateStore";
4
+import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
5
+import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
6
+import { resolveExplicitDiffOpenOnActivate } from "../ChatView.logic";
7
+import { useStableCallback } from "../../hooks/useStableCallback";
8
+import { type ScopedThreadRef, type RunId } from "@t3tools/contracts";
9
+import { scopedThreadKey } from "@t3tools/client-runtime/environment";
10
+import { type SetStateAction, useEffect } from "react";
11
+import {
12
+ isAtomCommandInterrupted,
13
+ squashAtomCommandFailure,
14
+} from "@t3tools/client-runtime/state/runtime";
15
+import { useDiffPanelStore } from "../../diffPanelStore";
16
+import { type ThreadPanelPresentation } from "../../rightPanelLayout";
17
+import {
18
+ selectActiveRightPanel,
19
+ type RightPanelSurface,
20
+ useRightPanelStore,
21
+} from "../../rightPanelStore";
22
+import { setActivePreviewTab } from "../../previewStateStore";
23
+import { BrowserSettingsReadError } from "../../browser/openFileInPreview";
24
+import { addBrowserSurface } from "../preview/addBrowserSurface";
25
+import { subscribePreviewAction } from "../preview/previewActionBus";
26
+import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
27
+import type { threadPullRequestPanelTarget } from "../pullRequest/pullRequestDetail.logic";
28
+import type { useDeviceState } from "../../state/device";
29
+import { stackedThreadToast, toastManager } from "../ui/toast";
30
+import { openDeviceSetupDialog } from "./ChatOverlayHosts";
31
+import { previewEnvironment } from "../../state/preview";
32
+import { useAtomCommand } from "../../state/use-atom-command";
33
+interface RightPanelActionsInput {
34
+ readonly activeThreadRef: ScopedThreadRef | null;
35
+ readonly activeProject: EnvironmentProject | null;
36
+ readonly activeRightPanelSurface: RightPanelSurface | null;
37
+ readonly activePreviewState: ThreadPreviewState;
38
+ readonly deviceState: ReturnType<typeof useDeviceState>["state"];
39
+ readonly pullRequestPanelTarget: ReturnType<typeof threadPullRequestPanelTarget> | null;
40
+ readonly threadPanelPresentation: ThreadPanelPresentation;
41
+ readonly isServerThread: boolean;
42
+ readonly isGitRepo: boolean;
43
+ readonly browserAvailable: boolean;
44
+ readonly canOperatePreview: boolean;
45
+ readonly diffOpen: boolean;
46
+ readonly previewPanelOpen: boolean;
47
+ readonly rightPanelOpen: boolean;
48
+ readonly canMaximizeRightPanel: boolean;
49
+ readonly rightPanelMaximized: boolean;
50
+ readonly supportsPullRequests: boolean;
51
+ readonly pullRequestsSurfaceAvailable: boolean;
52
+ readonly gitCwd: string | null;
53
+ readonly activeProjectCwd: string | null;
54
+ readonly explicitDiffOpenRef: { current: ScopedThreadRef | null };
55
+ readonly setTerminalFocusRequestId: Dispatch<SetStateAction<number>>;
56
+ readonly onDiffPanelOpen: (() => void) | undefined;
57
+}
58
+
59
+/**
60
+ * Opening, closing, and arranging the right panel's surfaces: browser, diff, files, pull
61
+ * requests, device, and terminal tabs.
62
+ */
63
+export function useRightPanelActions(input: RightPanelActionsInput) {
64
+ const {
65
+ activeThreadRef,
66
+ activeProject,
67
+ activeRightPanelSurface,
68
+ activePreviewState,
69
+ deviceState,
70
+ pullRequestPanelTarget,
71
+ threadPanelPresentation,
72
+ isServerThread,
73
+ isGitRepo,
74
+ browserAvailable,
75
+ canOperatePreview,
76
+ diffOpen,
77
+ previewPanelOpen,
78
+ rightPanelOpen,
79
+ canMaximizeRightPanel,
80
+ rightPanelMaximized,
81
+ supportsPullRequests,
82
+ pullRequestsSurfaceAvailable,
83
+ gitCwd,
84
+ activeProjectCwd,
85
+ explicitDiffOpenRef,
86
+ setTerminalFocusRequestId,
87
+ onDiffPanelOpen,
88
+ } = input;
89
+
90
+ const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
91
+ const onToggleDiff = () => {
92
+ if (!isServerThread) {
93
+ return;
94
+ }
95
+ if (!diffOpen) {
96
+ onDiffPanelOpen?.();
97
+ }
98
+ if (activeThreadRef) {
99
+ useRightPanelStore.getState().toggle(activeThreadRef, "diff");
100
+ }
101
+ };
102
+ const onToggleDiffStable = useStableCallback(onToggleDiff);
103
+ const createBrowserSurface = (profileId?: string) => {
104
+ if (!activeThreadRef || !canOperatePreview) return;
105
+ void addBrowserSurface({
106
+ threadRef: activeThreadRef,
107
+ openPreview,
108
+ ...(profileId === undefined ? {} : { profileId }),
109
+ }).then((result) => {
110
+ if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
111
+ const error = squashAtomCommandFailure(result);
112
+ if (error instanceof BrowserSettingsReadError) {
113
+ toastManager.add(
114
+ stackedThreadToast({
115
+ type: "error",
116
+ title: "Unable to open browser",
117
+ description: error.message,
118
+ }),
119
+ );
120
+ }
121
+ });
122
+ };
123
+ const createBrowserSurfaceStable = useStableCallback(createBrowserSurface);
124
+ const addDiffSurface = () => {
125
+ if (!activeThreadRef || !isServerThread || !isGitRepo) return;
126
+ useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");
127
+ useRightPanelStore.getState().open(activeThreadRef, "diff");
128
+ onDiffPanelOpen?.();
129
+ };
130
+ const addDiffSurfaceStable = useStableCallback(addDiffSurface);
131
+ const openChangesFromThreadPanel = () => {
132
+ addDiffSurface();
133
+ };
134
+ const openChangesFromThreadPanelStable = useStableCallback(openChangesFromThreadPanel);
135
+ const addFilesSurface = () => {
136
+ if (!activeThreadRef || !activeProject) return;
137
+ useRightPanelStore.getState().open(activeThreadRef, "files");
138
+ };
139
+ const addFilesSurfaceStable = useStableCallback(addFilesSurface);
140
+ const addPullRequestsSurface = () => {
141
+ if (!activeThreadRef || !pullRequestsSurfaceAvailable) return;
142
+ useRightPanelStore.getState().open(activeThreadRef, "pull-requests");
143
+ };
144
+ const addPullRequestsSurfaceStable = useStableCallback(addPullRequestsSurface);
145
+ const addDeviceSurface = () => {
146
+ if (!activeThreadRef) return;
147
+ if (!deviceState.onboardingCompleted || deviceState.hostStatus === "disabled") {
148
+ openDeviceSetupDialog(scopedThreadKey(activeThreadRef));
149
+ return;
150
+ }
151
+ useRightPanelStore.getState().open(activeThreadRef, "device");
152
+ };
153
+ const addDeviceSurfaceStable = useStableCallback(addDeviceSurface);
154
+ const openFileSurface = (relativePath: string) => {
155
+ if (!activeThreadRef || !activeProject) return;
156
+ useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
157
+ };
158
+ const openFileSurfaceStable = useStableCallback(openFileSurface);
159
+ const closePreviewPanel = () => {
160
+ if (activeThreadRef) {
161
+ if (activeRightPanelSurface?.kind === "preview" && activeRightPanelSurface.resourceId) {
162
+ usePreviewMiniPlayerStore
163
+ .getState()
164
+ .open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
165
+ } else if (activeRightPanelSurface?.kind === "device" && activeRightPanelSurface.target) {
166
+ usePreviewMiniPlayerStore
167
+ .getState()
168
+ .open(activeThreadRef, { kind: "device", ...activeRightPanelSurface.target });
169
+ }
170
+ useRightPanelStore.getState().setMaximized(activeThreadRef, false);
171
+ useRightPanelStore.getState().close(activeThreadRef);
172
+ }
173
+ };
174
+ const closePreviewPanelStable = useStableCallback(closePreviewPanel);
175
+ const togglePreviewPanel = () => {
176
+ if (!activeThreadRef || !browserAvailable) return;
177
+ if (previewPanelOpen) {
178
+ closePreviewPanel();
179
+ return;
180
+ }
181
+ if (!canOperatePreview) return;
182
+ const activeTabId = activePreviewState.activeTabId;
183
+ if (activeTabId) {
184
+ useRightPanelStore.getState().openBrowser(activeThreadRef, activeTabId);
185
+ } else {
186
+ createBrowserSurface();
187
+ }
188
+ };
189
+ const togglePreviewPanelStable = useStableCallback(togglePreviewPanel);
190
+ const activateRightPanelSurface = (surface: RightPanelSurface) => {
191
+ if (!activeThreadRef) return;
192
+ explicitDiffOpenRef.current = resolveExplicitDiffOpenOnActivate({
193
+ surfaceKind: surface.kind,
194
+ diffOpen,
195
+ activeThreadRef,
196
+ });
197
+ useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
198
+ if (surface.kind === "preview" && surface.resourceId) {
199
+ setActivePreviewTab(activeThreadRef, surface.resourceId);
200
+ }
201
+ if (surface.kind === "terminal") {
202
+ setTerminalFocusRequestId((value) => value + 1);
203
+ }
204
+ if (surface.kind === "diff" && !diffOpen) {
205
+ onDiffPanelOpen?.();
206
+ }
207
+ };
208
+ const activateRightPanelSurfaceStable = useStableCallback(activateRightPanelSurface);
209
+ const toggleRightPanel = () => {
210
+ if (!activeThreadRef) return;
211
+ if (rightPanelOpen) {
212
+ closePreviewPanel();
213
+ return;
214
+ }
215
+ useRightPanelStore.getState().toggleVisibility(activeThreadRef);
216
+ };
217
+ const toggleRightPanelStable = useStableCallback(toggleRightPanel);
218
+ const toggleThreadPanel = () => {
219
+ if (!activeThreadRef) return;
220
+ useRightPanelStore.getState().toggleThreadPanel(activeThreadRef, threadPanelPresentation);
221
+ };
222
+ const toggleThreadPanelStable = useStableCallback(toggleThreadPanel);
223
+ const toggleRightPanelMaximized = () => {
224
+ if (!canMaximizeRightPanel || !activeThreadRef) return;
225
+ useRightPanelStore.getState().setMaximized(activeThreadRef, !rightPanelMaximized);
226
+ };
227
+ const toggleRightPanelMaximizedStable = useStableCallback(toggleRightPanelMaximized);
228
+ const moveRightPanelSurface = (surfaceId: string, toIndex: number) => {
229
+ if (activeThreadRef)
230
+ useRightPanelStore.getState().moveSurface(activeThreadRef, surfaceId, toIndex);
231
+ };
232
+ const moveRightPanelSurfaceStable = useStableCallback(moveRightPanelSurface);
233
+ const copyRightPanelFilePath = (relativePath: string) => {
234
+ if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
235
+ toastManager.add(
236
+ stackedThreadToast({
237
+ type: "error",
238
+ title: "Failed to copy path",
239
+ description: "Clipboard API unavailable.",
240
+ }),
241
+ );
242
+ return;
243
+ }
244
+
245
+ const clipboardPath = formatFilePathForClipboard(
246
+ relativePath,
247
+ resolvePathLinkTarget(relativePath, gitCwd ?? activeProjectCwd ?? ""),
248
+ );
249
+ void navigator.clipboard.writeText(clipboardPath).then(
250
+ () => {
251
+ toastManager.add({
252
+ type: "success",
253
+ title: "Path copied",
254
+ description: clipboardPath,
255
+ });
256
+ },
257
+ (error) => {
258
+ toastManager.add(
259
+ stackedThreadToast({
260
+ type: "error",
261
+ title: "Failed to copy path",
262
+ description: error instanceof Error ? error.message : "An error occurred.",
263
+ }),
264
+ );
265
+ },
266
+ );
267
+ };
268
+ const copyRightPanelFilePathStable = useStableCallback(copyRightPanelFilePath);
269
+ const addPullRequestSurface = () => {
270
+ if (!supportsPullRequests || activeThreadRef === null || pullRequestPanelTarget === null)
271
+ return;
272
+ useRightPanelStore.getState().openPullRequest(activeThreadRef, pullRequestPanelTarget);
273
+ };
274
+ const addPullRequestSurfaceStable = useStableCallback(addPullRequestSurface);
275
+ const onOpenTurnDiff = (runId: RunId, filePath?: string) => {
276
+ if (!isServerThread || !activeThreadRef) return;
277
+ const diffOpen =
278
+ selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
279
+ explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
280
+ useDiffPanelStore.getState().selectTurn(activeThreadRef, runId, filePath);
281
+ useRightPanelStore.getState().open(activeThreadRef, "diff");
282
+ onDiffPanelOpen?.();
283
+ };
284
+ const onOpenTurnDiffStable = useStableCallback(onOpenTurnDiff);
285
+ useEffect(
286
+ () =>
287
+ subscribePreviewAction((action) => {
288
+ if (action === "toggle-panel") togglePreviewPanelStable();
289
+ }),
290
+ [togglePreviewPanelStable],
291
+ );
292
+
293
+ return {
294
+ activateRightPanelSurfaceStable,
295
+ addDeviceSurfaceStable,
296
+ addDiffSurfaceStable,
297
+ addFilesSurfaceStable,
298
+ addPullRequestSurfaceStable,
299
+ addPullRequestsSurfaceStable,
300
+ closePreviewPanelStable,
301
+ copyRightPanelFilePathStable,
302
+ createBrowserSurface,
303
+ createBrowserSurfaceStable,
304
+ moveRightPanelSurfaceStable,
305
+ onOpenTurnDiffStable,
306
+ onToggleDiffStable,
307
+ openChangesFromThreadPanelStable,
308
+ openFileSurfaceStable,
309
+ toggleRightPanelMaximizedStable,
310
+ toggleRightPanelStable,
311
+ toggleThreadPanelStable,
312
+ };
313
+}