The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { T, useGT } from "gt-react-native"; |
| 2 | |
| 3 | import { Button } from "./Button"; |
| 4 | import * as Dialog from "./Dialog"; |
| 5 | import { createContext, useContext, useEffect, useEffectEvent } from "react"; |
| 6 | import { Modal, View } from "react-native"; |
| 7 | import ImageViewing from "react-native-image-viewing"; |
| 8 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 9 | |
| 10 | import { openAttachmentInViewer } from "../../lib/attachmentDownload"; |
| 11 | import { useMediaActions } from "../../state/mediaActions"; |
| 12 | import type { |
| 13 | FilePreviewImages, |
| 14 | FilePreviewModalProps, |
| 15 | FilePreviewViewerProps, |
| 16 | ResolvedFilePreviewDocument, |
| 17 | } from "./FilePreviewModal.types"; |
| 18 | import { FilePreviewSession } from "./FilePreviewSession"; |
| 19 | import { MediaPreviewHeader } from "../MediaPreviewHeader"; |
| 20 | |
| 21 | export type { |
| 22 | FilePreviewItem, |
| 23 | FilePreviewSource, |
| 24 | ResolvedFilePreviewSource, |
| 25 | } from "./FilePreviewModal.types"; |
| 26 | import { PdfPreview } from "../PdfPreview"; |
| 27 | import { SheetHeader } from "./SheetHeader"; |
| 28 | import { AppText as Text } from "./AppText"; |
| 29 | |
| 30 | function PdfDocumentPreview(props: { |
| 31 | readonly source: ResolvedFilePreviewDocument; |
| 32 | readonly onRequestClose: () => void; |
| 33 | }) { |
| 34 | const gt = useGT(); |
| 35 | const insets = useSafeAreaInsets(); |
| 36 | const media = useMediaActions(props.source.actionsSource); |
| 37 | const save = media.actions.find((action) => action.id === "save"); |
| 38 | return ( |
| 39 | <Modal |
| 40 | animationType="slide" |
| 41 | onRequestClose={props.onRequestClose} |
| 42 | statusBarTranslucent |
| 43 | navigationBarTranslucent |
| 44 | > |
| 45 | <View |
| 46 | className="flex-1 bg-card" |
| 47 | style={{ paddingTop: insets.top, paddingBottom: insets.bottom }} |
| 48 | > |
| 49 | <SheetHeader |
| 50 | presentation="inline" |
| 51 | title={props.source.name ?? gt("PDF")} |
| 52 | onClose={props.onRequestClose} |
| 53 | actions={ |
| 54 | save |
| 55 | ? [ |
| 56 | { |
| 57 | accessibilityLabel: gt("Save or share"), |
| 58 | icon: "square.and.arrow.up", |
| 59 | disabled: save.disabled, |
| 60 | loading: media.sharing, |
| 61 | onPress: () => void save.run(), |
| 62 | }, |
| 63 | ] |
| 64 | : [] |
| 65 | } |
| 66 | /> |
| 67 | <PdfPreview uri={props.source.uri} /> |
| 68 | </View> |
| 69 | </Modal> |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | function DocumentPreview(props: { |
| 74 | readonly source: ResolvedFilePreviewDocument; |
| 75 | readonly onRequestClose: () => void; |
| 76 | readonly onOpenError?: (error: unknown) => void; |
| 77 | }) { |
| 78 | const gt = useGT(); |
| 79 | const { uri, name } = props.source; |
| 80 | const onRequestClose = useEffectEvent(props.onRequestClose); |
| 81 | const onOpenError = useEffectEvent((error: unknown) => { |
| 82 | if (props.onOpenError) props.onOpenError(error); |
| 83 | else |
| 84 | Dialog.alert( |
| 85 | gt("Could not open document"), |
| 86 | gt("A compatible viewer must be installed. Check your connection and try again."), |
| 87 | ); |
| 88 | }); |
| 89 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- opens the attachment in an external viewer and aborts the open on cleanup |
| 90 | useEffect(() => { |
| 91 | const controller = new AbortController(); |
| 92 | const input = { |
| 93 | uri, |
| 94 | attachment: { |
| 95 | name: name ?? "Document", |
| 96 | mimeType: |
| 97 | props.source.mimeType ?? |
| 98 | (props.source.kind === "pdf" ? "application/pdf" : "application/octet-stream"), |
| 99 | }, |
| 100 | signal: controller.signal, |
| 101 | }; |
| 102 | const opened = openAttachmentInViewer(input); |
| 103 | void opened |
| 104 | .catch((error: unknown) => { |
| 105 | if (!controller.signal.aborted) onOpenError(error); |
| 106 | }) |
| 107 | .finally(() => { |
| 108 | if (!controller.signal.aborted) onRequestClose(); |
| 109 | }); |
| 110 | return () => controller.abort(); |
| 111 | }, [uri, name, props.source.mimeType, props.source.kind]); |
| 112 | return ( |
| 113 | <Modal transparent animationType="fade" onRequestClose={props.onRequestClose}> |
| 114 | <View className="flex-1 items-center justify-center bg-backdrop p-6"> |
| 115 | <View className="w-full max-w-sm gap-4 rounded-2xl bg-sheet-solid p-6"> |
| 116 | <Text className="font-t3-semibold text-foreground"> |
| 117 | <T>Opening document…</T> |
| 118 | </Text> |
| 119 | <Text className="text-foreground-muted" numberOfLines={2}> |
| 120 | {name ?? gt("Document")} |
| 121 | </Text> |
| 122 | <View className="items-end"> |
| 123 | <Button label={gt("Cancel")} tone="text" onPress={props.onRequestClose} /> |
| 124 | </View> |
| 125 | </View> |
| 126 | </View> |
| 127 | </Modal> |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | type ImagePreviewProps = { |
| 132 | readonly source: FilePreviewImages; |
| 133 | readonly onRequestClose: () => void; |
| 134 | }; |
| 135 | |
| 136 | const ImagePreviewContext = createContext<ImagePreviewProps | null>(null); |
| 137 | |
| 138 | function ImagePreviewHeader(header: { readonly imageIndex: number }) { |
| 139 | const props = useContext(ImagePreviewContext)!; |
| 140 | const source = props.source.images[header.imageIndex]; |
| 141 | const gt = useGT(); |
| 142 | const insets = useSafeAreaInsets(); |
| 143 | const mediaActions = useMediaActions(source?.actionsSource, props.onRequestClose); |
| 144 | return ( |
| 145 | <View className="bg-black/70" style={{ paddingTop: insets.top }}> |
| 146 | <MediaPreviewHeader |
| 147 | title={source?.name ?? gt("Image")} |
| 148 | media={mediaActions} |
| 149 | closeAccessibilityLabel={gt("Close image")} |
| 150 | onClose={props.onRequestClose} |
| 151 | /> |
| 152 | </View> |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | // Images still resolving show the viewer's loading page, and remount by key once their URL arrives. |
| 157 | function imageViewingProps(source: FilePreviewImages) { |
| 158 | return { |
| 159 | images: source.images.map((image) => (image.uri ? { uri: image.uri } : {})), |
| 160 | keyExtractor: (_: unknown, index: number) => source.images[index]?.uri ?? `pending:${index}`, |
| 161 | // Stays fixed: react-native-image-viewing remounts the Modal and replays its fade when it changes. |
| 162 | imageIndex: source.selectedIndex, |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | function ImagePreview(props: ImagePreviewProps) { |
| 167 | return ( |
| 168 | <ImagePreviewContext value={props}> |
| 169 | <ImageViewing |
| 170 | {...imageViewingProps(props.source)} |
| 171 | visible |
| 172 | presentationStyle="fullScreen" |
| 173 | onRequestClose={props.onRequestClose} |
| 174 | swipeToCloseEnabled={false} |
| 175 | doubleTapToZoomEnabled |
| 176 | HeaderComponent={ImagePreviewHeader} |
| 177 | /> |
| 178 | </ImagePreviewContext> |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | function AndroidFilePreview(props: FilePreviewViewerProps) { |
| 183 | const { source } = props; |
| 184 | if (source.kind === "pdf") return <PdfDocumentPreview {...props} source={source} />; |
| 185 | if (source.kind !== "image") return <DocumentPreview {...props} source={source} />; |
| 186 | const selected = source.images[source.selectedIndex]!; |
| 187 | const images = source.images.filter((image) => !image.failed); |
| 188 | const gallery = { ...source, images, selectedIndex: images.indexOf(selected) }; |
| 189 | if (selected.actionsSource) |
| 190 | return <ImagePreview source={gallery} onRequestClose={props.onRequestClose} />; |
| 191 | return ( |
| 192 | <ImageViewing |
| 193 | {...imageViewingProps(gallery)} |
| 194 | visible |
| 195 | onRequestClose={props.onRequestClose} |
| 196 | swipeToCloseEnabled={false} |
| 197 | doubleTapToZoomEnabled |
| 198 | /> |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | /** Shows images, PDFs and documents full screen. */ |
| 203 | export function FilePreviewModal(props: FilePreviewModalProps) { |
| 204 | return <FilePreviewSession {...props} progressiveGallery={false} Viewer={AndroidFilePreview} />; |
| 205 | } |