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";
6import { MediaImagePreview } from "./MediaImagePreview";
7
8const NativeControls = requireNativeModule<{
9 presentFile(
10 uri: string,
11 name: string,
12 sourceIdentifier: string,
13 identifier: string,
14 ): Promise<void>;
15 dismissFile(identifier: string): Promise<void>;
16}>("T3NativeControls");
17
18function NativeFilePreview(props: {
19 readonly source: ResolvedFilePreviewSource;
20 readonly onRequestClose: () => void;
21}) {
22 const { uri, name, sourceIdentifier } = props.source;
23 const identifier = useId();
24 const onRequestClose = useEffectEvent(props.onRequestClose);
25
26 useEffect(() => {
27 let canceled = false;
28 void NativeControls.presentFile(uri, name ?? "Preview", sourceIdentifier ?? "", identifier)
29 .catch(() => {
30 if (!canceled) {
31 Alert.alert("Could not open preview", "The file could not be loaded. Please try again.");
32 }
33 })
34 .finally(() => {
35 if (!canceled) onRequestClose();
36 });
37 return () => {
38 canceled = true;
39 void NativeControls.dismissFile(identifier).catch(() => undefined);
40 };
41 }, [uri, name, sourceIdentifier, identifier]);
42
43 return null;
44}
45
46export function FilePreview(props: {
47 readonly source: ResolvedFilePreviewSource;
48 readonly onRequestClose: () => void;
49}) {
50 return props.source.kind === "image" && props.source.actionsSource ? (
51 <MediaImagePreview {...props} />
52 ) : (
53 <NativeFilePreview {...props} />
54 );
55}