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