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 projectName: string | null;
54 readonly workspaceRoot: string | undefined;
55 readonly availableEditors: ReadonlyArray<EditorId>;
56 readonly pendingSurfaceIds: ReadonlySet<string>;
57 readonly onOpenFile: (relativePath: string) => void;
58 readonly onPendingChange: (relativePath: string, pending: boolean) => void;
59}
60
61export function RightPanelSurfaceContent(props: {
62 readonly surface: RightPanelSurface | null;
63 readonly activeSurfaceId: string | null;
64 readonly threadRef: ScopedThreadRef;
65 readonly environmentId: EnvironmentId;
66 readonly visible: boolean;
67 readonly composerDraftTarget: ScopedThreadRef | DraftId;
68 readonly keybindings: ResolvedKeybindingsConfig;
69 readonly configuredPreviewUrls: ReadonlyArray<string>;
70 readonly workspaceMutationId: string | null;
71 readonly getShortcutContext: () => ShortcutMatchContext;
72 readonly onSendAnnotation: (
73 annotation: PreviewAnnotationPayload,
74 image: ComposerImageAttachment | null,
75 ) => void;
76 readonly onCloseSurface: (surface: RightPanelSurface) => void;
77 readonly terminal: RightPanelTerminalProps;
78 readonly pullRequest: RightPanelPullRequestProps;
79 readonly files: RightPanelFileProps;
80 readonly sideChatParentPanel: SideChatParentPanel;
81}) {
82 const { surface, threadRef, visible } = props;
83 if (surface === null) return null;
84 switch (surface.kind) {
85 case "preview":
86 return (
87 <Suspense fallback={null}>
88 <PreviewPanel
89 mode="embedded"
90 threadRef={threadRef}
91 tabId={surface.resourceId}
92 configuredUrls={props.configuredPreviewUrls}
93 visible={visible}
94 onSendAnnotation={props.onSendAnnotation}
95 />
96 </Suspense>
97 );
98 case "terminal":
99 return (
100 <PersistentThreadTerminalPanel
101 {...props.terminal}
102 visible={visible}
103 threadRef={threadRef}
104 surface={surface}
105 />
106 );
107 case "diff":
108 return (
109 <Suspense fallback={null}>
110 <DiffPanel
111 key={scopedThreadKey(threadRef)}
112 mode="embedded"
113 composerDraftTarget={props.composerDraftTarget}
114 workspaceMutationId={props.workspaceMutationId}
115 visible={visible}
116 />
117 </Suspense>
118 );
119 case "pull-request": {
120 const pullRequest = props.pullRequest;
121 if (!pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
122 if (!pullRequest.supported) {
123 return (
124 <PullRequestsUnavailableState
125 title="Pull requests unavailable"
126 error="Update this environment's T3 Code server to browse pull requests."
127 />
128 );
129 }
130 return (
131 <PullRequestDetailPanel
132 getShortcutContext={props.getShortcutContext}
133 shortcutsEnabled={visible && props.activeSurfaceId === surface.id}
134 key={`${scopedThreadKey(threadRef)}:${surface.id}`}
135 environmentId={props.environmentId}
136 onSelectPullRequest={(reference) => {
137 useRightPanelStore.getState().openPullRequest(threadRef, {
138 projectId: reference.projectId,
139 repository: reference.repository,
140 number: reference.number,
141 ...(reference.host ? { host: reference.host } : {}),
142 });
143 }}
144 threadRef={threadRef}
145 reference={{
146 projectId: surface.projectId as ProjectId,
147 ...(surface.host ? { host: surface.host } : {}),
148 repository: surface.repository,
149 number: surface.number,
150 }}
151 initialTab={surface.detailTab ?? "summary"}
152 onTabChange={(tab) => {
153 useRightPanelStore.getState().setPullRequestDetailTab(threadRef, surface.id, tab);
154 }}
155 context={pullRequestPanelContext(
156 {
157 projectId: pullRequest.projectId,
158 pullRequests: pullRequest.pullRequests,
159 linkedPullRequest: pullRequest.linkedPullRequest,
160 branchPullRequest: pullRequest.branchPullRequest,
161 },
162 surface,
163 )}
164 composerDraftTarget={props.composerDraftTarget}
165 onBack={pullRequest.onBack}
166 />
167 );
168 }
169 case "pull-requests":
170 return <ThreadPullRequestsPanel threadRef={threadRef} />;
171 case "side-chat":
172 // Not lazy: revealing a suspended tiptap editor tears down its view and crashes the composer.
173 return (
174 <SideChatPane
175 key={surface.id}
176 parentRef={threadRef}
177 threadId={surface.threadId}
178 parentPanel={props.sideChatParentPanel}
179 onClose={() => props.onCloseSurface(surface)}
180 />
181 );
182 case "device":
183 return (
184 <Suspense fallback={null}>
185 <DevicePanel
186 mode="embedded"
187 threadRef={threadRef}
188 key={surface.id}
189 surface={surface}
190 visible={visible}
191 onDismissSetup={() => {
192 props.onCloseSurface(surface);
193 useRightPanelStore.getState().show(threadRef);
194 }}
195 />
196 </Suspense>
197 );
198 case "files":
199 case "file": {
200 const files = props.files;
201 const attachment = surface.kind === "file" ? surface.attachment : undefined;
202 if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
203 return (
204 <Suspense fallback={null}>
205 <FilePreviewPanel
206 key={`${props.environmentId}:${
207 attachment ? `attachment:${attachment.id}` : files.workspaceRoot
208 }`}
209 environmentId={props.environmentId}
210 cwd={files.workspaceRoot ?? ""}
211 projectName={files.projectName ?? ""}
212 threadRef={threadRef}
213 composerDraftTarget={props.composerDraftTarget}
214 keybindings={props.keybindings}
215 availableEditors={files.availableEditors}
216 openInShortcutEnabled={visible}
217 relativePath={surface.kind === "file" ? surface.relativePath : null}
218 {...(attachment ? { attachment } : {})}
219 revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
220 revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
221 onOpenFile={files.onOpenFile}
222 onNavigateFolder={(relativePath, folderTrail, keepSource) =>
223 useRightPanelStore
224 .getState()
225 .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
226 }
227 folderTrail={surface.folderTrail}
228 onPendingChange={files.onPendingChange}
229 selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
230 workspaceMutationId={props.workspaceMutationId}
231 />
232 </Suspense>
233 );
234 }
235 }
236}