Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FilePreview.tsx

120 lines4.1 KB

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 );
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 useEffect(() => {
59 const controller = new AbortController();
60 const input = {
61 uri,
62 attachment: {
63 name: name ?? "Document",
64 mimeType:
65 props.source.mimeType ??
66 (props.source.kind === "pdf" ? "application/pdf" : "application/octet-stream"),
67 },
68 signal: controller.signal,
69 };
70 const opened = openAttachmentInViewer(input);
71 void opened
72 .catch((error: unknown) => {
73 if (!controller.signal.aborted) onOpenError(error);
74 })
75 .finally(() => {
76 if (!controller.signal.aborted) onRequestClose();
77 });
78 return () => controller.abort();
79 }, [uri, name, props.source.mimeType, props.source.kind]);
80 return (
81 <Modal transparent animationType="fade" onRequestClose={props.onRequestClose}>
82 <View className="flex-1 items-center justify-center bg-backdrop p-6">
83 <View className="w-full max-w-sm gap-4 rounded-2xl bg-sheet-solid p-6">
84 <Text className="font-t3-semibold text-foreground">Opening document…</Text>
85 <Text className="text-foreground-muted" numberOfLines={2}>
86 {name ?? "Document"}
87 </Text>
88 <View className="items-end">
89 <Button label="Cancel" tone="text" onPress={props.onRequestClose} />
90 </View>
91 </View>
92 </View>
93 </Modal>
94 );
96​
97export function FilePreview(props: {
98 readonly source: ResolvedFilePreviewSource;
99 readonly onRequestClose: () => void;
100 readonly onOpenError?: (error: unknown) => void;
101}) {
102 if (props.source.kind === "pdf") return <PdfDocumentPreview {...props} />;
103 if (props.source.kind !== "image") return <DocumentPreview {...props} />;
104 if (props.source.actionsSource) return <MediaImagePreview {...props} />;
105 return (
106 <ImageViewing
107 images={
108 props.source.gallery?.map((item) => ({ uri: item.uri })) ?? [{ uri: props.source.uri }]
109 }
110 imageIndex={Math.max(
111 0,
112 props.source.gallery?.findIndex((item) => item.uri === props.source.uri) ?? 0,
113 )}
114 visible
115 onRequestClose={props.onRequestClose}
116 swipeToCloseEnabled={false}
117 doubleTapToZoomEnabled
118 />
119 );