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

main
tsx 291 lines 10.8 KB
Raw
1 import { useGT } from "gt-react";
2 import type {
3 EditorId,
4 EnvironmentId,
5 IssueTrackerKind,
6 PreviewAnnotationPayload,
7 ProjectId,
8 ResolvedKeybindingsConfig,
9 ScopedThreadRef,
10 ThreadLinkedPullRequest,
11 ThreadPullRequestLink,
12 } from "@t3tools/contracts";
13 import { scopedThreadKey } from "@t3tools/client-runtime/environment";
14 import { lazy, Suspense } from "react";
15 import { LinkedThreadPane } from "./LinkedThreadPane";
16 import SideThreadPane from "./SideThreadPane";
17 import type { ComposerImageAttachment, DraftId } from "../../composerDraftStore";
18 import type { ShortcutMatchContext } from "../../keybindings";
19 import type { SideChatParentPanel } from "../../lib/sideChatPane";
20 import { type RightPanelSurface, useRightPanelStore } from "../../rightPanelStore";
21 import {
22 PersistentThreadTerminalPanel,
23 type PersistentThreadTerminalPanelProps,
24 } from "../PersistentThreadTerminals";
25 import { IssueDetailPanel } from "../issue/IssueDetailPanel";
26 import { pullRequestPanelContext } from "../pullRequest/pullRequestDetail.logic";
27 import { PullRequestDetailPanel } from "../pullRequest/PullRequestDetailPanel";
28 import { PullRequestDetailGhost } from "../pullRequest/PullRequestGhosts";
29 import { PullRequestsUnavailableState } from "../pullRequest/PullRequestsUnavailableState";
30 import { ThreadPullRequestsPanel } from "../pullRequest/ThreadPullRequestsPanel";
31
32 const PreviewPanel = lazy(() =>
33 import("../preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
34 );
35 const DiffPanel = lazy(() => import("../DiffPanel"));
36 const DevicePanel = lazy(() =>
37 import("../device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
38 );
39 const FilePreviewPanel = lazy(() => import("../files/FilePreviewPanel"));
40
41 type RightPanelTerminalProps = Omit<
42 PersistentThreadTerminalPanelProps,
43 "visible" | "threadRef" | "surface"
44 >;
45
46 interface RightPanelPullRequestProps {
47 readonly capabilityKnown: boolean;
48 readonly supported: boolean;
49 readonly projectId: ProjectId | null;
50 readonly pullRequests: ReadonlyArray<ThreadPullRequestLink>;
51 readonly linkedPullRequest: ThreadLinkedPullRequest | null;
52 readonly branchPullRequest: ThreadLinkedPullRequest | null | undefined;
53 readonly onBack: (() => void) | undefined;
54 }
55
56 interface RightPanelFileProps {
57 readonly projectId?: ProjectId | undefined;
58 readonly projectName: string | null;
59 readonly workspaceRoot: string | undefined;
60 readonly availableEditors: ReadonlyArray<EditorId>;
61 readonly pendingSurfaceIds: ReadonlySet<string>;
62 readonly onOpenFile: (relativePath: string) => void;
63 readonly onPendingChange: (relativePath: string, pending: boolean) => void;
64 }
65
66 export function RightPanelSurfaceContent(props: {
67 readonly surface: RightPanelSurface | null;
68 readonly activeSurfaceId: string | null;
69 readonly threadRef: ScopedThreadRef;
70 readonly environmentId: EnvironmentId;
71 readonly visible: boolean;
72 readonly composerDraftTarget: ScopedThreadRef | DraftId;
73 readonly keybindings: ResolvedKeybindingsConfig;
74 readonly configuredPreviewUrls: ReadonlyArray<string>;
75 readonly workspaceMutationId: string | null;
76 readonly getShortcutContext: () => ShortcutMatchContext;
77 readonly onSendAnnotation: (
78 annotation: PreviewAnnotationPayload,
79 image: ComposerImageAttachment | null,
80 ) => void;
81 readonly onCloseSurface: (surface: RightPanelSurface) => void;
82 readonly terminal: RightPanelTerminalProps;
83 readonly pullRequest: RightPanelPullRequestProps;
84 readonly issuesSupported: boolean;
85 readonly files: RightPanelFileProps;
86 readonly sideChatParentPanel: SideChatParentPanel;
87 }) {
88 const { surface, threadRef, visible, onCloseSurface } = props;
89 const gt = useGT();
90 if (surface === null) return null;
91 switch (surface.kind) {
92 case "preview":
93 return (
94 <Suspense fallback={null}>
95 <PreviewPanel
96 mode="embedded"
97 threadRef={threadRef}
98 tabId={surface.resourceId}
99 configuredUrls={props.configuredPreviewUrls}
100 visible={visible}
101 onSendAnnotation={props.onSendAnnotation}
102 />
103 </Suspense>
104 );
105 case "terminal":
106 return (
107 <PersistentThreadTerminalPanel
108 {...props.terminal}
109 visible={visible}
110 threadRef={threadRef}
111 surface={surface}
112 />
113 );
114 case "diff":
115 return (
116 <Suspense fallback={null}>
117 <DiffPanel
118 key={scopedThreadKey(threadRef)}
119 mode="embedded"
120 composerDraftTarget={props.composerDraftTarget}
121 workspaceMutationId={props.workspaceMutationId}
122 visible={visible}
123 />
124 </Suspense>
125 );
126 case "pull-request": {
127 const pullRequest = props.pullRequest;
128 if (!pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
129 if (!pullRequest.supported) {
130 return (
131 <PullRequestsUnavailableState
132 title={gt("Pull requests unavailable")}
133 error={gt("Update this environment's T3 Code server to browse pull requests.")}
134 />
135 );
136 }
137 return (
138 <PullRequestDetailPanel
139 getShortcutContext={props.getShortcutContext}
140 shortcutsEnabled={visible && props.activeSurfaceId === surface.id}
141 key={`${scopedThreadKey(threadRef)}:${surface.id}`}
142 environmentId={props.environmentId}
143 onSelectPullRequest={(reference) => {
144 useRightPanelStore.getState().openPullRequest(threadRef, {
145 projectId: reference.projectId,
146 repository: reference.repository,
147 number: reference.number,
148 ...(reference.host ? { host: reference.host } : {}),
149 });
150 }}
151 threadRef={threadRef}
152 reference={{
153 projectId: surface.projectId as ProjectId,
154 ...(surface.host ? { host: surface.host } : {}),
155 repository: surface.repository,
156 number: surface.number,
157 }}
158 initialTab={surface.detailTab ?? "summary"}
159 revealUrl={surface.revealUrl}
160 onRevealSettled={(revealUrl) => {
161 useRightPanelStore.getState().settlePullRequestReveal(threadRef, surface.id, revealUrl);
162 }}
163 onTabChange={(tab) => {
164 useRightPanelStore.getState().setPullRequestDetailTab(threadRef, surface.id, tab);
165 }}
166 context={pullRequestPanelContext(
167 {
168 projectId: pullRequest.projectId,
169 pullRequests: pullRequest.pullRequests,
170 linkedPullRequest: pullRequest.linkedPullRequest,
171 branchPullRequest: pullRequest.branchPullRequest,
172 },
173 surface,
174 )}
175 composerDraftTarget={props.composerDraftTarget}
176 onBack={pullRequest.onBack}
177 />
178 );
179 }
180 case "issue":
181 if (!props.pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
182 if (!props.issuesSupported) {
183 return (
184 <PullRequestsUnavailableState
185 title={gt("Issues unavailable")}
186 error={gt("Update this environment's T3 Code server to browse issues.")}
187 />
188 );
189 }
190 return (
191 <IssueDetailPanel
192 key={`${surface.environmentId ?? props.environmentId}:${surface.id}`}
193 environmentId={
194 (surface.environmentId as EnvironmentId | undefined) ?? props.environmentId
195 }
196 reference={{
197 projectId: surface.projectId as ProjectId,
198 tracker: surface.tracker as IssueTrackerKind,
199 ...(surface.host ? { host: surface.host } : {}),
200 scope: surface.scope,
201 key: surface.key,
202 }}
203 threadRef={threadRef}
204 onSelectPullRequest={(reference) => {
205 useRightPanelStore.getState().openPullRequest(threadRef, {
206 projectId: reference.projectId,
207 repository: reference.repository,
208 number: reference.number,
209 ...(reference.host ? { host: reference.host } : {}),
210 });
211 }}
212 />
213 );
214 case "pull-requests":
215 return <ThreadPullRequestsPanel threadRef={threadRef} />;
216 case "thread":
217 // Not lazy: revealing a suspended tiptap editor tears down its view and crashes the composer.
218 return (
219 <SideThreadPane
220 key={surface.id}
221 parentRef={threadRef}
222 surface={surface}
223 parentPanel={props.sideChatParentPanel}
224 onClose={() => onCloseSurface(surface)}
225 />
226 );
227 case "linked-thread":
228 return (
229 <LinkedThreadPane
230 key={surface.id}
231 panelRef={threadRef}
232 surface={surface}
233 visible={visible && props.activeSurfaceId === surface.id}
234 />
235 );
236 case "device":
237 return (
238 <Suspense fallback={null}>
239 <DevicePanel
240 mode="embedded"
241 threadRef={threadRef}
242 key={surface.id}
243 surface={surface}
244 visible={visible}
245 onDismissSetup={() => {
246 props.onCloseSurface(surface);
247 useRightPanelStore.getState().show(threadRef);
248 }}
249 />
250 </Suspense>
251 );
252 case "files":
253 case "file": {
254 const files = props.files;
255 const attachment = surface.kind === "file" ? surface.attachment : undefined;
256 if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
257 return (
258 <Suspense fallback={null}>
259 <FilePreviewPanel
260 key={`${props.environmentId}:${
261 attachment ? `attachment:${attachment.id}` : files.workspaceRoot
262 }`}
263 environmentId={props.environmentId}
264 cwd={files.workspaceRoot ?? ""}
265 projectName={files.projectName ?? ""}
266 projectId={files.projectId}
267 threadRef={threadRef}
268 composerDraftTarget={props.composerDraftTarget}
269 keybindings={props.keybindings}
270 availableEditors={files.availableEditors}
271 openInShortcutEnabled={visible}
272 relativePath={surface.kind === "file" ? surface.relativePath : null}
273 {...(attachment ? { attachment } : {})}
274 revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
275 revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
276 onOpenFile={files.onOpenFile}
277 onNavigateFolder={(relativePath, folderTrail, keepSource) =>
278 useRightPanelStore
279 .getState()
280 .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
281 }
282 folderTrail={surface.folderTrail}
283 onPendingChange={files.onPendingChange}
284 selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
285 workspaceMutationId={props.workspaceMutationId}
286 />
287 </Suspense>
288 );
289 }
290 }
291 }