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

ComposerContextSheet.tsx

435 lines16.8 KB

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 );
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 );
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 style={{
191 flex: 1,
192 justifyContent: "flex-end",
193 backgroundColor: Platform.OS === "android" ? "#00000066" : undefined,
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 === "skill" ? (
354 <View className="gap-3">
355 <ContextField label="Skill" value={record.name} />
356 <ContextField
357 label="Description"
358 value={
359 props.skillDescription ?? "No description is available for this skill."
360 }
361 />
362 {props.onOpenSkill ? (
363 <Pressable
364 accessibilityRole="button"
365 onPress={props.onOpenSkill}
366 className="rounded-xl bg-subtle p-4"
367 >
368 <Text className="text-foreground">View instructions</Text>
369 </Pressable>
370 ) : null}
371 </View>
372 ) : null}
373 </>
374 )}
375 {attachmentRecord && "attachmentId" in attachmentRecord ? (
376 <ComposerContextAttachment
377 key={JSON.stringify([
378 props.environmentId,
379 attachmentRecord.attachmentId,
380 props.attachments?.find((entry) => entry.id === attachmentRecord.attachmentId)
381 ?.fileUri,
382 ])}
383 record={attachmentRecord}
384 environmentId={props.environmentId}
385 attachment={props.attachments?.find(
386 (entry) => entry.id === attachmentRecord.attachmentId,
387 )}
388 />
389 ) : null}
390 {pullRequestUrl && /^https?:\/\//i.test(pullRequestUrl) ? (
391 <Pressable
392 accessibilityRole="link"
393 onPress={() => {
394 void Linking.openURL(pullRequestUrl).catch(() =>
395 Alert.alert("Could not open pull request", "Try again when connected."),
396 );
397 }}
398 className="rounded-xl bg-subtle p-4"
399 >
400 <Text className="text-foreground">Open pull request</Text>
401 </Pressable>
402 ) : null}
403 {props.onOpenAttachment ? (
404 <Pressable
405 accessibilityRole="button"
406 onPress={props.onOpenAttachment}
407 className="rounded-xl bg-subtle p-4"
408 >
409 <Text className="text-foreground">Open attachment</Text>
410 </Pressable>
411 ) : null}
412 {props.onOpenPullRequest ? (
413 <Pressable
414 accessibilityRole="button"
415 onPress={props.onOpenPullRequest}
416 className="rounded-xl bg-subtle p-4"
417 >
418 <Text className="text-foreground">Open pull request</Text>
419 </Pressable>
420 ) : null}
421 {props.onRemove ? (
422 <Pressable
423 accessibilityRole="button"
424 onPress={props.onRemove}
425 className="rounded-xl bg-subtle p-4"
426 >
427 <Text className="text-foreground">Remove from draft</Text>
428 </Pressable>
429 ) : null}
430 </ScrollView>
431 </View>
432 </View>
433 </Modal>
434 );