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