Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceFileImagePreview.tsx

121 lines3.8 KB

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