Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useMemo, useState } from "react";
3import { ActivityIndicator, Image, Pressable, View } from "react-native";
4import ImageViewing from "react-native-image-viewing";
5import { AsyncResult } from "effect/unstable/reactivity";
6
7import { AppText as Text } from "../../components/AppText";
8import { EmptyState } from "../../components/EmptyState";
9import { workspaceFileImageAtom } from "./workspace-file-image-cache";
10
11function ResolvedWorkspaceFileImagePreview(props: {
12 readonly accessibilityLabel: string;
13 readonly uri: string;
14}) {
15 const [loadError, setLoadError] = useState<string | null>(null);
16 const [fullScreenVisible, setFullScreenVisible] = useState(false);
17 const imageSource = useMemo(
18 () => ({ uri: props.uri, cache: "force-cache" as const }),
19 [props.uri],
20 );
21 const fullScreenImages = useMemo(() => [imageSource], [imageSource]);
22
23 return (
24 <View className="relative flex-1 bg-subtle">
25 <Pressable
26 accessibilityRole="button"
27 accessibilityLabel={`Open full-screen preview of ${props.accessibilityLabel}`}
28 disabled={loadError !== null}
29 className="flex-1 p-4 active:bg-subtle-strong"
30 onPress={() => setFullScreenVisible(true)}
31 >
32 <Image
33 accessible={false}
34 source={imageSource}
35 className="h-full w-full"
36 resizeMode="contain"
37 onLoadStart={() => setLoadError(null)}
38 onError={(event) => {
39 setLoadError(event.nativeEvent.error || "The image could not be rendered.");
40 }}
41 />
42 </Pressable>
43
44 {loadError !== null ? (
45 <View className="absolute inset-0 items-center justify-center bg-card px-6">
46 <EmptyState title="Image unavailable" detail={loadError} />
47 </View>
48 ) : null}
49
50 <ImageViewing
51 images={fullScreenImages}
52 imageIndex={0}
53 visible={fullScreenVisible}
54 onRequestClose={() => setFullScreenVisible(false)}
55 swipeToCloseEnabled
56 doubleTapToZoomEnabled
57 />
58 </View>
59 );
60}
61
62function CachedWorkspaceFileImagePreview(props: {
63 readonly accessibilityLabel: string;
64 readonly uri: string;
65}) {
66 const imageAtom = useMemo(() => workspaceFileImageAtom(props.uri), [props.uri]);
67 const imageResult = useAtomValue(imageAtom);
68
69 if (AsyncResult.isFailure(imageResult)) {
70 return (
71 <View className="flex-1 items-center justify-center bg-card px-6">
72 <EmptyState
73 title="Image unavailable"
74 detail="The image could not be loaded into the local cache."
75 />
76 </View>
77 );
78 }
79
80 if (!AsyncResult.isSuccess(imageResult)) {
81 return (
82 <View className="flex-1 items-center justify-center gap-3 bg-card px-6">
83 <ActivityIndicator />
84 <Text className="text-center text-sm text-foreground-muted">Loading image...</Text>
85 </View>
86 );
87 }
88
89 return (
90 <ResolvedWorkspaceFileImagePreview
91 accessibilityLabel={props.accessibilityLabel}
92 uri={imageResult.value}
93 />
94 );
95}
96
97export function WorkspaceFileImagePreview(props: {
98 readonly accessibilityLabel: string;
99 readonly uri: string | null;
100}) {
101 if (props.uri === null) {
102 return (
103 <View className="flex-1 items-center justify-center gap-3 bg-card px-6">
104 <ActivityIndicator />
105 <Text className="text-center text-sm text-foreground-muted">
106 Preparing image preview...
107 </Text>
108 </View>
109 );
110 }
111
112 return (
113 <CachedWorkspaceFileImagePreview
114 accessibilityLabel={props.accessibilityLabel}
115 uri={props.uri}
116 />
117 );
118}