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

WorkspaceFileImagePreview.tsx

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