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

ComposerAttachmentStrip.tsx

335 lines12.1 KB

1import { FilePreviewGallery, useFilePreviewGallery } from "./FilePreviewGallery";
2import { composerImageGallery } from "../lib/composerImageGallery";
3import { SymbolView } from "./ui/AppSymbol";
4import { imageMimeType } from "@t3tools/shared/image";
5import { videoMimeType } from "@t3tools/shared/video";
6import { useAtomValue } from "@effect/atom-react";
7import { AsyncResult, Atom } from "effect/reactivity";
8import { Paths } from "expo-file-system";
9import { Image, Pressable, ScrollView, View } from "react-native";
10​
11import { AppText as Text } from "./ui/AppText";
12import { MaterializedPreviewKey, materializedPreviewAtom } from "../lib/composerImagePreview";
13import { PierreEntryIcon } from "./PierreEntryIcon";
14import {
15 isFileBackedComposerAttachment,
16 type DraftComposerAttachment,
17 type DraftComposerFileAttachment,
18 type DraftComposerImageAttachment,
19} from "../lib/composerImages";
20import { resolveOwnedComposerAttachmentFileUri } from "../lib/composerAttachmentFiles";
21import { VideoAttachmentTile } from "./VideoAttachmentTile";
22import { type MediaActionsSource } from "../lib/mediaActionsSource";
23import { PresentationSource } from "./ui/NativePresentation";
24import type { FilePreviewSource } from "./FilePreviewModal";
25import { isPdfFile } from "../lib/filePreview";
26import type { EnvironmentId } from "@t3tools/contracts";
27import {
28 retryComposerAttachmentUpload,
29 useComposerAttachmentUploadState,
30} from "../state/composer-attachment-uploads";
31​
32export interface ComposerAttachmentStripProps {
33 readonly environmentId?: EnvironmentId;
34 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
35 readonly onRemove: (imageId: string) => void;
36 readonly onPressPreview?: (source: FilePreviewSource) => void;
37 readonly onPressVideo?: (
38 attachment: DraftComposerFileAttachment,
39 sourceIdentifier: string,
40 ) => void;
41 readonly onPressDocument?: (attachment: DraftComposerFileAttachment) => void;
42 readonly imageSize?: number;
43 readonly imageBorderRadius?: number;
44 readonly removeButtonPlacement?: "overlay" | "gutter";
46​
47type ComposerAttachmentThumbnailProps = {
48 readonly environmentId?: EnvironmentId;
49 readonly attachment: DraftComposerAttachment;
50 readonly size: number;
51 readonly borderRadius: number;
52 readonly compact?: boolean;
53 readonly onPressPreview?: (source: FilePreviewSource) => void;
54 readonly onPressVideo?: (
55 attachment: DraftComposerFileAttachment,
56 sourceIdentifier: string,
57 ) => void;
58 readonly onPressDocument?: (attachment: DraftComposerFileAttachment) => void;
59};
60​
61export function ComposerAttachmentThumbnail(props: ComposerAttachmentThumbnailProps) {
62 const upload = useComposerAttachmentUploadState(props.environmentId, props.attachment.id);
63 return (
64 <View style={{ width: props.size, height: props.size }}>
65 <ComposerAttachmentContent {...props} />
66 {upload && upload.status !== "ready" ? (
67 <Pressable
68 accessibilityRole={upload.status === "failed" ? "button" : "text"}
69 accessibilityLabel={
70 upload.status === "failed"
71 ? `Retry uploading ${props.attachment.name}`
72 : `Uploading ${props.attachment.name}, ${Math.floor(upload.progress * 100)}%`
73 }
74 accessibilityHint={upload.status === "failed" ? upload.reason : undefined}
75 disabled={upload.status !== "failed"}
76 onPress={() =>
77 props.environmentId &&
78 retryComposerAttachmentUpload(props.environmentId, props.attachment.id)
79 }
80 className="absolute bottom-0.5 left-0.5 flex-row items-center gap-0.5 rounded-full bg-black/70 px-1 py-0.5"
81 >
82 <SymbolView
83 name={upload.status === "failed" ? "arrow.clockwise" : "arrow.up"}
84 size={props.compact ? 8 : 10}
85 tintColor="#ffffff"
86 type="monochrome"
87 />
88 {!props.compact ? (
89 <Text className="text-2xs text-white">
90 {upload.status === "failed" ? "Retry" : `${Math.floor(upload.progress * 100)}%`}
91 </Text>
92 ) : null}
93 </Pressable>
94 ) : null}
95 </View>
96 );
98​
99/** Fabric re-parses URLs each layout; large data URLs can stall and abort commits. */
100const INLINE_PREVIEW_FALLBACK_MAX_CHARS = 256_000;
101​
102const noMaterializedPreview = Atom.make(AsyncResult.initial<string | null>()).pipe(
103 Atom.withLabel("composer-image-preview:none"),
104);
105​
106function useComposerImagePreviewUri(attachment: DraftComposerImageAttachment): string | null {
107 const { fileUri, previewUri } = attachment;
108 const inlinePreview = fileUri === undefined && previewUri.startsWith("data:");
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 }
120 if (inlinePreview) {
121 if (!AsyncResult.isSuccess(materialized)) return null;
122 return (
123 materialized.value ??
124 (previewUri.length <= INLINE_PREVIEW_FALLBACK_MAX_CHARS ? previewUri : null)
125 );
126 }
127 return previewUri;
129​
130function ComposerImageAttachment(
131 props: ComposerAttachmentThumbnailProps & { readonly attachment: DraftComposerImageAttachment },
132) {
133 const { attachment } = props;
134 const withGallery = useFilePreviewGallery();
135 const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
136 const previewUri = useComposerImagePreviewUri(attachment);
137 const sourceIdentifier = `draft-image:${attachment.id}`;
138 return (
139 <PresentationSource identifier={sourceIdentifier}>
140 <Pressable
141 accessibilityRole="imagebutton"
142 accessibilityLabel={`Open ${attachment.name}`}
143 disabled={!props.onPressPreview}
144 onPress={() =>
145 props.onPressPreview?.(
146 withGallery(
147 isFileBackedComposerAttachment(attachment)
148 ? { kind: "image", attachment, name: attachment.name, sourceIdentifier }
149 : {
150 kind: "image",
151 uri: attachment.dataUrl ?? attachment.previewUri,
152 name: attachment.name,
153 sourceIdentifier,
154 },
155 ),
156 )
157 }
158 >
159 <Image
160 source={previewUri === null ? undefined : { uri: previewUri }}
161 style={style}
162 className="bg-subtle"
163 resizeMode="cover"
164 />
165 </Pressable>
166 </PresentationSource>
167 );
169​
170function ComposerAttachmentContent(props: ComposerAttachmentThumbnailProps) {
171 const { attachment } = props;
172 // The document picker types every pick as a plain file, so a picture arrives here as one.
173 if (attachment.type === "image" || imageMimeType(attachment) !== null) {
174 const { source: _droppedSource, ...rest } = attachment;
175 return (
176 <ComposerImageAttachment
177 {...props}
178 attachment={
179 attachment.type === "image"
180 ? attachment
181 : { ...rest, type: "image", previewUri: attachment.fileUri }
182 }
183 />
184 );
185 }
186 const onPressVideo = props.onPressVideo;
187 if (onPressVideo && videoMimeType(attachment) !== null) {
188 return (
189 <ComposerVideoAttachment {...props} attachment={attachment} onPressVideo={onPressVideo} />
190 );
191 }
192 return <ComposerFileAttachment {...props} attachment={attachment} />;
194​
195function ComposerFileAttachment(
196 props: ComposerAttachmentThumbnailProps & { readonly attachment: DraftComposerFileAttachment },
197) {
198 const { attachment } = props;
199 const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
200 const canPreview = isPdfFile(attachment) && props.onPressPreview !== undefined;
201 const sourceIdentifier = `draft-file:${attachment.id}`;
202 const onPressDocument = props.onPressDocument;
203 return (
204 <>
205 <PresentationSource identifier={sourceIdentifier}>
206 <Pressable
207 accessibilityRole="button"
208 accessibilityLabel={`Open ${attachment.name}`}
209 disabled={!canPreview && onPressDocument === undefined}
210 onPress={() =>
211 canPreview
212 ? props.onPressPreview?.({
213 kind: "pdf",
214 name: attachment.name,
215 attachment,
216 sourceIdentifier,
217 })
218 : onPressDocument?.(attachment)
219 }
220 className={
221 props.compact
222 ? "items-center justify-center bg-subtle"
223 : "items-center justify-center gap-1 bg-subtle px-2"
224 }
225 style={style}
226 >
227 <PierreEntryIcon path={attachment.name} kind="file" size={props.compact ? 15 : 22} />
228 {!props.compact ? (
229 <Text className="w-full text-center text-2xs text-foreground" numberOfLines={1}>
230 {attachment.name}
231 </Text>
232 ) : null}
233 </Pressable>
234 </PresentationSource>
235 </>
236 );
238​
239function ComposerVideoAttachment(props: {
240 readonly attachment: DraftComposerFileAttachment;
241 readonly size: number;
242 readonly borderRadius: number;
243 readonly compact?: boolean;
244 readonly onPressVideo: (
245 attachment: DraftComposerFileAttachment,
246 sourceIdentifier: string,
247 ) => void;
248}) {
249 const { attachment } = props;
250 const sourceIdentifier = `draft:${attachment.id}`;
251 const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
252 const actionsSource: MediaActionsSource = {
253 name: attachment.name,
254 mimeType: videoMimeType(attachment) ?? attachment.mimeType,
255 sourceIdentifier,
256 attachment,
257 };
258​
259 return (
260 <VideoAttachmentTile
261 name={attachment.name}
262 sourceIdentifier={sourceIdentifier}
263 thumbnailSource={attachment}
264 compact={props.compact}
265 onPress={() => props.onPressVideo(attachment, sourceIdentifier)}
266 actionsSource={actionsSource}
267 style={style}
268 />
269 );
271​
272export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) {
273 const gallery = composerImageGallery(props.attachments);
274 const size = props.imageSize ?? 72;
275 const radius = props.imageBorderRadius ?? 16;
276 const removeButtonPlacement = props.removeButtonPlacement ?? "overlay";
277 const removeButtonGutter = removeButtonPlacement === "gutter" ? 10 : 0;
278​
279 if (props.attachments.length === 0) {
280 return null;
281 }
282​
283 return (
284 <FilePreviewGallery value={gallery}>
285 <ScrollView
286 horizontal
287 showsHorizontalScrollIndicator={false}
288 keyboardShouldPersistTaps="always"
289 className="grow-0"
290 >
291 <View className="flex-row gap-2.5">
292 {props.attachments.map((attachment) => (
293 <View
294 key={attachment.id}
295 className="relative"
296 style={{
297 paddingTop: removeButtonGutter,
298 paddingRight: removeButtonGutter,
299 }}
300 >
301 <ComposerAttachmentThumbnail
302 environmentId={props.environmentId}
303 attachment={attachment}
304 size={size}
305 borderRadius={radius}
306 onPressPreview={props.onPressPreview}
307 onPressVideo={props.onPressVideo}
308 onPressDocument={props.onPressDocument}
309 />
310 <Pressable
311 accessibilityRole="button"
312 accessibilityLabel={`Remove ${attachment.name}`}
313 className="absolute h-[22px] w-[22px] items-center justify-center rounded-[11px] bg-black/55"
314 style={{
315 top: removeButtonPlacement === "gutter" ? 0 : 4,
316 right: removeButtonPlacement === "gutter" ? 0 : 4,
317 }}
318 hitSlop={6}
319 onPress={() => props.onRemove(attachment.id)}
320 >
321 <SymbolView
322 name="xmark"
323 size={9}
324 tintColor="#ffffff"
325 type="monochrome"
326 weight="bold"
327 />
328 </Pressable>
329 </View>
330 ))}
331 </View>
332 </ScrollView>
333 </FilePreviewGallery>
334 );