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

RightPanelSurfaceContent.tsx

224 lines8.5 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 { 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 { PullRequestDetailPanel } from "../pullRequest/PullRequestDetailPanel";
22import { PullRequestDetailGhost } from "../pullRequest/PullRequestGhosts";
23import { PullRequestsUnavailableState } from "../pullRequest/PullRequestsUnavailableState";
24import { ThreadPullRequestsPanel } from "../pullRequest/ThreadPullRequestsPanel";
25​
26const PreviewPanel = lazy(() =>
27 import("../preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
28);
29const DiffPanel = lazy(() => import("../DiffPanel"));
30const DevicePanel = lazy(() =>
31 import("../device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
32);
33const FilePreviewPanel = lazy(() => import("../files/FilePreviewPanel"));
34​
35export type RightPanelTerminalProps = Omit<
36 PersistentThreadTerminalPanelProps,
37 "visible" | "threadRef" | "surface"
38>;
39​
40export interface RightPanelPullRequestProps {
41 readonly capabilityKnown: boolean;
42 readonly supported: boolean;
43 readonly projectId: ProjectId | null;
44 readonly pullRequests: ReadonlyArray<ThreadPullRequestLink>;
45 readonly linkedPullRequest: ThreadLinkedPullRequest | null;
46 readonly branchPullRequest: ThreadLinkedPullRequest | null | undefined;
47 /** Back to the pull request list; absent when there is no list to go back to. */
48 readonly onBack: (() => void) | undefined;
50​
51export interface RightPanelFileProps {
52 readonly projectName: string | null;
53 readonly workspaceRoot: string | undefined;
54 readonly availableEditors: ReadonlyArray<EditorId>;
55 readonly pendingSurfaceIds: ReadonlySet<string>;
56 readonly onOpenFile: (relativePath: string) => void;
57 readonly onPendingChange: (relativePath: string, pending: boolean) => void;
59​
60/** The body of the thread's right panel for the surface it is showing. */
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}) {
81 const { surface, threadRef, visible } = props;
82 if (surface === null) return null;
83 switch (surface.kind) {
84 case "preview":
85 return (
86 <Suspense fallback={null}>
87 <PreviewPanel
88 mode="embedded"
89 threadRef={threadRef}
90 tabId={surface.resourceId}
91 configuredUrls={props.configuredPreviewUrls}
92 visible={visible}
93 onSendAnnotation={props.onSendAnnotation}
94 />
95 </Suspense>
96 );
97 case "terminal":
98 return (
99 <PersistentThreadTerminalPanel
100 {...props.terminal}
101 visible={visible}
102 threadRef={threadRef}
103 surface={surface}
104 />
105 );
106 case "diff":
107 return (
108 <Suspense fallback={null}>
109 <DiffPanel
110 key={scopedThreadKey(threadRef)}
111 mode="embedded"
112 composerDraftTarget={props.composerDraftTarget}
113 workspaceMutationId={props.workspaceMutationId}
114 visible={visible}
115 />
116 </Suspense>
117 );
118 case "pull-request": {
119 const pullRequest = props.pullRequest;
120 if (!pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
121 if (!pullRequest.supported) {
122 return (
123 <PullRequestsUnavailableState
124 title="Pull requests unavailable"
125 error="Update this environment's T3 Code server to browse pull requests."
126 />
127 );
128 }
129 return (
130 <PullRequestDetailPanel
131 getShortcutContext={props.getShortcutContext}
132 shortcutsEnabled={visible && props.activeSurfaceId === surface.id}
133 key={`${scopedThreadKey(threadRef)}:${surface.id}`}
134 environmentId={props.environmentId}
135 onSelectPullRequest={(reference) => {
136 useRightPanelStore.getState().openPullRequest(threadRef, {
137 projectId: reference.projectId,
138 repository: reference.repository,
139 number: reference.number,
140 ...(reference.host ? { host: reference.host } : {}),
141 });
142 }}
143 threadRef={threadRef}
144 reference={{
145 projectId: surface.projectId as ProjectId,
146 ...(surface.host ? { host: surface.host } : {}),
147 repository: surface.repository,
148 number: surface.number,
149 }}
150 initialTab={surface.detailTab ?? "summary"}
151 onTabChange={(tab) => {
152 useRightPanelStore.getState().setPullRequestDetailTab(threadRef, surface.id, tab);
153 }}
154 context={pullRequestPanelContext(
155 {
156 projectId: pullRequest.projectId,
157 pullRequests: pullRequest.pullRequests,
158 linkedPullRequest: pullRequest.linkedPullRequest,
159 branchPullRequest: pullRequest.branchPullRequest,
160 },
161 surface,
162 )}
163 composerDraftTarget={props.composerDraftTarget}
164 onBack={pullRequest.onBack}
165 />
166 );
167 }
168 case "pull-requests":
169 return <ThreadPullRequestsPanel threadRef={threadRef} />;
170 case "device":
171 return (
172 <Suspense fallback={null}>
173 <DevicePanel
174 mode="embedded"
175 threadRef={threadRef}
176 key={surface.id}
177 surface={surface}
178 visible={visible}
179 onDismissSetup={() => {
180 props.onCloseSurface(surface);
181 useRightPanelStore.getState().show(threadRef);
182 }}
183 />
184 </Suspense>
185 );
186 case "files":
187 case "file": {
188 const files = props.files;
189 const attachment = surface.kind === "file" ? surface.attachment : undefined;
190 if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
191 return (
192 <Suspense fallback={null}>
193 <FilePreviewPanel
194 key={`${props.environmentId}:${
195 attachment ? `attachment:${attachment.id}` : files.workspaceRoot
196 }`}
197 environmentId={props.environmentId}
198 cwd={files.workspaceRoot ?? ""}
199 projectName={files.projectName ?? ""}
200 threadRef={threadRef}
201 composerDraftTarget={props.composerDraftTarget}
202 keybindings={props.keybindings}
203 availableEditors={files.availableEditors}
204 openInShortcutEnabled={visible}
205 relativePath={surface.kind === "file" ? surface.relativePath : null}
206 {...(attachment ? { attachment } : {})}
207 revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
208 revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
209 onOpenFile={files.onOpenFile}
210 onNavigateFolder={(relativePath, folderTrail, keepSource) =>
211 useRightPanelStore
212 .getState()
213 .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
214 }
215 folderTrail={surface.folderTrail}
216 onPendingChange={files.onPendingChange}
217 selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
218 workspaceMutationId={props.workspaceMutationId}
219 />
220 </Suspense>
221 );
222 }
223 }