Open History to see the last commit
1import { Button } from "./ui/Button";
2import * as Dialog from "./ui/Dialog";
3import { useEffect, useEffectEvent } from "react";
4import { Modal, View } from "react-native";
5import ImageViewing from "react-native-image-viewing";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7
8import { openAttachmentInViewer } from "../lib/attachmentDownload";
9import type { ResolvedFilePreviewSource } from "./FilePreviewModal.types";
10import { MediaImagePreview } from "./MediaImagePreview";
11import { PdfPreview } from "./PdfPreview";
12import { ControlPill } from "./ui/ControlPill";
13import { AppText as Text } from "./ui/AppText";
14
15function PdfDocumentPreview(props: {
16 readonly source: ResolvedFilePreviewSource;
17 readonly onRequestClose: () => void;
18}) {
19 const insets = useSafeAreaInsets();
20 return (
21 <Modal
22 animationType="slide"
23 onRequestClose={props.onRequestClose}
24 statusBarTranslucent
25 navigationBarTranslucent
26 >
27 <View
28 className="flex-1 bg-card"
29 style={{ paddingTop: insets.top, paddingBottom: insets.bottom }}
30 >
31 <View className="flex-row items-center gap-3 border-b border-border px-4 py-2">
32 <Text className="flex-1 font-t3-semibold text-foreground" numberOfLines={1}>
33 {props.source.name ?? "PDF"}
34 </Text>
35 <ControlPill label="Close" variant="pill" onPress={props.onRequestClose} />
36 </View>
37 <PdfPreview uri={props.source.uri} />
38 </View>
39 </Modal>
40 );
41}
42
43function DocumentPreview(props: {
44 readonly source: ResolvedFilePreviewSource;
45 readonly onRequestClose: () => void;
46 readonly onOpenError?: (error: unknown) => void;
47}) {
48 const { uri, name } = props.source;
49 const onRequestClose = useEffectEvent(props.onRequestClose);
50 const onOpenError = useEffectEvent((error: unknown) => {
51 if (props.onOpenError) props.onOpenError(error);
52 else
53 Dialog.alert(
54 "Could not open document",
55 "A compatible viewer must be installed. Check your connection and try again.",
56 );
57 });
58 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- opens the attachment in an external viewer and aborts the open on cleanup
59 useEffect(() => {
60 const controller = new AbortController();
61 const input = {
62 uri,
63 attachment: {
64 name: name ?? "Document",
65 mimeType:
66 props.source.mimeType ??
67 (props.source.kind === "pdf" ? "application/pdf" : "application/octet-stream"),
68 },
69 signal: controller.signal,
70 };
71 const opened = openAttachmentInViewer(input);
72 void opened
73 .catch((error: unknown) => {
74 if (!controller.signal.aborted) onOpenError(error);
75 })
76 .finally(() => {
77 if (!controller.signal.aborted) onRequestClose();
78 });
79 return () => controller.abort();
80 }, [uri, name, props.source.mimeType, props.source.kind]);
81 return (
82 <Modal transparent animationType="fade" onRequestClose={props.onRequestClose}>
83 <View className="flex-1 items-center justify-center bg-backdrop p-6">
84 <View className="w-full max-w-sm gap-4 rounded-2xl bg-sheet-solid p-6">
85 <Text className="font-t3-semibold text-foreground">Opening document…</Text>
86 <Text className="text-foreground-muted" numberOfLines={2}>
87 {name ?? "Document"}
88 </Text>
89 <View className="items-end">
90 <Button label="Cancel" tone="text" onPress={props.onRequestClose} />
91 </View>
92 </View>
93 </View>
94 </Modal>
95 );
96}
97
98export function FilePreview(props: {
99 readonly source: ResolvedFilePreviewSource;
100 readonly onRequestClose: () => void;
101 readonly onOpenError?: (error: unknown) => void;
102}) {
103 if (props.source.kind === "pdf") return <PdfDocumentPreview {...props} />;
104 if (props.source.kind !== "image") return <DocumentPreview {...props} />;
105 if (props.source.actionsSource) return <MediaImagePreview {...props} />;
106 return (
107 <ImageViewing
108 images={
109 props.source.gallery?.map((item) => ({ uri: item.uri })) ?? [{ uri: props.source.uri }]
110 }
111 imageIndex={Math.max(
112 0,
113 props.source.gallery?.findIndex((item) => item.uri === props.source.uri) ?? 0,
114 )}
115 visible
116 onRequestClose={props.onRequestClose}
117 swipeToCloseEnabled={false}
118 doubleTapToZoomEnabled
119 />
120 );
121}