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

useRightPanelActions.ts

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