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

ComposerContextSheet.tsx

454 lines17.7 KB

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