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

RightPanelSurfaceContent.tsx

238 lines8.7 KB

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 { type ComponentProps, lazy, Suspense } from "react";
13import type { ComposerImageAttachment, DraftId } from "../../composerDraftStore";
14import type { ShortcutMatchContext } from "../../keybindings";
15import { type RightPanelSurface, useRightPanelStore } from "../../rightPanelStore";
16import {
17 PersistentThreadTerminalPanel,
18 type PersistentThreadTerminalPanelProps,
19} from "../PersistentThreadTerminals";
20import { pullRequestPanelContext } from "../pullRequest/pullRequestDetail.logic";
21import { PullRequestDetailGhost } from "../pullRequest/PullRequestGhosts";
22import { PullRequestsUnavailableState } from "../pullRequest/PullRequestsUnavailableState";
23​
24const LazyPullRequestDetailPanel = lazy(() =>
25 import("../pullRequest/PullRequestDetailPanel").then((module) => ({
26 default: module.PullRequestDetailPanel,
27 })),
28);
29function PullRequestDetailPanel(props: ComponentProps<typeof LazyPullRequestDetailPanel>) {
30 return (
31 <Suspense fallback={<PullRequestDetailGhost />}>
32 <LazyPullRequestDetailPanel {...props} />
33 </Suspense>
34 );
36const ThreadPullRequestsPanel = lazy(() =>
37 import("../pullRequest/ThreadPullRequestsPanel").then((module) => ({
38 default: module.ThreadPullRequestsPanel,
39 })),
40);
41const PreviewPanel = lazy(() =>
42 import("../preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
43);
44const DiffPanel = lazy(() => import("../DiffPanel"));
45const DevicePanel = lazy(() =>
46 import("../device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
47);
48const FilePreviewPanel = lazy(() => import("../files/FilePreviewPanel"));
49​
50export type RightPanelTerminalProps = Omit<
51 PersistentThreadTerminalPanelProps,
52 "visible" | "threadRef" | "surface"
53>;
54​
55export interface RightPanelPullRequestProps {
56 readonly capabilityKnown: boolean;
57 readonly supported: boolean;
58 readonly projectId: ProjectId | null;
59 readonly pullRequests: ReadonlyArray<ThreadPullRequestLink>;
60 readonly linkedPullRequest: ThreadLinkedPullRequest | null;
61 readonly branchPullRequest: ThreadLinkedPullRequest | null | undefined;
62 /** Back to the pull request list; absent when there is no list to go back to. */
63 readonly onBack: (() => void) | undefined;
65​
66export interface RightPanelFileProps {
67 readonly projectName: string | null;
68 readonly workspaceRoot: string | undefined;
69 readonly availableEditors: ReadonlyArray<EditorId>;
70 readonly pendingSurfaceIds: ReadonlySet<string>;
71 readonly onOpenFile: (relativePath: string) => void;
72 readonly onPendingChange: (relativePath: string, pending: boolean) => void;
74​
75/** The body of the thread's right panel for the surface it is showing. */
76export function RightPanelSurfaceContent(props: {
77 readonly surface: RightPanelSurface | null;
78 readonly activeSurfaceId: string | null;
79 readonly threadRef: ScopedThreadRef;
80 readonly environmentId: EnvironmentId;
81 readonly visible: boolean;
82 readonly composerDraftTarget: ScopedThreadRef | DraftId;
83 readonly keybindings: ResolvedKeybindingsConfig;
84 readonly configuredPreviewUrls: ReadonlyArray<string>;
85 readonly workspaceMutationId: string | null;
86 readonly getShortcutContext: () => ShortcutMatchContext;
87 readonly onSendAnnotation: (
88 annotation: PreviewAnnotationPayload,
89 image: ComposerImageAttachment | null,
90 ) => void;
91 readonly onCloseSurface: (surface: RightPanelSurface) => void;
92 readonly terminal: RightPanelTerminalProps;
93 readonly pullRequest: RightPanelPullRequestProps;
94 readonly files: RightPanelFileProps;
95}) {
96 const { surface, threadRef, visible } = props;
97 if (surface === null) return null;
98 switch (surface.kind) {
99 case "preview":
100 return (
101 <Suspense fallback={null}>
102 <PreviewPanel
103 mode="embedded"
104 threadRef={threadRef}
105 tabId={surface.resourceId}
106 configuredUrls={props.configuredPreviewUrls}
107 visible={visible}
108 onSendAnnotation={props.onSendAnnotation}
109 />
110 </Suspense>
111 );
112 case "terminal":
113 return (
114 <PersistentThreadTerminalPanel
115 {...props.terminal}
116 visible={visible}
117 threadRef={threadRef}
118 surface={surface}
119 />
120 );
121 case "diff":
122 return (
123 <Suspense fallback={null}>
124 <DiffPanel
125 key={scopedThreadKey(threadRef)}
126 mode="embedded"
127 composerDraftTarget={props.composerDraftTarget}
128 workspaceMutationId={props.workspaceMutationId}
129 threadRef={threadRef}
130 />
131 </Suspense>
132 );
133 case "pull-request": {
134 const pullRequest = props.pullRequest;
135 if (!pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
136 if (!pullRequest.supported) {
137 return (
138 <PullRequestsUnavailableState
139 title="Pull requests unavailable"
140 error="Update this environment's T3 Code server to browse pull requests."
141 />
142 );
143 }
144 return (
145 <PullRequestDetailPanel
146 getShortcutContext={props.getShortcutContext}
147 shortcutsEnabled={visible && props.activeSurfaceId === surface.id}
148 key={`${surface.host ?? ""}:${surface.repository}#${surface.number}`}
149 environmentId={props.environmentId}
150 onSelectPullRequest={(reference) => {
151 useRightPanelStore.getState().openPullRequest(threadRef, {
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 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 "pull-requests":
180 return (
181 <Suspense fallback={null}>
182 <ThreadPullRequestsPanel threadRef={threadRef} />
183 </Suspense>
184 );
185 case "device":
186 return (
187 <Suspense fallback={null}>
188 <DevicePanel
189 mode="embedded"
190 threadRef={threadRef}
191 key={surface.id}
192 surface={surface}
193 visible={visible}
194 onDismissSetup={() => {
195 props.onCloseSurface(surface);
196 useRightPanelStore.getState().show(threadRef);
197 }}
198 />
199 </Suspense>
200 );
201 case "files":
202 case "file": {
203 const files = props.files;
204 const attachment = surface.kind === "file" ? surface.attachment : undefined;
205 if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
206 return (
207 <Suspense fallback={null}>
208 <FilePreviewPanel
209 key={`${props.environmentId}:${
210 attachment ? `attachment:${attachment.id}` : files.workspaceRoot
211 }`}
212 environmentId={props.environmentId}
213 cwd={files.workspaceRoot ?? ""}
214 projectName={files.projectName ?? ""}
215 threadRef={threadRef}
216 composerDraftTarget={props.composerDraftTarget}
217 keybindings={props.keybindings}
218 availableEditors={files.availableEditors}
219 relativePath={surface.kind === "file" ? surface.relativePath : null}
220 {...(attachment ? { attachment } : {})}
221 revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
222 revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
223 onOpenFile={files.onOpenFile}
224 onNavigateFolder={(relativePath, folderTrail, keepSource) =>
225 useRightPanelStore
226 .getState()
227 .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
228 }
229 folderTrail={surface.folderTrail}
230 onPendingChange={files.onPendingChange}
231 selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
232 workspaceMutationId={props.workspaceMutationId}
233 />
234 </Suspense>
235 );
236 }
237 }