Open History to see the last commit
1import * as Dialog from "./ui/Dialog";
2import { useEffect, useEffectEvent } from "react";
3import { Modal, Pressable, View } from "react-native";
4import ImageViewing from "react-native-image-viewing";
5
6import { openAttachmentInViewer } from "../lib/attachmentDownload";
7import type { ResolvedFilePreviewSource } from "./FilePreviewModal.types";
8import { MediaImagePreview } from "./MediaImagePreview";
9import { AppText as Text } from "./ui/AppText";
10
11function DocumentPreview(props: {
12 readonly source: ResolvedFilePreviewSource;
13 readonly onRequestClose: () => void;
14 readonly onOpenError?: (error: unknown) => void;
15}) {
16 const { uri, name } = props.source;
17 const onRequestClose = useEffectEvent(props.onRequestClose);
18 const onOpenError = useEffectEvent((error: unknown) => {
19 if (props.onOpenError) props.onOpenError(error);
20 else
21 Dialog.alert(
22 "Could not open document",
23 "A compatible viewer must be installed. Check your connection and try again.",
24 );
25 });
26 useEffect(() => {
27 const controller = new AbortController();
28 const input = {
29 uri,
30 attachment: {
31 name: name ?? "Document",
32 mimeType:
33 props.source.mimeType ??
34 (props.source.kind === "pdf" ? "application/pdf" : "application/octet-stream"),
35 },
36 signal: controller.signal,
37 };
38 const opened = openAttachmentInViewer(input);
39 void opened
40 .catch((error: unknown) => {
41 if (!controller.signal.aborted) onOpenError(error);
42 })
43 .finally(() => {
44 if (!controller.signal.aborted) onRequestClose();
45 });
46 return () => controller.abort();
47 }, [uri, name, props.source.mimeType, props.source.kind]);
48 return (
49 <Modal transparent animationType="fade" onRequestClose={props.onRequestClose}>
50 <View className="flex-1 items-center justify-center bg-backdrop p-6">
51 <View className="w-full max-w-sm gap-4 rounded-2xl bg-sheet-solid p-6">
52 <Text className="font-t3-semibold text-foreground">Opening document…</Text>
53 <Text className="text-foreground-muted" numberOfLines={2}>
54 {name ?? "Document"}
55 </Text>
56 <Pressable
57 accessibilityRole="button"
58 onPress={props.onRequestClose}
59 className="self-end p-3"
60 >
61 <Text className="text-foreground">Cancel</Text>
62 </Pressable>
63 </View>
64 </View>
65 </Modal>
66 );
67}
68
69export function FilePreview(props: {
70 readonly source: ResolvedFilePreviewSource;
71 readonly onRequestClose: () => void;
72 readonly onOpenError?: (error: unknown) => void;
73}) {
74 if (props.source.kind !== "image") return <DocumentPreview {...props} />;
75 if (props.source.actionsSource) return <MediaImagePreview {...props} />;
76 return (
77 <ImageViewing
78 images={
79 props.source.gallery?.map((item) => ({ uri: item.uri })) ?? [{ uri: props.source.uri }]
80 }
81 imageIndex={Math.max(
82 0,
83 props.source.gallery?.findIndex((item) => item.uri === props.source.uri) ?? 0,
84 )}
85 visible
86 onRequestClose={props.onRequestClose}
87 swipeToCloseEnabled={false}
88 doubleTapToZoomEnabled
89 />
90 );
91}