The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useGT } from "gt-react-native"; |
| 2 | import { useId, useState } from "react"; |
| 3 | import { Image, Pressable, View } from "react-native"; |
| 4 | |
| 5 | import { FilePreviewLoading } from "./FilePreviewFeedback"; |
| 6 | import { EmptyState } from "../../components/ui/EmptyState"; |
| 7 | import { FilePreviewModal, type FilePreviewSource } from "../../components/ui/FilePreviewModal"; |
| 8 | import { PresentationSource } from "../../components/ui/NativePresentation"; |
| 9 | import { useMediaActions } from "../../state/mediaActions"; |
| 10 | import { type MediaActionsSource } from "../../lib/mediaActionsSource"; |
| 11 | import { MediaActionsMenu } from "../../components/MediaActionsMenu"; |
| 12 | |
| 13 | function ResolvedWorkspaceFileImagePreview(props: { |
| 14 | readonly accessibilityLabel: string; |
| 15 | readonly uri: string; |
| 16 | readonly actionsSource?: MediaActionsSource; |
| 17 | }) { |
| 18 | const gt = useGT(); |
| 19 | const [loadError, setLoadError] = useState<string | null>(null); |
| 20 | const [preview, setPreview] = useState<FilePreviewSource | null>(null); |
| 21 | const sourceIdentifier = useId(); |
| 22 | const mediaActions = useMediaActions(props.actionsSource); |
| 23 | const imageSource = { uri: props.uri, cache: "force-cache" as const }; |
| 24 | |
| 25 | return ( |
| 26 | <View className="relative flex-1 bg-subtle"> |
| 27 | <MediaActionsMenu media={mediaActions} style={{ flex: 1 }}> |
| 28 | <Pressable |
| 29 | accessibilityRole="button" |
| 30 | accessibilityLabel={gt("Open full-screen preview of {name}", { |
| 31 | name: props.accessibilityLabel, |
| 32 | })} |
| 33 | accessibilityHint={ |
| 34 | mediaActions.actions.length > 0 ? gt("Touch and hold for media actions") : undefined |
| 35 | } |
| 36 | disabled={loadError !== null} |
| 37 | className="flex-1 p-4 active:bg-subtle-strong" |
| 38 | onPress={() => |
| 39 | setPreview({ |
| 40 | kind: "image", |
| 41 | uri: props.uri, |
| 42 | name: props.accessibilityLabel, |
| 43 | sourceIdentifier, |
| 44 | actionsSource: props.actionsSource, |
| 45 | }) |
| 46 | } |
| 47 | > |
| 48 | <PresentationSource identifier={sourceIdentifier} style={{ flex: 1 }}> |
| 49 | <Image |
| 50 | accessible={false} |
| 51 | source={imageSource} |
| 52 | className="h-full w-full" |
| 53 | resizeMode="contain" |
| 54 | onLoadStart={() => setLoadError(null)} |
| 55 | onError={(event) => { |
| 56 | setLoadError(event.nativeEvent.error || gt("The image could not be rendered.")); |
| 57 | }} |
| 58 | /> |
| 59 | </PresentationSource> |
| 60 | </Pressable> |
| 61 | </MediaActionsMenu> |
| 62 | {loadError !== null ? ( |
| 63 | <View |
| 64 | pointerEvents="none" |
| 65 | className="absolute inset-0 items-center justify-center bg-card px-6" |
| 66 | > |
| 67 | <EmptyState title={gt("Image unavailable")} detail={loadError} /> |
| 68 | </View> |
| 69 | ) : null} |
| 70 | <FilePreviewModal source={preview} onRequestClose={() => setPreview(null)} /> |
| 71 | </View> |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | export function WorkspaceFileImagePreview(props: { |
| 76 | readonly accessibilityLabel: string; |
| 77 | readonly uri: string | null; |
| 78 | readonly actionsSource?: MediaActionsSource; |
| 79 | }) { |
| 80 | const gt = useGT(); |
| 81 | if (props.uri === null) { |
| 82 | return <FilePreviewLoading message={gt("Preparing image preview...")} background="card" />; |
| 83 | } |
| 84 | |
| 85 | return ( |
| 86 | <ResolvedWorkspaceFileImagePreview |
| 87 | accessibilityLabel={props.accessibilityLabel} |
| 88 | uri={props.uri} |
| 89 | actionsSource={props.actionsSource} |
| 90 | /> |
| 91 | ); |
| 92 | } |