Open History to see the last commit
1import { requireNativeModule } from "expo";
2import { useEffect, useEffectEvent, useId } from "react";
3import { Alert } from "react-native";
4
5import type { ResolvedFilePreviewSource } from "./FilePreviewModal";
6
7const NativeControls = requireNativeModule<{
8 presentFile(
9 uri: string,
10 name: string,
11 sourceIdentifier: string,
12 identifier: string,
13 ): Promise<void>;
14 dismissFile(identifier: string): Promise<void>;
15}>("T3NativeControls");
16
17function NativeFilePreview(props: {
18 readonly source: ResolvedFilePreviewSource;
19 readonly onRequestClose: () => void;
20}) {
21 const { uri, name, sourceIdentifier } = props.source;
22 const identifier = useId();
23 const onRequestClose = useEffectEvent(props.onRequestClose);
24
25 useEffect(() => {
26 let canceled = false;
27 void NativeControls.presentFile(uri, name ?? "Preview", sourceIdentifier ?? "", identifier)
28 .catch(() => {
29 if (!canceled) {
30 Alert.alert("Could not open preview", "The file could not be loaded. Please try again.");
31 }
32 })
33 .finally(() => {
34 if (!canceled) onRequestClose();
35 });
36 return () => {
37 canceled = true;
38 void NativeControls.dismissFile(identifier).catch(() => undefined);
39 };
40 }, [uri, name, sourceIdentifier, identifier]);
41
42 return null;
43}
44
45export function FilePreview(props: {
46 readonly source: ResolvedFilePreviewSource;
47 readonly onRequestClose: () => void;
48}) {
49 return <NativeFilePreview {...props} />;
50}