Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useRightPanelActions.ts

334 lines12.8 KB

1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2import type { ThreadPreviewState } from "../../previewStateStore";
3import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
4import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
5import { resolveExplicitDiffOpenOnActivate } from "../ChatView.logic";
6import { useStableCallback } from "../../hooks/useStableCallback";
7import { type ScopedThreadRef, type RunId } from "@t3tools/contracts";
8import { scopedThreadKey } from "@t3tools/client-runtime/environment";
9import { useEffect } from "react";
10import {
11 isAtomCommandInterrupted,
12 squashAtomCommandFailure,
13} from "@t3tools/client-runtime/state/runtime";
14import { useDiffPanelStore } from "../../diffPanelStore";
15import { type ThreadPanelPresentation } from "../../rightPanelLayout";
16import {
17 selectActiveRightPanel,
18 type RightPanelSurface,
19 useRightPanelStore,
20} from "../../rightPanelStore";
21import { setActivePreviewTab } from "../../previewStateStore";
22import { BrowserSettingsReadError } from "../../browser/openFileInPreview";
23import { addBrowserSurface } from "../preview/addBrowserSurface";
24import { focusActivePanelTerminal, focusTerminal } from "../../lib/terminalFocus";
25import { subscribePreviewAction } from "../preview/previewActionBus";
26import { floatDeviceSurface } from "../preview/agentOpenedPreviews";
27import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
28import type { threadPullRequestPanelTarget } from "../pullRequest/pullRequestDetail.logic";
29import type { useDeviceState } from "../../state/device";
30import { stackedThreadToast, toastManager } from "../ui/toast";
31import { openDeviceSetupDialog } from "./ChatOverlayHosts";
32import { previewEnvironment } from "../../state/preview";
33import { useAtomCommand } from "../../state/use-atom-command";
34import type { ChatShortcutCommands } from "./useChatShortcuts";
35interface RightPanelActionsInput {
36 readonly activeThreadRef: ScopedThreadRef | null;
37 readonly activeProject: EnvironmentProject | null;
38 readonly activeRightPanelSurface: RightPanelSurface | null;
39 readonly activePreviewState: ThreadPreviewState;
40 readonly deviceState: ReturnType<typeof useDeviceState>["state"];
41 readonly deviceStateLoaded: boolean;
42 readonly pullRequestPanelTarget: ReturnType<typeof threadPullRequestPanelTarget> | null;
43 readonly threadPanelPresentation: ThreadPanelPresentation;
44 readonly isServerThread: boolean;
45 readonly isGitRepo: boolean;
46 readonly browserAvailable: boolean;
47 readonly canOperatePreview: boolean;
48 readonly diffOpen: boolean;
49 readonly previewPanelOpen: boolean;
50 readonly rightPanelOpen: boolean;
51 readonly canMaximizeRightPanel: boolean;
52 readonly rightPanelMaximized: boolean;
53 readonly supportsPullRequests: boolean;
54 readonly pullRequestsSurfaceAvailable: boolean;
55 readonly gitCwd: string | null;
56 readonly activeProjectCwd: string | null;
57 readonly explicitDiffOpenRef: { current: ScopedThreadRef | null };
58 readonly onDiffPanelOpen: (() => void) | undefined;
59 readonly closeRightPanelSurface: (surface: RightPanelSurface) => void;
61​
62/**
63 * Opening, closing, and arranging the right panel's surfaces: browser, diff, files, pull
64 * requests, device, and terminal tabs.
65 */
66export function useRightPanelActions(input: RightPanelActionsInput) {
67 const {
68 closeRightPanelSurface,
69 activeThreadRef,
70 activeProject,
71 activeRightPanelSurface,
72 activePreviewState,
73 deviceState,
74 deviceStateLoaded,
75 pullRequestPanelTarget,
76 threadPanelPresentation,
77 isServerThread,
78 isGitRepo,
79 browserAvailable,
80 canOperatePreview,
81 diffOpen,
82 previewPanelOpen,
83 rightPanelOpen,
84 canMaximizeRightPanel,
85 rightPanelMaximized,
86 supportsPullRequests,
87 pullRequestsSurfaceAvailable,
88 gitCwd,
89 activeProjectCwd,
90 explicitDiffOpenRef,
91 onDiffPanelOpen,
92 } = input;
93​
94 const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
95 const onToggleDiff = () => {
96 if (!isServerThread) {
97 return;
98 }
99 if (!diffOpen) {
100 onDiffPanelOpen?.();
101 }
102 if (activeThreadRef) {
103 useRightPanelStore.getState().toggle(activeThreadRef, "diff");
104 }
105 };
106 const onToggleDiffStable = useStableCallback(onToggleDiff);
107 const createBrowserSurface = (profileId?: string) => {
108 if (!activeThreadRef || !canOperatePreview) return;
109 void addBrowserSurface({
110 threadRef: activeThreadRef,
111 openPreview,
112 ...(profileId === undefined ? {} : { profileId }),
113 }).then((result) => {
114 if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
115 const error = squashAtomCommandFailure(result);
116 if (error instanceof BrowserSettingsReadError) {
117 toastManager.add(
118 stackedThreadToast({
119 type: "error",
120 title: "Unable to open browser",
121 description: error.message,
122 }),
123 );
124 }
125 });
126 };
127 const createBrowserSurfaceStable = useStableCallback(createBrowserSurface);
128 const addDiffSurface = () => {
129 if (!activeThreadRef || !isServerThread || !isGitRepo) return;
130 useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");
131 useRightPanelStore.getState().open(activeThreadRef, "diff");
132 onDiffPanelOpen?.();
133 };
134 const addDiffSurfaceStable = useStableCallback(addDiffSurface);
135 const openChangesFromThreadPanel = () => {
136 addDiffSurface();
137 };
138 const openChangesFromThreadPanelStable = useStableCallback(openChangesFromThreadPanel);
139 const addFilesSurface = () => {
140 if (!activeThreadRef || !activeProject) return;
141 useRightPanelStore.getState().open(activeThreadRef, "files");
142 };
143 const addFilesSurfaceStable = useStableCallback(addFilesSurface);
144 const addPullRequestsSurface = () => {
145 if (!activeThreadRef || !pullRequestsSurfaceAvailable) return;
146 useRightPanelStore.getState().open(activeThreadRef, "pull-requests");
147 };
148 const addPullRequestsSurfaceStable = useStableCallback(addPullRequestsSurface);
149 const addDeviceSurface = () => {
150 if (!activeThreadRef) return;
151 if (!deviceState.onboardingCompleted || deviceState.hostStatus === "disabled") {
152 openDeviceSetupDialog(scopedThreadKey(activeThreadRef));
153 return;
154 }
155 useRightPanelStore.getState().open(activeThreadRef, "device");
156 };
157 const addDeviceSurfaceStable = useStableCallback(addDeviceSurface);
158 const openFileSurface = (relativePath: string) => {
159 if (!activeThreadRef || !activeProject) return;
160 useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
161 };
162 const openFileSurfaceStable = useStableCallback(openFileSurface);
163 const closePreviewPanel = () => {
164 if (activeThreadRef) {
165 if (activeRightPanelSurface?.kind === "preview" && activeRightPanelSurface.resourceId) {
166 usePreviewMiniPlayerStore
167 .getState()
168 .open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
169 } else if (activeRightPanelSurface?.kind === "device" && activeRightPanelSurface.target) {
170 floatDeviceSurface(
171 activeThreadRef,
172 { kind: "device", ...activeRightPanelSurface.target },
173 deviceStateLoaded ? deviceState : null,
174 );
175 }
176 useRightPanelStore.getState().setMaximized(activeThreadRef, false);
177 useRightPanelStore.getState().close(activeThreadRef);
178 }
179 };
180 const closePreviewPanelStable = useStableCallback(closePreviewPanel);
181 const togglePreviewPanel = () => {
182 if (!activeThreadRef || !browserAvailable) return;
183 if (previewPanelOpen) {
184 closePreviewPanel();
185 return;
186 }
187 if (!canOperatePreview) return;
188 const activeTabId = activePreviewState.activeTabId;
189 if (activeTabId) {
190 useRightPanelStore.getState().openBrowser(activeThreadRef, activeTabId);
191 } else {
192 createBrowserSurface();
193 }
194 };
195 const togglePreviewPanelStable = useStableCallback(togglePreviewPanel);
196 const activateRightPanelSurface = (surface: RightPanelSurface) => {
197 if (!activeThreadRef) return;
198 explicitDiffOpenRef.current = resolveExplicitDiffOpenOnActivate({
199 surfaceKind: surface.kind,
200 diffOpen,
201 activeThreadRef,
202 });
203 useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
204 if (surface.kind === "preview" && surface.resourceId) {
205 setActivePreviewTab(activeThreadRef, surface.resourceId);
206 }
207 if (surface.kind === "terminal") {
208 focusTerminal(activeThreadRef, surface.activeTerminalId);
209 }
210 if (surface.kind === "diff" && !diffOpen) {
211 onDiffPanelOpen?.();
212 }
213 };
214 const activateRightPanelSurfaceStable = useStableCallback(activateRightPanelSurface);
215 const toggleRightPanel = () => {
216 if (!activeThreadRef) return;
217 if (rightPanelOpen) {
218 closePreviewPanel();
219 return;
220 }
221 useRightPanelStore.getState().toggleVisibility(activeThreadRef);
222 focusActivePanelTerminal(activeThreadRef);
223 };
224 const toggleRightPanelStable = useStableCallback(toggleRightPanel);
225 const toggleThreadPanel = () => {
226 if (!activeThreadRef) return;
227 useRightPanelStore.getState().toggleThreadPanel(activeThreadRef, threadPanelPresentation);
228 };
229 const toggleThreadPanelStable = useStableCallback(toggleThreadPanel);
230 const toggleRightPanelMaximized = () => {
231 if (!canMaximizeRightPanel || !activeThreadRef) return;
232 useRightPanelStore.getState().setMaximized(activeThreadRef, !rightPanelMaximized);
233 };
234 const toggleRightPanelMaximizedStable = useStableCallback(toggleRightPanelMaximized);
235 const moveRightPanelSurface = (surfaceId: string, toIndex: number) => {
236 if (activeThreadRef)
237 useRightPanelStore.getState().moveSurface(activeThreadRef, surfaceId, toIndex);
238 };
239 const moveRightPanelSurfaceStable = useStableCallback(moveRightPanelSurface);
240 const copyRightPanelFilePath = (relativePath: string) => {
241 if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
242 toastManager.add(
243 stackedThreadToast({
244 type: "error",
245 title: "Failed to copy path",
246 description: "Clipboard API unavailable.",
247 }),
248 );
249 return;
250 }
251​
252 const clipboardPath = formatFilePathForClipboard(
253 relativePath,
254 resolvePathLinkTarget(relativePath, gitCwd ?? activeProjectCwd ?? ""),
255 );
256 void navigator.clipboard.writeText(clipboardPath).then(
257 () => {
258 toastManager.add({
259 type: "success",
260 title: "Path copied",
261 description: clipboardPath,
262 });
263 },
264 (error) => {
265 toastManager.add(
266 stackedThreadToast({
267 type: "error",
268 title: "Failed to copy path",
269 description: error instanceof Error ? error.message : "An error occurred.",
270 }),
271 );
272 },
273 );
274 };
275 const copyRightPanelFilePathStable = useStableCallback(copyRightPanelFilePath);
276 const addPullRequestSurface = () => {
277 if (!supportsPullRequests || activeThreadRef === null || pullRequestPanelTarget === null)
278 return;
279 useRightPanelStore.getState().openPullRequest(activeThreadRef, pullRequestPanelTarget);
280 };
281 const addPullRequestSurfaceStable = useStableCallback(addPullRequestSurface);
282 const onOpenTurnDiff = (runId: RunId, filePath?: string) => {
283 if (!isServerThread || !activeThreadRef) return;
284 const diffOpen =
285 selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
286 explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
287 useDiffPanelStore.getState().selectTurn(activeThreadRef, runId, filePath);
288 useRightPanelStore.getState().open(activeThreadRef, "diff");
289 onDiffPanelOpen?.();
290 };
291 const onOpenTurnDiffStable = useStableCallback(onOpenTurnDiff);
292 useEffect(
293 () =>
294 subscribePreviewAction((action) => {
295 if (action === "toggle-panel") togglePreviewPanelStable();
296 }),
297 [togglePreviewPanelStable],
298 );
299​
300 const shortcuts = {
301 "rightPanel.toggle": () => toggleRightPanel(),
302 "rightPanel.toggleMaximized": ({ event }) => {
303 if (!event.repeat) toggleRightPanelMaximized();
304 },
305 "threadPanel.toggle": () => toggleThreadPanel(),
306 "rightPanel.close": ({ event }) => {
307 if (!activeRightPanelSurface) return false;
308 if (!event.repeat) closeRightPanelSurface(activeRightPanelSurface);
309 },
310 "diff.toggle": () => onToggleDiff(),
311 } satisfies ChatShortcutCommands;
312​
313 return {
314 shortcuts,
315 activateRightPanelSurfaceStable,
316 addDeviceSurfaceStable,
317 addDiffSurfaceStable,
318 addFilesSurfaceStable,
319 addPullRequestSurfaceStable,
320 addPullRequestsSurfaceStable,
321 closePreviewPanelStable,
322 copyRightPanelFilePathStable,
323 createBrowserSurface,
324 createBrowserSurfaceStable,
325 moveRightPanelSurfaceStable,
326 onOpenTurnDiffStable,
327 onToggleDiffStable,
328 openChangesFromThreadPanelStable,
329 openFileSurfaceStable,
330 toggleRightPanelMaximizedStable,
331 toggleRightPanelStable,
332 toggleThreadPanelStable,
333 };