@@ -3,10 +3,13 @@ import { composerImageGallery } from "../lib/composerImageGallery"; |
3 | 3 | import { SymbolView } from "./ui/AppSymbol"; |
4 | 4 | import { imageMimeType } from "@t3tools/shared/image"; |
5 | 5 | import { videoMimeType } from "@t3tools/shared/video"; |
6 | | -import { useEffect, useMemo, useState } from "react"; |
| 6 | +import { useAtomValue } from "@effect/atom-react"; |
| 7 | +import { AsyncResult, Atom } from "effect/reactivity"; |
| 8 | +import { Paths } from "expo-file-system"; |
7 | 9 | import { Image, Pressable, ScrollView, View } from "react-native"; |
8 | 10 |
|
9 | 11 | import { AppText as Text } from "./ui/AppText"; |
| 12 | +import { MaterializedPreviewKey, materializedPreviewAtom } from "../lib/composerImagePreview"; |
10 | 13 | import { PierreEntryIcon } from "./PierreEntryIcon"; |
11 | 14 | import { |
12 | 15 | isFileBackedComposerAttachment, |
@@ -93,65 +96,31 @@ export function ComposerAttachmentThumbnail(props: ComposerAttachmentThumbnailPr |
93 | 96 | ); |
94 | 97 | } |
95 | 98 |
|
96 | | -const PREVIEW_CACHE_DIRECTORY = "t3-composer-previews"; |
97 | | - |
98 | 99 | /** Fabric re-parses URLs each layout; large data URLs can stall and abort commits. */ |
99 | 100 | const INLINE_PREVIEW_FALLBACK_MAX_CHARS = 256_000; |
100 | 101 |
|
101 | | -async function materializeDataUrlPreview(id: string, dataUrl: string): Promise<string | null> { |
102 | | - const comma = dataUrl.indexOf(","); |
103 | | - if (comma < 0) return null; |
104 | | - const { Directory, File, Paths } = await import("expo-file-system"); |
105 | | - const mimeType = /^data:([^;,]+)/.exec(dataUrl)?.[1] ?? "image/jpeg"; |
106 | | - const extension = (mimeType.split("/")[1] ?? "jpg").replace("jpeg", "jpg"); |
107 | | - const directory = new Directory(Paths.cache, PREVIEW_CACHE_DIRECTORY); |
108 | | - directory.create({ idempotent: true, intermediates: true }); |
109 | | - const file = new File(directory, `${id}.${extension}`); |
110 | | - if (!file.exists) { |
111 | | - file.create(); |
112 | | - await file.write(dataUrl.slice(comma + 1), { encoding: "base64" }); |
113 | | - } |
114 | | - return file.uri; |
115 | | -} |
| 102 | +const noMaterializedPreview = Atom.make(AsyncResult.initial<string | null>()).pipe( |
| 103 | + Atom.withLabel("composer-image-preview:none"), |
| 104 | +); |
116 | 105 |
|
117 | 106 | function useComposerImagePreviewUri(attachment: DraftComposerImageAttachment): string | null { |
118 | | - const { id, fileUri, previewUri } = attachment; |
119 | | - const [rebased, setRebased] = useState<{ fileUri: string; uri: string } | null>(null); |
120 | | - const [materialized, setMaterialized] = useState<{ id: string; uri: string | null } | null>(null); |
| 107 | + const { fileUri, previewUri } = attachment; |
121 | 108 | const inlinePreview = fileUri === undefined && previewUri.startsWith("data:"); |
122 | | - useEffect(() => { |
123 | | - if (fileUri === undefined) return; |
124 | | - let cancelled = false; |
125 | | - void (async () => { |
126 | | - const { Paths } = await import("expo-file-system"); |
127 | | - const owned = resolveOwnedComposerAttachmentFileUri(fileUri, Paths.document.uri); |
128 | | - if (!cancelled && owned !== null && owned !== previewUri) setRebased({ fileUri, uri: owned }); |
129 | | - })(); |
130 | | - return () => { |
131 | | - cancelled = true; |
132 | | - }; |
133 | | - }, [fileUri, previewUri]); |
134 | | - useEffect(() => { |
135 | | - if (!inlinePreview) return; |
136 | | - let cancelled = false; |
137 | | - void materializeDataUrlPreview(id, previewUri) |
138 | | - .then((uri) => { |
139 | | - if (!cancelled && uri !== null) setMaterialized({ id, uri }); |
140 | | - }) |
141 | | - .catch((error: unknown) => { |
142 | | - console.warn("[composer-attachments] could not cache an image preview", error); |
143 | | - if (!cancelled) setMaterialized({ id, uri: null }); |
144 | | - }); |
145 | | - return () => { |
146 | | - cancelled = true; |
147 | | - }; |
148 | | - }, [id, inlinePreview, previewUri]); |
149 | | - if (fileUri !== undefined && rebased?.fileUri === fileUri) return rebased.uri; |
150 | | - if (fileUri !== undefined) return previewUri.startsWith("data:") ? fileUri : previewUri; |
| 109 | + const materialized = useAtomValue( |
| 110 | + inlinePreview |
| 111 | + ? materializedPreviewAtom(new MaterializedPreviewKey(attachment)) |
| 112 | + : noMaterializedPreview, |
| 113 | + ); |
| 114 | + if (fileUri !== undefined) { |
| 115 | + // Reinstalls move the app container, so a stored file URI may point at the old one. |
| 116 | + const owned = resolveOwnedComposerAttachmentFileUri(fileUri, Paths.document.uri); |
| 117 | + if (owned !== null) return owned; |
| 118 | + return previewUri.startsWith("data:") ? fileUri : previewUri; |
| 119 | + } |
151 | 120 | if (inlinePreview) { |
152 | | - if (materialized?.id !== id) return null; |
| 121 | + if (!AsyncResult.isSuccess(materialized)) return null; |
153 | 122 | return ( |
154 | | - materialized.uri ?? |
| 123 | + materialized.value ?? |
155 | 124 | (previewUri.length <= INLINE_PREVIEW_FALLBACK_MAX_CHARS ? previewUri : null) |
156 | 125 | ); |
157 | 126 | } |
@@ -280,15 +249,12 @@ function ComposerVideoAttachment(props: { |
280 | 249 | const { attachment } = props; |
281 | 250 | const sourceIdentifier = `draft:${attachment.id}`; |
282 | 251 | const style = { width: props.size, height: props.size, borderRadius: props.borderRadius }; |
283 | | - const actionsSource = useMemo<MediaActionsSource>( |
284 | | - () => ({ |
285 | | - name: attachment.name, |
286 | | - mimeType: videoMimeType(attachment) ?? attachment.mimeType, |
287 | | - sourceIdentifier, |
288 | | - attachment, |
289 | | - }), |
290 | | - [attachment, sourceIdentifier], |
291 | | - ); |
| 252 | + const actionsSource: MediaActionsSource = { |
| 253 | + name: attachment.name, |
| 254 | + mimeType: videoMimeType(attachment) ?? attachment.mimeType, |
| 255 | + sourceIdentifier, |
| 256 | + attachment, |
| 257 | + }; |
292 | 258 |
|
293 | 259 | return ( |
294 | 260 | <VideoAttachmentTile |
@@ -304,7 +270,7 @@ function ComposerVideoAttachment(props: { |
304 | 270 | } |
305 | 271 |
|
306 | 272 | export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) { |
307 | | - const gallery = useMemo(() => composerImageGallery(props.attachments), [props.attachments]); |
| 273 | + const gallery = composerImageGallery(props.attachments); |
308 | 274 | const size = props.imageSize ?? 72; |
309 | 275 | const radius = props.imageBorderRadius ?? 16; |
310 | 276 | const removeButtonPlacement = props.removeButtonPlacement ?? "overlay"; |
|