Open History to see the last commit
1import { SourceFileSurface } from "../features/files/SourceFileSurface";
2import { filePreviewKind } from "@t3tools/shared/filePreview";
3import type {
4 ComposerContextRecord,
5 ElementContextSource,
6 EnvironmentId,
7 ScopedThreadRef,
8} from "@t3tools/contracts";
9import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
10import { videoMimeType } from "@t3tools/shared/video";
11import { useState } from "react";
12import {
13 Alert,
14 Linking,
15 Modal,
16 Platform,
17 Pressable,
18 ScrollView,
19 useWindowDimensions,
20 View,
21} from "react-native";
22import { useSafeAreaInsets } from "react-native-safe-area-context";
23import { REVIEW_MONO_FONT_FAMILY } from "../features/review/reviewDiffRendering";
24import { ReviewCommentCard, useReviewCommentColors } from "../features/review/ReviewCommentCard";
25import {
26 composerAttachmentInlineUri,
27 isFileBackedComposerAttachment,
28 type DraftComposerAttachment,
29} from "../lib/composerImages";
30import { FilePreviewModal } from "./FilePreviewModal";
31import { VideoPreviewModal } from "./VideoPreviewModal";
32import { ComposerContextAttachment } from "./ComposerContextAttachment";
33import { AppText as Text } from "./AppText";
34import { SymbolView } from "./AppSymbol";
35import { ContextSheetSize } from "./ContextSheetSize";
36import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
37import { getMobileTerminalTheme } from "../features/terminal/terminalTheme";
38
39function ContextField(props: { label: string; value: string | null | undefined; code?: boolean }) {
40 if (!props.value) return null;
41 return (
42 <View className="gap-1">
43 <Text className="text-xs text-foreground-muted">{props.label}</Text>
44 {props.code && (props.label === "HTML" || props.label === "Styles") ? (
45 <View
46 className="overflow-hidden rounded-xl border border-border"
47 style={{ height: Math.min(260, Math.max(100, props.value.split("\n").length * 22 + 36)) }}
48 >
49 <SourceFileSurface
50 embedded
51 contents={props.value}
52 path={props.label === "HTML" ? "element.html" : "styles.css"}
53 />
54 </View>
55 ) : (
56 <Text
57 selectable
58 className={props.code ? "text-sm text-foreground" : "text-base text-foreground"}
59 style={props.code ? { fontFamily: REVIEW_MONO_FONT_FAMILY } : undefined}
60 >
61 {props.value}
62 </Text>
63 )}
64 </View>
65 );
66}
67
68function ContextSource(props: { source: ElementContextSource | null }) {
69 const source = props.source;
70 if (!source) return null;
71 const location = source.fileName
72 ? `${source.fileName}${source.lineNumber !== null ? `:${source.lineNumber}${source.columnNumber !== null ? `:${source.columnNumber}` : ""}` : ""}`
73 : null;
74 return (
75 <ContextField
76 label="Source"
77 value={[source.functionName, location].filter(Boolean).join("\n")}
78 code
79 />
80 );
81}
82
83/** Touch equivalent of the web context popover; snapshots remain readable offline. */
84export function ComposerContextSheet(props: {
85 readonly label: string;
86 readonly record: ComposerContextRecord | undefined;
87 readonly onClose: () => void;
88 readonly onRemove?: () => void;
89 readonly onOpenAttachment?: () => void;
90 readonly onOpenPullRequest?: () => void;
91 readonly skillDescription?: string;
92 readonly onOpenSkill?: () => void;
93 readonly onOpenThread?: (thread: ScopedThreadRef) => void;
94 readonly environmentId?: EnvironmentId;
95 readonly records?: ReadonlyArray<ComposerContextRecord>;
96 readonly attachments?: ReadonlyArray<DraftComposerAttachment>;
97}) {
98 const reviewColors = useReviewCommentColors();
99 const insets = useSafeAreaInsets();
100 const { height: windowHeight } = useWindowDimensions();
101 const { themeId, themeAppearance } = useAppearancePreferences();
102 const terminalTheme = getMobileTerminalTheme(themeId, themeAppearance);
103 const [headerHeight, setHeaderHeight] = useState(0);
104 const [bodyHeight, setBodyHeight] = useState(0);
105 const measuredHeight = headerHeight + bodyHeight;
106 const record = props.record;
107 const localAttachment =
108 record && "attachmentId" in record
109 ? props.attachments?.find((entry) => entry.id === record.attachmentId)
110 : undefined;
111 const localFile =
112 localAttachment && isFileBackedComposerAttachment(localAttachment)
113 ? localAttachment
114 : undefined;
115 if (record && !("payload" in record) && (record.kind === "image" || record.kind === "file")) {
116 const mimeType = videoMimeType(record) ?? record.mimeType;
117 const previewKind = filePreviewKind(record);
118 const resource = {
119 _tag: "attachment" as const,
120 attachmentId: record.attachmentId,
121 fileName: record.name,
122 mimeType,
123 };
124 const remoteSource = props.environmentId
125 ? { environmentId: props.environmentId, resource }
126 : null;
127 if (videoMimeType(record)) {
128 if (localFile?.type === "file") {
129 return (
130 <VideoPreviewModal
131 source={{ type: "local", attachment: localFile }}
132 onRequestClose={props.onClose}
133 />
134 );
135 }
136 if (remoteSource && !localFile) {
137 return (
138 <VideoPreviewModal
139 source={{
140 type: "media",
141 name: record.name,
142 mimeType,
143 ...remoteSource,
144 actionsSource: { name: record.name, mimeType, ...remoteSource },
145 }}
146 onRequestClose={props.onClose}
147 />
148 );
149 }
150 } else if (record.kind === "image" || previewKind === "image" || previewKind === "pdf") {
151 const inlineUri = composerAttachmentInlineUri(localAttachment);
152 const source = localFile
153 ? { attachment: localFile }
154 : inlineUri
155 ? { uri: inlineUri }
156 : remoteSource;
157 if (source) {
158 return (
159 <FilePreviewModal
160 source={{
161 kind: record.kind === "image" || previewKind === "image" ? "image" : "pdf",
162 name: record.name,
163 ...source,
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 <Modal
187 animationType="slide"
188 presentationStyle={Platform.OS === "android" ? "overFullScreen" : "pageSheet"}
189 transparent={Platform.OS === "android"}
190 onRequestClose={props.onClose}
191 >
192 <View
193 className={
194 Platform.OS === "android" ? "flex-1 justify-end bg-backdrop" : "flex-1 justify-end"
195 }
196 >
197 {Platform.OS === "android" ? (
198 <Pressable
199 accessibilityRole="button"
200 accessibilityLabel="Dismiss context"
201 onPress={props.onClose}
202 style={{ position: "absolute", top: 0, right: 0, bottom: 0, left: 0 }}
203 />
204 ) : null}
205 <View
206 className="overflow-hidden rounded-t-3xl bg-sheet-solid"
207 style={
208 Platform.OS === "android"
209 ? {
210 height: Math.min(
211 measuredHeight || windowHeight * 0.5,
212 windowHeight - insets.top - 24,
213 ),
214 }
215 : { flex: 1 }
216 }
217 >
218 <ContextSheetSize height={measuredHeight} />
219 <View
220 onLayout={(event) => setHeaderHeight(event.nativeEvent.layout.height)}
221 className="flex-row items-center justify-between gap-3 border-b border-border px-4 pb-2 pt-4"
222 >
223 {terminal ? (
224 <SymbolView name="terminal" size={20} tintColor={terminalTheme.palette[2]} />
225 ) : null}
226 <View className="min-w-0 flex-1">
227 <Text className="text-base font-t3-semibold text-foreground" numberOfLines={2}>
228 {terminal?.terminalLabel ?? props.label}
229 </Text>
230 {terminal ? (
231 <Text className="text-xs text-foreground-muted">
232 Lines {terminal.lineStart}–{terminal.lineEnd}
233 </Text>
234 ) : null}
235 </View>
236 <Pressable
237 accessibilityRole="button"
238 accessibilityLabel="Close context"
239 onPress={props.onClose}
240 className="p-3"
241 >
242 <Text className="text-foreground">Done</Text>
243 </Pressable>
244 </View>
245 <ScrollView
246 style={{ flexShrink: 1 }}
247 onContentSizeChange={(_width, height) => setBodyHeight(height)}
248 contentContainerStyle={{
249 padding: 16,
250 gap: 16,
251 paddingBottom: Math.max(20, insets.bottom),
252 }}
253 >
254 {!record ? (
255 <Text className="text-foreground">
256 Context unavailable. The reference was copied without its payload. Copy it again
257 from the original message or remove it.
258 </Text>
259 ) : "payload" in record ? (
260 <Text className="text-foreground">
261 This context type is not supported by this version of the app. Its payload will be
262 preserved when sent.
263 </Text>
264 ) : (
265 <>
266 {record.kind === "terminal" ? (
267 <View
268 className="overflow-hidden rounded-xl border border-border"
269 style={{ backgroundColor: terminalTheme.background }}
270 >
271 <ScrollView
272 horizontal
273 showsHorizontalScrollIndicator
274 contentContainerStyle={{ padding: 12 }}
275 >
276 <Text
277 selectable
278 className="text-sm text-foreground"
279 style={{
280 fontFamily: REVIEW_MONO_FONT_FAMILY,
281 fontSize: 13,
282 lineHeight: 20,
283 }}
284 >
285 {record.text}
286 </Text>
287 </ScrollView>
288 </View>
289 ) : null}
290 {record.kind === "review-comment" ? (
291 <>
292 {record.pullRequest ? (
293 <ContextField
294 label={`#${record.pullRequest.number} · ${record.pullRequest.isDraft ? "draft" : record.pullRequest.state}`}
295 value={`${record.pullRequest.title}\n${record.pullRequest.headBranch} → ${record.pullRequest.baseBranch}`}
296 />
297 ) : null}
298 {!record.sectionId.startsWith("pull-request:") ? (
299 <>
300 <ReviewCommentCard
301 comment={{ ...record, id: record.contextId }}
302 colors={reviewColors}
303 />
304 </>
305 ) : null}
306 </>
307 ) : null}
308 {record.kind === "preview-annotation" ? (
309 <>
310 <ContextField label="Page" value={record.pageTitle ?? record.pageUrl} />
311 <ContextField label="URL" value={record.pageUrl} />
312 <ContextField label="Comment" value={record.comment} />
313 <ContextField label="Selection" value={record.targetSummary} />
314 <ContextField
315 label="Requested changes"
316 value={record.styleChanges.join("\n")}
317 />
318 {record.elements?.map((element, index) => (
319 <View
320 key={`${element.selector ?? element.tagName}:${index}`}
321 className="gap-3"
322 >
323 <ContextField
324 label="Element"
325 value={element.componentName ?? element.tagName}
326 />
327 <ContextField label="Selector" value={element.selector} code />
328 <ContextSource source={element.source} />
329 <ContextField label="HTML" value={element.htmlPreview} code />
330 <ContextField label="Styles" value={element.styles} code />
331 </View>
332 ))}
333 </>
334 ) : null}
335 {record.kind === "element" ? (
336 <>
337 <ContextField label="Page" value={record.pageUrl} />
338 <ContextField label="Element" value={record.componentName ?? record.tagName} />
339 <ContextField label="Selector" value={record.selector} code />
340 <ContextSource source={record.source} />
341 <ContextField label="HTML" value={record.htmlPreview} code />
342 <ContextField label="Styles" value={record.styles} code />
343 </>
344 ) : null}
345 {record.kind === "image" ? (
346 <ContextField
347 label="File"
348 value={`${record.mimeType} · ${formatAttachmentSize(record.sizeBytes)}`}
349 />
350 ) : null}
351 {record.kind === "mention" ? (
352 <ContextField label="Path" value={record.path} code />
353 ) : null}
354 {record.kind === "thread" ? (
355 <View className="gap-3">
356 <ContextField label="Thread" value={record.title} />
357 {props.onOpenThread ? (
358 <Pressable
359 accessibilityRole="button"
360 onPress={() =>
361 props.onOpenThread?.({
362 environmentId: record.environmentId,
363 threadId: record.threadId,
364 })
365 }
366 className="rounded-xl bg-subtle p-4"
367 >
368 <Text className="text-foreground">Open thread</Text>
369 </Pressable>
370 ) : null}
371 </View>
372 ) : null}
373 {record.kind === "skill" ? (
374 <View className="gap-3">
375 <ContextField label="Skill" value={record.name} />
376 <ContextField
377 label="Description"
378 value={
379 props.skillDescription ?? "No description is available for this skill."
380 }
381 />
382 {props.onOpenSkill ? (
383 <Pressable
384 accessibilityRole="button"
385 onPress={props.onOpenSkill}
386 className="rounded-xl bg-subtle p-4"
387 >
388 <Text className="text-foreground">View instructions</Text>
389 </Pressable>
390 ) : null}
391 </View>
392 ) : null}
393 </>
394 )}
395 {attachmentRecord && "attachmentId" in attachmentRecord ? (
396 <ComposerContextAttachment
397 key={JSON.stringify([
398 props.environmentId,
399 attachmentRecord.attachmentId,
400 props.attachments?.find((entry) => entry.id === attachmentRecord.attachmentId)
401 ?.fileUri,
402 ])}
403 record={attachmentRecord}
404 environmentId={props.environmentId}
405 attachment={props.attachments?.find(
406 (entry) => entry.id === attachmentRecord.attachmentId,
407 )}
408 />
409 ) : null}
410 {pullRequestUrl && /^https?:\/\//i.test(pullRequestUrl) ? (
411 <Pressable
412 accessibilityRole="link"
413 onPress={() => {
414 void Linking.openURL(pullRequestUrl).catch(() =>
415 Alert.alert("Could not open pull request", "Try again when connected."),
416 );
417 }}
418 className="rounded-xl bg-subtle p-4"
419 >
420 <Text className="text-foreground">Open pull request</Text>
421 </Pressable>
422 ) : null}
423 {props.onOpenAttachment ? (
424 <Pressable
425 accessibilityRole="button"
426 onPress={props.onOpenAttachment}
427 className="rounded-xl bg-subtle p-4"
428 >
429 <Text className="text-foreground">Open attachment</Text>
430 </Pressable>
431 ) : null}
432 {props.onOpenPullRequest ? (
433 <Pressable
434 accessibilityRole="button"
435 onPress={props.onOpenPullRequest}
436 className="rounded-xl bg-subtle p-4"
437 >
438 <Text className="text-foreground">Open pull request</Text>
439 </Pressable>
440 ) : null}
441 {props.onRemove ? (
442 <Pressable
443 accessibilityRole="button"
444 onPress={props.onRemove}
445 className="rounded-xl bg-subtle p-4"
446 >
447 <Text className="text-foreground">Remove from draft</Text>
448 </Pressable>
449 ) : null}
450 </ScrollView>
451 </View>
452 </View>
453 </Modal>
454 );
455}