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

RightPanelSurfaceContent.tsx

310 lines11.6 KB

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