The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { useAtomRefresh, useAtomValue } from "@effect/atom-react"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import { Atom, AsyncResult } from "effect/reactivity"; |
| 6 | import { msg, useGT } from "gt-react-native"; |
| 7 | import { useState } from "react"; |
| 8 | import { View } from "react-native"; |
| 9 | import Pdf from "react-native-pdf-jsi"; |
| 10 | |
| 11 | import { FilePreviewLoading, FilePreviewNotice } from "../features/files/FilePreviewFeedback"; |
| 12 | import { pdfInitialPage, pdfLinkUrl, preparePdfPreview } from "../lib/pdfPreview"; |
| 13 | import { m } from "../i18n/messages"; |
| 14 | import { tryOpenExternalUrl } from "../lib/openExternalUrl"; |
| 15 | import { ControlPill } from "./ui/ControlPill"; |
| 16 | |
| 17 | const preparedPdfAtom = Atom.family((uri: string) => |
| 18 | Atom.make( |
| 19 | // The downloaded copy lives as long as a viewer reads it; Retry refreshes the atom, |
| 20 | // which closes the scope and discards the copy before downloading again. Acquisition |
| 21 | // stays interruptible so closing the viewer aborts the download. |
| 22 | Effect.acquireRelease( |
| 23 | Effect.tryPromise({ |
| 24 | try: (signal) => { |
| 25 | const prepared = preparePdfPreview(uri, signal); |
| 26 | // An interrupt can land after the copy exists but before acquireRelease owns it. |
| 27 | signal.addEventListener("abort", () => { |
| 28 | void prepared.then( |
| 29 | (file) => file?.dispose(), |
| 30 | () => undefined, |
| 31 | ); |
| 32 | }); |
| 33 | return prepared; |
| 34 | }, |
| 35 | catch: (cause) => |
| 36 | cause instanceof Error |
| 37 | ? (errorDisplayMessage(cause, m) ?? cause.message) |
| 38 | : m(msg("The PDF could not be loaded.")), |
| 39 | }).pipe( |
| 40 | Effect.filterOrElse( |
| 41 | (file) => file !== null, |
| 42 | () => Effect.interrupt, |
| 43 | ), |
| 44 | Effect.interruptible, |
| 45 | ), |
| 46 | (file) => Effect.sync(file.dispose), |
| 47 | ), |
| 48 | ).pipe(Atom.withLabel(`android-pdf-preview:${uri}`)), |
| 49 | ); |
| 50 | |
| 51 | export function PdfPreview(props: { readonly uri: string }) { |
| 52 | const gt = useGT(); |
| 53 | const atom = preparedPdfAtom(props.uri); |
| 54 | const prepared = useAtomValue(atom); |
| 55 | const refresh = useAtomRefresh(atom); |
| 56 | // A render failure belongs to the downloaded copy that failed; Retry replaces it. |
| 57 | const [unrenderable, setUnrenderable] = useState<unknown>(null); |
| 58 | const file = prepared._tag === "Success" && !prepared.waiting ? prepared.value : null; |
| 59 | const error = |
| 60 | file !== null && unrenderable === file |
| 61 | ? gt("The PDF could not be rendered. It may be damaged or password protected.") |
| 62 | : prepared._tag === "Failure" && !prepared.waiting |
| 63 | ? Option.getOrElse(AsyncResult.error(prepared), () => gt("The PDF could not be loaded.")) |
| 64 | : null; |
| 65 | const localUri = file?.uri ?? null; |
| 66 | |
| 67 | if (error) { |
| 68 | return ( |
| 69 | <View className="flex-1 gap-4 bg-card"> |
| 70 | <FilePreviewNotice title={gt("Could not load PDF")}>{error}</FilePreviewNotice> |
| 71 | <View className="items-center"> |
| 72 | <ControlPill label={gt("Try again")} variant="pill" onPress={refresh} /> |
| 73 | </View> |
| 74 | </View> |
| 75 | ); |
| 76 | } |
| 77 | if (localUri === null) |
| 78 | return <FilePreviewLoading message={gt("Loading PDF...")} background="card" />; |
| 79 | |
| 80 | return ( |
| 81 | |
| 82 | source={{ uri: localUri }} |
| 83 | initialPage={pdfInitialPage(props.uri) - 1} |
| 84 | enableAnnotationRendering |
| 85 | enableDoubleTapZoom |
| 86 | onLinkPress={({ url: href }) => { |
| 87 | const url = pdfLinkUrl(href, props.uri); |
| 88 | if (url !== null) void tryOpenExternalUrl(url, "file-preview"); |
| 89 | }} |
| 90 | onError={() => { |
| 91 | setUnrenderable(file); |
| 92 | }} |
| 93 | style={{ flex: 1, width: "100%" }} |
| 94 | /> |
| 95 | ); |
| 96 | } |