Open History to see the last commit
1import * as Dialog from "./ui/Dialog";
2import { requireNativeModule } from "expo";
3import { useEffect, useEffectEvent, useId, useRef, useState } from "react";
4
5import type { FilePreviewImage, ResolvedFilePreviewSource } from "./FilePreviewModal.types";
6
7const NativeControls = requireNativeModule<{
8 presentFile(
9 uri: string,
10 name: string,
11 sourceIdentifier: string,
12 identifier: string,
13 ): Promise<void>;
14 presentFiles(
15 uris: readonly (string | null)[],
16 names: readonly string[],
17 selectedIndex: number,
18 sourceIdentifiers: readonly string[],
19 identifier: string,
20 ): Promise<void>;
21 updateFile(identifier: string, index: number, uri: string | null): Promise<void>;
22 dismissFile(identifier: string): Promise<void>;
23}>("T3NativeControls");
24
25// Null tells the viewer an image failed; undefined means its URL has not arrived yet.
26const nativeUri = (image: FilePreviewImage) => (image.failed ? null : image.uri);
27
28function NativeFilePreview(props: {
29 readonly source: ResolvedFilePreviewSource;
30 readonly onRequestClose: () => void;
31 readonly onOpenError?: (error: unknown) => void;
32}) {
33 const identifier = useId();
34 // Later URLs reach the open viewer through updateFile instead of presenting again.
35 const [presented] = useState(props.source);
36 const delivered = useRef<(string | null | undefined)[]>([]);
37 const onRequestClose = useEffectEvent(props.onRequestClose);
38 const onOpenError = useEffectEvent((error: unknown) => {
39 if (props.onOpenError) props.onOpenError(error);
40 else Dialog.alert("Could not open preview", "The file could not be loaded. Please try again.");
41 });
42
43 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- presents the native file preview and dismisses it on cleanup
44 useEffect(() => {
45 let canceled = false;
46 let presentation: Promise<void>;
47 // Images open while still loading; other files must download first so Quick Look can refuse them.
48 if (presented.kind === "image") {
49 const { images, selectedIndex } = presented;
50 delivered.current = images.map((image) => image.uri);
51 presentation = NativeControls.presentFiles(
52 images.map((image) => image.uri ?? null),
53 images.map((image) => image.name ?? "Preview"),
54 selectedIndex,
55 images.map((image) => image.sourceIdentifier ?? ""),
56 identifier,
57 );
58 } else {
59 const { uri, name, sourceIdentifier } = presented;
60 presentation = NativeControls.presentFile(
61 uri,
62 name ?? "Preview",
63 sourceIdentifier ?? "",
64 identifier,
65 );
66 }
67 void presentation
68 .catch((error: unknown) => {
69 if (!canceled) onOpenError(error);
70 })
71 .finally(() => {
72 if (!canceled) onRequestClose();
73 });
74 return () => {
75 canceled = true;
76 void NativeControls.dismissFile(identifier).catch(() => undefined);
77 };
78 }, [presented, identifier]);
79
80 const images = props.source.kind === "image" ? props.source.images : undefined;
81 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hands image URLs that resolve after opening to the native viewer
82 useEffect(() => {
83 images?.forEach((image, index) => {
84 const uri = nativeUri(image);
85 if (uri === undefined || delivered.current[index] === uri) return;
86 delivered.current[index] = uri;
87 void NativeControls.updateFile(identifier, index, uri).catch(() => undefined);
88 });
89 }, [images, identifier]);
90
91 return null;
92}
93
94export function FilePreview(props: {
95 readonly source: ResolvedFilePreviewSource;
96 readonly onRequestClose: () => void;
97 readonly onOpenError?: (error: unknown) => void;
98}) {
99 return <NativeFilePreview {...props} />;
100}