The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Dialog from "./Dialog"; |
| 2 | import { requireNativeModule } from "expo"; |
| 3 | import { msg, useGT } from "gt-react-native"; |
| 4 | import { useEffect, useEffectEvent, useId, useRef, useState } from "react"; |
| 5 | |
| 6 | import { m } from "../../i18n/messages"; |
| 7 | import type { |
| 8 | FilePreviewImage, |
| 9 | FilePreviewModalProps, |
| 10 | FilePreviewViewerProps, |
| 11 | } from "./FilePreviewModal.types"; |
| 12 | import { FilePreviewSession } from "./FilePreviewSession"; |
| 13 | |
| 14 | export type { |
| 15 | FilePreviewItem, |
| 16 | FilePreviewSource, |
| 17 | ResolvedFilePreviewSource, |
| 18 | } from "./FilePreviewModal.types"; |
| 19 | |
| 20 | const NativeControls = requireNativeModule<{ |
| 21 | presentFile( |
| 22 | uri: string, |
| 23 | name: string, |
| 24 | sourceIdentifier: string, |
| 25 | identifier: string, |
| 26 | ): Promise<void>; |
| 27 | presentFiles( |
| 28 | uris: readonly (string | null)[], |
| 29 | names: readonly string[], |
| 30 | selectedIndex: number, |
| 31 | sourceIdentifiers: readonly string[], |
| 32 | identifier: string, |
| 33 | ): Promise<void>; |
| 34 | updateFile(identifier: string, index: number, uri: string | null): Promise<void>; |
| 35 | dismissFile(identifier: string): Promise<void>; |
| 36 | isFilePreviewFinished(identifier: string): boolean; |
| 37 | }>("T3NativeControls"); |
| 38 | |
| 39 | // Null tells the viewer an image failed; undefined means its URL has not arrived yet. |
| 40 | const nativeUri = (image: FilePreviewImage) => (image.failed ? null : image.uri); |
| 41 | |
| 42 | function NativeFilePreview(props: FilePreviewViewerProps) { |
| 43 | const gt = useGT(); |
| 44 | const identifier = useId(); |
| 45 | // Later URLs reach the native presentation through updateFile instead of presenting again. |
| 46 | const [presented] = useState(props.source); |
| 47 | const delivered = useRef<(string | null | undefined)[]>([]); |
| 48 | const onRequestClose = useEffectEvent(props.onRequestClose); |
| 49 | const onPresent = useEffectEvent((isFinished: () => boolean) => props.onPresent?.(isFinished)); |
| 50 | const onOpenError = useEffectEvent((error: unknown) => { |
| 51 | if (props.onOpenError) props.onOpenError(error); |
| 52 | else |
| 53 | Dialog.alert( |
| 54 | gt("Could not open preview"), |
| 55 | gt("The file could not be loaded. Please try again."), |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- presents the native file preview and dismisses it on cleanup |
| 60 | useEffect(() => { |
| 61 | let canceled = false; |
| 62 | let presentation: Promise<void>; |
| 63 | // Images hand over before every URL resolves; other files must download first so Quick Look can refuse them. |
| 64 | if (presented.kind === "image") { |
| 65 | const { images, selectedIndex } = presented; |
| 66 | delivered.current = images.map((image) => image.uri); |
| 67 | presentation = NativeControls.presentFiles( |
| 68 | images.map((image) => image.uri ?? null), |
| 69 | images.map( |
| 70 | (image) => image.name ?? m(msg("Preview", { $context: "noun, preview window title" })), |
| 71 | ), |
| 72 | selectedIndex, |
| 73 | images.map((image) => image.sourceIdentifier ?? ""), |
| 74 | identifier, |
| 75 | ); |
| 76 | } else { |
| 77 | const { uri, name, sourceIdentifier } = presented; |
| 78 | presentation = NativeControls.presentFile( |
| 79 | uri, |
| 80 | name ?? m(msg("Preview", { $context: "noun, preview window title" })), |
| 81 | sourceIdentifier ?? "", |
| 82 | identifier, |
| 83 | ); |
| 84 | } |
| 85 | onPresent(() => NativeControls.isFilePreviewFinished(identifier)); |
| 86 | void presentation |
| 87 | .catch((error: unknown) => { |
| 88 | if (!canceled) onOpenError(error); |
| 89 | }) |
| 90 | .finally(() => { |
| 91 | if (!canceled) onRequestClose(); |
| 92 | }); |
| 93 | return () => { |
| 94 | canceled = true; |
| 95 | void NativeControls.dismissFile(identifier).catch(() => undefined); |
| 96 | }; |
| 97 | }, [presented, identifier]); |
| 98 | |
| 99 | const images = props.source.kind === "image" ? props.source.images : undefined; |
| 100 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hands image URLs that resolve after opening to the native viewer |
| 101 | useEffect(() => { |
| 102 | images?.forEach((image, index) => { |
| 103 | const uri = nativeUri(image); |
| 104 | if (uri === undefined || delivered.current[index] === uri) return; |
| 105 | delivered.current[index] = uri; |
| 106 | void NativeControls.updateFile(identifier, index, uri).catch(() => undefined); |
| 107 | }); |
| 108 | }, [images, identifier]); |
| 109 | |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | /** Shows images, PDFs and documents full screen in the native viewer (Android: FilePreviewModal.android.tsx). */ |
| 114 | export function FilePreviewModal(props: FilePreviewModalProps) { |
| 115 | return <FilePreviewSession {...props} progressiveGallery Viewer={NativeFilePreview} />; |
| 116 | } |