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

FilePreviewModal.tsx

107 lines4.0 KB

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