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

ComposerContextSheet.tsx

387 lines14.6 KB

1import { Button } from "./ui/Button";
2import * as Dialog from "./ui/Dialog";
3import { useOpenPullRequest } from "../native/useOpenPullRequest";
4import { SourceFileSurface } from "../features/files/SourceFileSurface";
5import { filePreviewKind } from "@t3tools/shared/filePreview";
6import type {
7 ComposerContextRecord,
8 ElementContextSource,
9 EnvironmentId,
10 ProjectId,
11 ScopedThreadRef,
12} from "@t3tools/contracts";
13import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
14import { videoMimeType } from "@t3tools/shared/video";
15import { Platform, Pressable, ScrollView, View } from "react-native";
16import { useSafeAreaInsets } from "react-native-safe-area-context";
17import { REVIEW_MONO_FONT_FAMILY } from "../features/review/reviewDiffRendering";
18import { ReviewCommentCard, useReviewCommentColors } from "../features/review/ReviewCommentCard";
19import {
20 composerAttachmentInlineUri,
21 isFileBackedComposerAttachment,
22 type DraftComposerAttachment,
23} from "../lib/composerImages";
24import { contextImageGallery } from "../lib/contextImageGallery";
25import { FilePreviewModal } from "./FilePreviewModal";
26import { VideoPreviewModal } from "./VideoPreviewModal";
27import { ComposerContextAttachment } from "./ComposerContextAttachment";
28import { AppText as Text } from "./ui/AppText";
29import { Sheet } from "./ui/Sheet";
30import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
31import { getMobileTerminalTheme } from "../features/terminal/terminalTheme";
32​
33function ContextField(props: { label: string; value: string | null | undefined; code?: boolean }) {
34 if (!props.value) return null;
35 return (
36 <View className="gap-1">
37 <Text className="text-xs text-foreground-muted">{props.label}</Text>
38 {props.code && (props.label === "HTML" || props.label === "Styles") ? (
39 <View
40 className="overflow-hidden rounded-xl border border-border"
41 style={{ height: Math.min(260, Math.max(100, props.value.split("\n").length * 22 + 36)) }}
42 >
43 <SourceFileSurface
44 embedded
45 contents={props.value}
46 path={props.label === "HTML" ? "element.html" : "styles.css"}
47 />
48 </View>
49 ) : (
50 <Text
51 selectable
52 className={props.code ? "text-sm text-foreground" : "text-base text-foreground"}
53 style={props.code ? { fontFamily: REVIEW_MONO_FONT_FAMILY } : undefined}
54 >
55 {props.value}
56 </Text>
57 )}
58 </View>
59 );
61​
62function ContextSource(props: { source: ElementContextSource | null }) {
63 const source = props.source;
64 if (!source) return null;
65 const location = source.fileName
66 ? `${source.fileName}${source.lineNumber !== null ? `:${source.lineNumber}${source.columnNumber !== null ? `:${source.columnNumber}` : ""}` : ""}`
67 : null;
68 return (
69 <ContextField
70 label="Source"
71 value={[source.functionName, location].filter(Boolean).join("\n")}
72 code
73 />
74 );
76​
77export function ComposerContextSheet(props: {
78 readonly label: string;
79 readonly record: ComposerContextRecord | undefined;
80 readonly onClose: () => void;
81 readonly onRemove?: () => void;
82 readonly onOpenAttachment?: () => void;
83 readonly onOpenPullRequest?: () => void;
84 readonly skillDescription?: string;
85 readonly onOpenSkill?: () => void;
86 readonly onOpenThread?: (thread: ScopedThreadRef) => void;
87 readonly environmentId?: EnvironmentId;
88 readonly projectId?: ProjectId;
89 readonly records?: ReadonlyArray<ComposerContextRecord>;
90 readonly attachments?: ReadonlyArray<DraftComposerAttachment>;
91}) {
92 const openPullRequest = useOpenPullRequest({
93 context:
94 props.environmentId && props.projectId
95 ? { environmentId: props.environmentId, projectId: props.projectId }
96 : undefined,
97 });
98 const reviewColors = useReviewCommentColors();
99 const insets = useSafeAreaInsets();
100 const { themeId, themeAppearance } = useAppearancePreferences();
101 const terminalTheme = getMobileTerminalTheme(themeId, themeAppearance);
102 const record = props.record;
103 const localAttachment =
104 record && "attachmentId" in record
105 ? props.attachments?.find((entry) => entry.id === record.attachmentId)
106 : undefined;
107 const localFile =
108 localAttachment && isFileBackedComposerAttachment(localAttachment)
109 ? localAttachment
110 : undefined;
111 if (record && !("payload" in record) && (record.kind === "image" || record.kind === "file")) {
112 const mimeType = videoMimeType(record) ?? record.mimeType;
113 const previewKind = filePreviewKind(record);
114 const resource = {
115 _tag: "attachment" as const,
116 attachmentId: record.attachmentId,
117 fileName: record.name,
118 mimeType,
119 };
120 const remoteSource = props.environmentId
121 ? { environmentId: props.environmentId, resource }
122 : null;
123 if (videoMimeType(record)) {
124 if (localFile?.type === "file") {
125 return (
126 <VideoPreviewModal
127 source={{ type: "local", attachment: localFile }}
128 onRequestClose={props.onClose}
129 />
130 );
131 }
132 if (remoteSource && !localFile) {
133 return (
134 <VideoPreviewModal
135 source={{
136 type: "media",
137 name: record.name,
138 mimeType,
139 ...remoteSource,
140 actionsSource: { name: record.name, mimeType, ...remoteSource },
141 }}
142 onRequestClose={props.onClose}
143 />
144 );
145 }
146 } else if (record.kind === "image" || previewKind === "image" || previewKind === "pdf") {
147 const inlineUri = composerAttachmentInlineUri(localAttachment);
148 const source = localFile
149 ? { attachment: localFile }
150 : inlineUri
151 ? { uri: inlineUri }
152 : remoteSource;
153 if (source) {
154 return (
155 <FilePreviewModal
156 source={{
157 kind: record.kind === "image" || previewKind === "image" ? "image" : "pdf",
158 name: record.name,
159 ...source,
160 gallery: contextImageGallery(
161 props.records ?? [],
162 props.attachments ?? [],
163 props.environmentId,
164 ),
165 actionsSource: { name: record.name, mimeType, ...source },
166 }}
167 onRequestClose={props.onClose}
168 />
169 );
170 }
171 }
172 }
173 const attachmentRecord =
174 record && "attachmentId" in record
175 ? record
176 : record?.kind === "preview-annotation" && "screenshotContextId" in record
177 ? props.records?.find(
178 (entry) => entry.contextId === record.screenshotContextId && "attachmentId" in entry,
179 )
180 : undefined;
181 const pullRequestUrl =
182 record?.kind === "review-comment" && "pullRequest" in record
183 ? record.pullRequest?.url
184 : undefined;
185 const terminal = record?.kind === "terminal" && !("payload" in record) ? record : null;
186 return (
187 <Sheet title={terminal?.terminalLabel ?? props.label} onClose={props.onClose}>
188 <ScrollView
189 nestedScrollEnabled
190 className="flex-1"
191 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
192 contentContainerStyle={{
193 paddingHorizontal: 20,
194 paddingTop: 12,
195 gap: 16,
196 paddingBottom: Math.max(20, insets.bottom),
197 }}
198 >
199 {terminal ? (
200 <Text className="text-sm text-foreground-muted">
201 Lines {terminal.lineStart}–{terminal.lineEnd}
202 </Text>
203 ) : null}
204 {!record ? (
205 <Text className="text-foreground">
206 Context unavailable. The reference was copied without its payload. Copy it again from
207 the original message or remove it.
208 </Text>
209 ) : "payload" in record ? (
210 <Text className="text-foreground">
211 This context type is not supported by this version of the app. Its payload will be
212 preserved when sent.
213 </Text>
214 ) : (
215 <>
216 {record.kind === "terminal" ? (
217 <View
218 className="overflow-hidden rounded-xl"
219 style={{ backgroundColor: terminalTheme.background }}
220 >
221 <ScrollView
222 horizontal
223 showsHorizontalScrollIndicator
224 contentContainerStyle={{ padding: 12 }}
225 >
226 <Text
227 selectable
228 className="text-sm text-foreground"
229 style={{
230 fontFamily: REVIEW_MONO_FONT_FAMILY,
231 fontSize: 13,
232 lineHeight: 20,
233 color: terminalTheme.foreground,
234 }}
235 >
236 {record.text}
237 </Text>
238 </ScrollView>
239 </View>
240 ) : null}
241 {record.kind === "review-comment" ? (
242 <>
243 {record.pullRequest ? (
244 <ContextField
245 label={`#${record.pullRequest.number} · ${record.pullRequest.isDraft ? "draft" : record.pullRequest.state}`}
246 value={`${record.pullRequest.title}\n${record.pullRequest.headBranch} → ${record.pullRequest.baseBranch}`}
247 />
248 ) : null}
249 {!record.sectionId.startsWith("pull-request:") ? (
250 <>
251 <ReviewCommentCard
252 comment={{ ...record, id: record.contextId }}
253 colors={reviewColors}
254 />
255 </>
256 ) : null}
257 </>
258 ) : null}
259 {record.kind === "preview-annotation" ? (
260 <>
261 <ContextField label="Page" value={record.pageTitle ?? record.pageUrl} />
262 <ContextField label="URL" value={record.pageUrl} />
263 <ContextField label="Comment" value={record.comment} />
264 <ContextField label="Selection" value={record.targetSummary} />
265 <ContextField label="Requested changes" value={record.styleChanges.join("\n")} />
266 {record.elements?.map((element, index) => (
267 <View key={`${element.selector ?? element.tagName}:${index}`} className="gap-3">
268 <ContextField
269 label="Element"
270 value={element.componentName ?? element.tagName}
271 />
272 <ContextField label="Selector" value={element.selector} code />
273 <ContextSource source={element.source} />
274 <ContextField label="HTML" value={element.htmlPreview} code />
275 <ContextField label="Styles" value={element.styles} code />
276 </View>
277 ))}
278 </>
279 ) : null}
280 {record.kind === "element" ? (
281 <>
282 <ContextField label="Page" value={record.pageUrl} />
283 <ContextField label="Element" value={record.componentName ?? record.tagName} />
284 <ContextField label="Selector" value={record.selector} code />
285 <ContextSource source={record.source} />
286 <ContextField label="HTML" value={record.htmlPreview} code />
287 <ContextField label="Styles" value={record.styles} code />
288 </>
289 ) : null}
290 {record.kind === "image" ? (
291 <ContextField
292 label="File"
293 value={`${record.mimeType} · ${formatAttachmentSize(record.sizeBytes)}`}
294 />
295 ) : null}
296 {record.kind === "mention" ? (
297 <ContextField label="Path" value={record.path} code />
298 ) : null}
299 {record.kind === "thread" ? (
300 <View className="gap-3">
301 <ContextField label="Thread" value={record.title} />
302 {props.onOpenThread ? (
303 <Button
304 label="Open thread"
305 tone="secondary"
306 onPress={() =>
307 props.onOpenThread?.({
308 environmentId: record.environmentId,
309 threadId: record.threadId,
310 })
311 }
312 fullWidth
313 />
314 ) : null}
315 </View>
316 ) : null}
317 {record.kind === "skill" ? (
318 <View className="gap-3">
319 <ContextField label="Skill" value={record.name} />
320 <ContextField
321 label="Description"
322 value={props.skillDescription ?? "No description is available for this skill."}
323 />
324 {props.onOpenSkill ? (
325 <Button
326 label="View instructions"
327 tone="secondary"
328 onPress={props.onOpenSkill}
329 fullWidth
330 />
331 ) : null}
332 </View>
333 ) : null}
334 </>
335 )}
336 {attachmentRecord && "attachmentId" in attachmentRecord ? (
337 <ComposerContextAttachment
338 key={JSON.stringify([
339 props.environmentId,
340 attachmentRecord.attachmentId,
341 props.attachments?.find((entry) => entry.id === attachmentRecord.attachmentId)
342 ?.fileUri,
343 ])}
344 record={attachmentRecord}
345 environmentId={props.environmentId}
346 attachment={props.attachments?.find(
347 (entry) => entry.id === attachmentRecord.attachmentId,
348 )}
349 />
350 ) : null}
351 {pullRequestUrl && /^https?:\/\//i.test(pullRequestUrl) ? (
352 <Pressable
353 accessibilityRole="link"
354 onPress={() => {
355 props.onClose();
356 void openPullRequest(pullRequestUrl).catch(() =>
357 Dialog.alert("Could not open pull request", "Try again when connected."),
358 );
359 }}
360 className="rounded-xl bg-subtle p-4"
361 >
362 <Text className="text-foreground">Open pull request</Text>
363 </Pressable>
364 ) : null}
365 {props.onOpenAttachment ? (
366 <Button
367 label="Open attachment"
368 tone="secondary"
369 onPress={props.onOpenAttachment}
370 fullWidth
371 />
372 ) : null}
373 {props.onOpenPullRequest && !pullRequestUrl ? (
374 <Button
375 label="Open pull request"
376 tone="secondary"
377 onPress={props.onOpenPullRequest}
378 fullWidth
379 />
380 ) : null}
381 {props.onRemove ? (
382 <Button label="Remove from draft" tone="secondary" onPress={props.onRemove} fullWidth />
383 ) : null}
384 </ScrollView>
385 </Sheet>
386 );