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