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