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

RightPanelSurfaceContent.tsx

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