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
76/** Touch equivalent of the web context popover; snapshots remain readable offline. */
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 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}