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