@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 362 lines 14 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
3 import * as Effect from "effect/Effect";
4 import * as Schema from "effect/Schema";
5 import { AsyncResult, Atom } from "effect/reactivity";
6 import { Image } from "expo-image";
7 import { T, useGT } from "gt-react-native";
8 import { createContext, useContext, useId, useState } from "react";
9 import { fetch } from "expo/fetch";
10 import { parse, SvgAst, type JsxAST } from "react-native-svg";
11 import { Pressable, StyleSheet, View, type ViewStyle } from "react-native";
12
13 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
14 import { useFilePreviewGallery } from "../../components/ui/FilePreviewGallery";
15 import { AppText as Text } from "../../components/ui/AppText";
16 import type { FilePreviewSource } from "../../components/ui/FilePreviewModal";
17 import { MediaActionsMenu } from "../../components/MediaActionsMenu";
18 import { PresentationSource } from "../../components/ui/NativePresentation";
19 import { useMediaActions } from "../../state/mediaActions";
20 import type { MediaActionsSource } from "../../lib/mediaActionsSource";
21 import { useAssetUrlState } from "../../state/assets";
22 import { WorkspaceMutationIdContext } from "./workspaceMutationIdContext";
23 import { ZoomableImagePressable } from "./ZoomableImagePressable";
24 import { assetImageCacheKey } from "../../lib/assetImageSource";
25 import { filePreviewSourceIdentifier } from "../../lib/filePreviewGallery";
26 import {
27 MARKDOWN_IMAGE_MAX_WIDTH,
28 type MarkdownImageDisplaySize,
29 resolveMarkdownImageDisplaySize,
30 resolveSvgImageSize,
31 } from "./markdownImageSize";
32
33 /** Use known feed width immediately; later image measurement would move already-positioned rows. */
34 export const MarkdownImageAvailableWidthContext = createContext(0);
35
36 type ImageSize = { readonly width: number; readonly height: number };
37
38 class SvgLoadError extends Schema.TaggedError<SvgLoadError>()("SvgLoadError", {
39 cause: Schema.Defect(),
40 }) {}
41
42 async function readSvgXml(uri: string, signal: AbortSignal): Promise<string> {
43 if (uri.startsWith("data:image/svg+xml")) {
44 const comma = uri.indexOf(",");
45 if (comma < 0) throw new Error("SVG data URI has no content");
46 const content = decodeURIComponent(uri.slice(comma + 1));
47 return uri.slice(0, comma).includes(";base64")
48 ? new TextDecoder().decode(
49 Uint8Array.from(atob(content), (character) => character.charCodeAt(0)),
50 )
51 : content;
52 }
53 // expo/fetch, not the runtime HttpClient: it is the one that reads file:// URIs.
54 const response = await fetch(uri, { signal });
55 if (!response.ok && !(response.status === 0 && uri.startsWith("file://")))
56 throw new Error(`SVG request failed: ${response.status}`);
57 return response.text();
58 }
59
60 type SvgDocument = { readonly ast: JsxAST; readonly size: ImageSize | null };
61
62 // Feed rows unmount as they scroll away. Only parsed documents outlive their
63 // readers (for the row that scrolls back); a failed load is dropped with its last
64 // reader so the next mount fetches again.
65 const parsedSvgDocumentAtom = Atom.family((_uri: string) =>
66 Atom.make<SvgDocument | null>(null).pipe(
67 Atom.setIdleTTL(60_000),
68 Atom.withLabel("mobile:thread-markdown-svg-parsed"),
69 ),
70 );
71
72 const svgDocumentAtom = Atom.family((uri: string) =>
73 Atom.make((get) => {
74 const parsed = parsedSvgDocumentAtom(uri);
75 const cached = get(parsed);
76 if (cached !== null) return Effect.succeed(cached);
77 return Effect.tryPromise({
78 try: async (signal): Promise<SvgDocument> => {
79 const xml = await readSvgXml(uri, signal);
80 signal.throwIfAborted();
81 const ast = parse(xml);
82 if (!ast) throw new Error("SVG has no root element");
83 return { ast, size: resolveSvgImageSize(ast.props) };
84 },
85 catch: (cause) => new SvgLoadError({ cause }),
86 }).pipe(Effect.tap((document) => Effect.sync(() => get.set(parsed, document))));
87 }).pipe(Atom.withLabel("mobile:thread-markdown-svg")),
88 );
89
90 const noSvgDocumentAtom = Atom.make(AsyncResult.initial<SvgDocument, SvgLoadError>());
91
92 export function ThreadMarkdownImageView(props: {
93 readonly uri: string | null;
94 readonly sourceKey: string;
95 readonly sourceIdentifier?: string;
96 readonly cacheKey?: string | undefined;
97 readonly unavailable: boolean;
98 readonly unavailableDetail?: string | undefined;
99 readonly alt: string | null;
100 readonly knownSize?: { readonly width: number; readonly height: number } | undefined;
101 readonly format?: "svg";
102 readonly actionsSource?: MediaActionsSource;
103 readonly onPressPreview: (source: FilePreviewSource) => void;
104 }) {
105 const gt = useGT();
106 const withGallery = useFilePreviewGallery();
107 const localSourceIdentifier = useId();
108 const sourceIdentifier = props.sourceIdentifier ?? localSourceIdentifier;
109 const mediaActions = useMediaActions(props.actionsSource);
110 const contextWidth = useContext(MarkdownImageAvailableWidthContext);
111 const [measuredWidth, setMeasuredWidth] = useState(0);
112 const availableWidth =
113 measuredWidth > 0 && contextWidth > 0
114 ? Math.min(contextWidth, measuredWidth)
115 : contextWidth || measuredWidth;
116 const svg = useAtomValue(
117 props.format === "svg" && props.uri !== null ? svgDocumentAtom(props.uri) : noSvgDocumentAtom,
118 );
119 const svgDocument = AsyncResult.isSuccess(svg) ? svg.value : null;
120 // Keyed by source so a new source drops the previous image's decoded dimensions.
121 const [decoded, setDecoded] = useState<{ sourceKey: string; size: ImageSize } | null>(null);
122 const decodedSize =
123 svgDocument?.size ?? (decoded?.sourceKey === props.sourceKey ? decoded.size : null);
124 // A failure belongs to one URI; any URI change, including through null, retries.
125 const [failure, setFailure] = useState({ uri: props.uri, failed: false });
126 if (failure.uri !== props.uri) setFailure({ uri: props.uri, failed: false });
127
128 const sourceSize = decodedSize ?? props.knownSize ?? null;
129 const displaySize: MarkdownImageDisplaySize | null =
130 sourceSize === null || availableWidth <= 0
131 ? null
132 : resolveMarkdownImageDisplaySize({
133 sourceWidth: sourceSize.width,
134 sourceHeight: sourceSize.height,
135 availableWidth,
136 });
137 const failed =
138 props.unavailable ||
139 (props.uri !== null && failure.uri === props.uri && failure.failed) ||
140 AsyncResult.isFailure(svg);
141 const placeholderWidth: ViewStyle["width"] =
142 availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";
143 const frameStyle: ViewStyle = displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 };
144
145 return (
146 <View
147 onLayout={(event) => setMeasuredWidth(event.nativeEvent.layout.width)}
148 style={{ alignSelf: "stretch", gap: 6 }}
149 >
150 {props.uri === null || failed ? (
151 <MediaActionsMenu media={mediaActions}>
152 <Pressable
153 accessibilityRole="imagebutton"
154 accessibilityLabel={props.alt ?? gt("Markdown image")}
155 accessibilityHint={
156 mediaActions.actions.length > 0 ? gt("Touch and hold for media actions") : undefined
157 }
158 className="items-center justify-center rounded-[10px] bg-md-code-bg"
159 style={frameStyle}
160 >
161 {failed ? (
162 <Text className="px-3 text-center text-xs text-foreground-muted">
163 {props.unavailableDetail
164 ? gt("Image unavailable. {detail}", { detail: props.unavailableDetail })
165 : gt("Image unavailable")}
166 </Text>
167 ) : (
168 <AppActivityIndicator />
169 )}
170 </Pressable>
171 </MediaActionsMenu>
172 ) : (
173 <PresentationSource identifier={sourceIdentifier} style={{ alignSelf: "flex-start" }}>
174 <MediaActionsMenu media={mediaActions}>
175 <ZoomableImagePressable
176 key={props.sourceKey}
177 accessibilityRole="imagebutton"
178 accessibilityLabel={props.alt ?? gt("Markdown image")}
179 accessibilityHint={
180 mediaActions.actions.length > 0 ? gt("Touch and hold for media actions") : undefined
181 }
182 onPress={() =>
183 props.onPressPreview(
184 withGallery({
185 kind: "image",
186 uri: props.uri!,
187 name: props.actionsSource?.name ?? props.alt ?? gt("Image"),
188 sourceIdentifier,
189 actionsSource: props.actionsSource,
190 }),
191 )
192 }
193 className="overflow-hidden rounded-[10px] bg-md-code-bg"
194 style={{ alignSelf: "flex-start", ...frameStyle }}
195 >
196 {props.format === "svg" ? (
197 <ThreadMarkdownSvg ast={svgDocument?.ast ?? null} />
198 ) : (
199 <ThreadMarkdownImageRequest
200 uri={props.uri}
201 cacheKey={props.cacheKey}
202 onLoad={(size) =>
203 // Only the first decode counts: later loads report the bitmap
204 // downsampled for the current frame (969 vs 970 wide), so keeping
205 // them resizes the frame, which reloads the image, and so on.
206 setDecoded((current) =>
207 current?.sourceKey === props.sourceKey
208 ? current
209 : { sourceKey: props.sourceKey, size },
210 )
211 }
212 onError={() => setFailure({ uri: props.uri, failed: true })}
213 />
214 )}
215 </ZoomableImagePressable>
216 </MediaActionsMenu>
217 </PresentationSource>
218 )}
219 {props.alt ? (
220 <Text selectable className="text-xs text-foreground-muted">
221 {props.alt}
222 </Text>
223 ) : null}
224 </View>
225 );
226 }
227
228 function ThreadMarkdownImageRequest(props: {
229 readonly uri: string;
230 readonly cacheKey: string | undefined;
231 readonly onLoad: (sourceSize: ImageSize) => void;
232 readonly onError: () => void;
233 }) {
234 const [loaded, setLoaded] = useState(false);
235
236 return (
237 <>
238 <Image
239 source={{ uri: props.uri, cacheKey: props.cacheKey }}
240 contentFit="contain"
241 cachePolicy="memory-disk"
242 accessible={false}
243 onLoad={(event) => {
244 setLoaded(true);
245 props.onLoad(event.source);
246 }}
247 onError={props.onError}
248 style={{ width: "100%", height: "100%", opacity: loaded ? 1 : 0 }}
249 />
250 {loaded ? null : <ThreadMarkdownImageLoading />}
251 </>
252 );
253 }
254
255 function ThreadMarkdownImageLoading() {
256 return (
257 <View
258 pointerEvents="none"
259 style={[StyleSheet.absoluteFill, { alignItems: "center", justifyContent: "center" }]}
260 >
261 <Text className="text-xs text-foreground-muted">
262 <T>Loading image…</T>
263 </Text>
264 </View>
265 );
266 }
267
268 function ThreadMarkdownSvg(props: { readonly ast: JsxAST | null }) {
269 return props.ast === null ? (
270 <ThreadMarkdownImageLoading />
271 ) : (
272 <SvgAst ast={props.ast} override={{ width: "100%", height: "100%", accessible: false }} />
273 );
274 }
275
276 export function ThreadMarkdownImage(props: {
277 readonly environmentId: EnvironmentId;
278 readonly resource: Extract<
279 AssetResource,
280 { readonly _tag: "attachment" | "media-file" | "draft-workspace-file" | "tool-output-image" }
281 >;
282 readonly alt: string | null;
283 readonly srcFragment?: string;
284 readonly actionsSource?: MediaActionsSource;
285 readonly onPressPreview: (source: FilePreviewSource) => void;
286 }) {
287 const gt = useGT();
288 const withGallery = useFilePreviewGallery();
289 const workspaceMutationId = useContext(WorkspaceMutationIdContext);
290 const assetUrl = useAssetUrlState(
291 props.environmentId,
292 props.resource,
293 props.resource._tag === "media-file" ? workspaceMutationId : undefined,
294 );
295 const resourceKey =
296 props.resource._tag === "attachment"
297 ? `attachment:${props.resource.attachmentId}`
298 : props.resource._tag === "tool-output-image"
299 ? `tool-output:${props.resource.itemId}:${props.resource.index}`
300 : `workspace:${props.resource.path}`;
301 // The view keeps the first decoded size per key, so a file edited in place needs a
302 // new key; a re-signed URL for the same revision must not get one.
303 const sourceKey =
304 assetUrl._tag === "Success" && assetUrl.fileRevision !== undefined
305 ? `${resourceKey}@${assetUrl.fileRevision}`
306 : resourceKey;
307
308 return (
309 <ThreadMarkdownImageView
310 uri={assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null}
311 cacheKey={
312 assetUrl._tag === "Success"
313 ? assetImageCacheKey(props.environmentId, props.resource, assetUrl.fileRevision)
314 : undefined
315 }
316 sourceKey={sourceKey}
317 sourceIdentifier={filePreviewSourceIdentifier({
318 kind: "image",
319 environmentId: props.environmentId,
320 resource: props.resource,
321 })}
322 unavailable={assetUrl._tag === "Failure"}
323 unavailableDetail={
324 assetUrl._tag === "Failure" && assetUrl.reason === "missing-file-access"
325 ? gt("This connection cannot read host files. Pair again with a new link to view it.")
326 : undefined
327 }
328 knownSize={assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined}
329 format={
330 (props.resource._tag === "media-file" || props.resource._tag === "draft-workspace-file") &&
331 /\.svg$/i.test(props.resource.path)
332 ? "svg"
333 : undefined
334 }
335 alt={props.alt}
336 actionsSource={props.actionsSource}
337 onPressPreview={(source) => {
338 const { uri: _uri, ...rest } = source as FilePreviewSource & { uri?: string };
339 props.onPressPreview(
340 withGallery({
341 ...rest,
342 gallery: undefined,
343 environmentId: props.environmentId,
344 resource: props.resource,
345 }),
346 );
347 }}
348 />
349 );
350 }
351
352 export function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {
353 return (
354 <ThreadMarkdownImageView
355 uri={null}
356 sourceKey="unavailable"
357 unavailable
358 alt={props.alt}
359 onPressPreview={() => undefined}
360 />
361 );
362 }