Open History to see the last commit
1import { useEffect, useEffectEvent } from "react";
2import { Alert } from "react-native";
3import ImageViewing from "react-native-image-viewing";
4
5import { downloadAndShareAttachment, shareLocalAttachment } from "../lib/attachmentDownload";
6import type { ResolvedFilePreviewSource } from "./FilePreviewModal";
7import { MediaImagePreview } from "./MediaImagePreview";
8
9function PdfPreview(props: {
10 readonly source: ResolvedFilePreviewSource;
11 readonly onRequestClose: () => void;
12}) {
13 const { uri, name } = props.source;
14 const onRequestClose = useEffectEvent(props.onRequestClose);
15 useEffect(() => {
16 const controller = new AbortController();
17 const input = {
18 attachment: { name: name ?? "Document.pdf", mimeType: "application/pdf" },
19 signal: controller.signal,
20 };
21 // Android's system chooser supplies the installed PDF apps.
22 const opened =
23 uri.startsWith("file:") || uri.startsWith("content:")
24 ? shareLocalAttachment({ ...input, uri })
25 : downloadAndShareAttachment({ ...input, url: uri });
26 void opened
27 .catch(() => {
28 if (!controller.signal.aborted) Alert.alert("Could not open PDF", "Please try again.");
29 })
30 .finally(() => {
31 if (!controller.signal.aborted) onRequestClose();
32 });
33 return () => controller.abort();
34 }, [uri, name]);
35 return null;
36}
37
38export function FilePreview(props: {
39 readonly source: ResolvedFilePreviewSource;
40 readonly onRequestClose: () => void;
41}) {
42 if (props.source.kind === "pdf") return <PdfPreview {...props} />;
43 if (props.source.actionsSource) return <MediaImagePreview {...props} />;
44 return (
45 <ImageViewing
46 images={[{ uri: props.source.uri }]}
47 imageIndex={0}
48 visible
49 onRequestClose={props.onRequestClose}
50 swipeToCloseEnabled
51 doubleTapToZoomEnabled
52 />
53 );
54}