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