Open History to see the last commit
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;
54}
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;
64}
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={props.environmentId}
145 onSelectPullRequest={(reference) => {
146 useRightPanelStore.getState().openPullRequest(threadRef, {
147 projectId: reference.projectId,
148 repository: reference.repository,
149 number: reference.number,
150 ...(reference.host ? { host: reference.host } : {}),
151 });
152 }}
153 threadRef={threadRef}
154 reference={{
155 projectId: surface.projectId as ProjectId,
156 ...(surface.host ? { host: surface.host } : {}),
157 repository: surface.repository,
158 number: surface.number,
159 }}
160 initialTab={surface.detailTab ?? "summary"}
161 revealUrl={surface.revealUrl}
162 onRevealSettled={(revealUrl) => {
163 useRightPanelStore.getState().settlePullRequestReveal(threadRef, surface.id, revealUrl);
164 }}
165 onTabChange={(tab) => {
166 useRightPanelStore.getState().setPullRequestDetailTab(threadRef, surface.id, tab);
167 }}
168 context={pullRequestPanelContext(
169 {
170 projectId: pullRequest.projectId,
171 pullRequests: pullRequest.pullRequests,
172 linkedPullRequest: pullRequest.linkedPullRequest,
173 branchPullRequest: pullRequest.branchPullRequest,
174 },
175 surface,
176 )}
177 composerDraftTarget={props.composerDraftTarget}
178 onBack={pullRequest.onBack}
179 />
180 );
181 }
182 case "issue":
183 if (!props.pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
184 if (!props.issuesSupported) {
185 return (
186 <PullRequestsUnavailableState
187 title={gt("Issues unavailable")}
188 error={gt("Update this environment's T3 Code server to browse issues.")}
189 />
190 );
191 }
192 return (
193 <IssueDetailPanel
194 key={`${surface.environmentId ?? props.environmentId}:${surface.id}`}
195 environmentId={
196 (surface.environmentId as EnvironmentId | undefined) ?? props.environmentId
197 }
198 reference={{
199 projectId: surface.projectId as ProjectId,
200 tracker: surface.tracker as IssueTrackerKind,
201 ...(surface.host ? { host: surface.host } : {}),
202 scope: surface.scope,
203 key: surface.key,
204 }}
205 threadRef={threadRef}
206 onSelectPullRequest={(reference) => {
207 useRightPanelStore.getState().openPullRequest(threadRef, {
208 projectId: reference.projectId,
209 repository: reference.repository,
210 number: reference.number,
211 ...(reference.host ? { host: reference.host } : {}),
212 });
213 }}
214 />
215 );
216 case "pull-requests":
217 return <ThreadPullRequestsPanel threadRef={threadRef} />;
218 case "thread":
219 // Not lazy: revealing a suspended tiptap editor tears down its view and crashes the composer.
220 return (
221 <SideThreadPane
222 key={surface.id}
223 parentRef={threadRef}
224 surface={surface}
225 parentPanel={props.sideChatParentPanel}
226 onClose={() => onCloseSurface(surface)}
227 />
228 );
229 case "linked-thread":
230 return (
231 <LinkedThreadPane
232 key={surface.id}
233 panelRef={threadRef}
234 surface={surface}
235 visible={visible && props.activeSurfaceId === surface.id}
236 />
237 );
238 case "device":
239 return (
240 <Suspense fallback={null}>
241 <DevicePanel
242 mode="embedded"
243 threadRef={threadRef}
244 key={surface.id}
245 surface={surface}
246 visible={visible}
247 onDismissSetup={() => {
248 props.onCloseSurface(surface);
249 useRightPanelStore.getState().show(threadRef);
250 }}
251 />
252 </Suspense>
253 );
254 case "files":
255 case "file": {
256 const files = props.files;
257 const attachment = surface.kind === "file" ? surface.attachment : undefined;
258 if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
259 return (
260 <Suspense fallback={null}>
261 <FilePreviewPanel
262 key={`${props.environmentId}:${
263 attachment ? `attachment:${attachment.id}` : files.workspaceRoot
264 }`}
265 environmentId={props.environmentId}
266 cwd={files.workspaceRoot ?? ""}
267 projectName={files.projectName ?? ""}
268 projectId={files.projectId}
269 threadRef={threadRef}
270 composerDraftTarget={props.composerDraftTarget}
271 keybindings={props.keybindings}
272 availableEditors={files.availableEditors}
273 openInShortcutEnabled={visible}
274 relativePath={surface.kind === "file" ? surface.relativePath : null}
275 {...(attachment ? { attachment } : {})}
276 revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
277 revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
278 onOpenFile={files.onOpenFile}
279 onNavigateFolder={(relativePath, folderTrail, keepSource) =>
280 useRightPanelStore
281 .getState()
282 .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
283 }
284 folderTrail={surface.folderTrail}
285 onPendingChange={files.onPendingChange}
286 selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
287 workspaceMutationId={props.workspaceMutationId}
288 />
289 </Suspense>
290 );
291 }
292 }
293}