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 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;
49}
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;
58}
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 />
115 </Suspense>
116 );
117 case "pull-request": {
118 const pullRequest = props.pullRequest;
119 if (!pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
120 if (!pullRequest.supported) {
121 return (
122 <PullRequestsUnavailableState
123 title="Pull requests unavailable"
124 error="Update this environment's T3 Code server to browse pull requests."
125 />
126 );
127 }
128 return (
129 <PullRequestDetailPanel
130 getShortcutContext={props.getShortcutContext}
131 shortcutsEnabled={visible && props.activeSurfaceId === surface.id}
132 key={`${surface.host ?? ""}:${surface.repository}#${surface.number}`}
133 environmentId={props.environmentId}
134 onSelectPullRequest={(reference) => {
135 useRightPanelStore.getState().openPullRequest(threadRef, {
136 projectId: reference.projectId,
137 repository: reference.repository,
138 number: reference.number,
139 ...(reference.host ? { host: reference.host } : {}),
140 });
141 }}
142 threadRef={threadRef}
143 reference={{
144 projectId: surface.projectId as ProjectId,
145 ...(surface.host ? { host: surface.host } : {}),
146 repository: surface.repository,
147 number: surface.number,
148 }}
149 context={pullRequestPanelContext(
150 {
151 projectId: pullRequest.projectId,
152 pullRequests: pullRequest.pullRequests,
153 linkedPullRequest: pullRequest.linkedPullRequest,
154 branchPullRequest: pullRequest.branchPullRequest,
155 },
156 surface,
157 )}
158 composerDraftTarget={props.composerDraftTarget}
159 onBack={pullRequest.onBack}
160 />
161 );
162 }
163 case "pull-requests":
164 return <ThreadPullRequestsPanel threadRef={threadRef} />;
165 case "device":
166 return (
167 <Suspense fallback={null}>
168 <DevicePanel
169 mode="embedded"
170 threadRef={threadRef}
171 key={surface.id}
172 surface={surface}
173 visible={visible}
174 onDismissSetup={() => {
175 props.onCloseSurface(surface);
176 useRightPanelStore.getState().show(threadRef);
177 }}
178 />
179 </Suspense>
180 );
181 case "files":
182 case "file": {
183 const files = props.files;
184 const attachment = surface.kind === "file" ? surface.attachment : undefined;
185 if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
186 return (
187 <Suspense fallback={null}>
188 <FilePreviewPanel
189 key={`${props.environmentId}:${
190 attachment ? `attachment:${attachment.id}` : files.workspaceRoot
191 }`}
192 environmentId={props.environmentId}
193 cwd={files.workspaceRoot ?? ""}
194 projectName={files.projectName ?? ""}
195 threadRef={threadRef}
196 composerDraftTarget={props.composerDraftTarget}
197 keybindings={props.keybindings}
198 availableEditors={files.availableEditors}
199 relativePath={surface.kind === "file" ? surface.relativePath : null}
200 {...(attachment ? { attachment } : {})}
201 revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
202 revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
203 onOpenFile={files.onOpenFile}
204 onNavigateFolder={(relativePath, folderTrail, keepSource) =>
205 useRightPanelStore
206 .getState()
207 .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
208 }
209 folderTrail={surface.folderTrail}
210 onPendingChange={files.onPendingChange}
211 selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
212 workspaceMutationId={props.workspaceMutationId}
213 />
214 </Suspense>
215 );
216 }
217 }
218}