Repository navigation
Expand file tree
/
Copy pathThreadMarkdownImage.tsx
More file actions
222 lines (210 loc) · 7.88 KB
/
Copy pathThreadMarkdownImage.tsx
File metadata and controls
222 lines (210 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
import { createContext, useContext, useEffect, useId, useState } from "react";
import {
ActivityIndicator,
Image,
Pressable,
StyleSheet,
View,
type ViewStyle,
} from "react-native";
import { AppText as Text } from "../../components/AppText";
import type { FilePreviewSource } from "../../components/FilePreviewModal";
import { MediaActionsMenu } from "../../components/MediaActionsMenu";
import { PresentationSource } from "../../components/NativePresentation";
import { useMediaActions } from "../../state/mediaActions";
import { type MediaActionsSource } from "../../lib/mediaActionsSource";
import { useAssetUrlState } from "../../state/assets";
import {
MARKDOWN_IMAGE_MAX_WIDTH,
type MarkdownImageDisplaySize,
resolveMarkdownImageDisplaySize,
} from "./markdownImageSize";
/**
* Width the feed lays markdown out in. The feed already knows this from its
* viewport, so an image can size its frame on the first render instead of
* waiting for its own onLayout, which would change the row's height once
* more after the list has positioned the rows below it. It is an upper
* bound: a list item or blockquote indents its column, and the measured
* width takes over once it is known.
*/
export const MarkdownImageAvailableWidthContext = createContext(0);
export function ThreadMarkdownImageView(props: {
readonly uri: string | null;
readonly sourceKey: string;
readonly unavailable: boolean;
readonly alt: string | null;
/** Pixel size from the server, when it could read the header; the frame is final from the first render. */
readonly knownSize?: { readonly width: number; readonly height: number } | undefined;
readonly actionsSource?: MediaActionsSource;
readonly onPressPreview: (source: FilePreviewSource) => void;
}) {
const sourceIdentifier = useId();
const mediaActions = useMediaActions(props.actionsSource);
const contextWidth = useContext(MarkdownImageAvailableWidthContext);
const [measuredWidth, setMeasuredWidth] = useState(0);
const availableWidth =
measuredWidth > 0 && contextWidth > 0
? Math.min(contextWidth, measuredWidth)
: contextWidth || measuredWidth;
const [decodedSize, setDecodedSize] = useState<{ width: number; height: number } | null>(null);
const [failedUri, setFailedUri] = useState<string | null>(null);
useEffect(() => {
setDecodedSize(null);
}, [props.sourceKey]);
useEffect(() => {
setFailedUri(null);
}, [props.uri]);
// The decoded size is what the platform actually drew, so it wins over the
// server's header hint once it exists.
const sourceSize = decodedSize ?? props.knownSize ?? null;
const displaySize: MarkdownImageDisplaySize | null =
sourceSize === null || availableWidth <= 0
? null
: resolveMarkdownImageDisplaySize({
sourceWidth: sourceSize.width,
sourceHeight: sourceSize.height,
availableWidth,
});
const failed = props.unavailable || (props.uri !== null && failedUri === props.uri);
const placeholderWidth: ViewStyle["width"] =
availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";
const frameStyle: ViewStyle = displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 };
return (
<View
onLayout={(event) => setMeasuredWidth(event.nativeEvent.layout.width)}
style={{ alignSelf: "stretch", gap: 6 }}
>
{props.uri === null || failed ? (
<MediaActionsMenu media={mediaActions}>
<Pressable
accessibilityRole="imagebutton"
accessibilityLabel={props.alt ?? "Markdown image"}
accessibilityHint={
mediaActions.actions.length > 0 ? "Touch and hold for media actions" : undefined
}
className="items-center justify-center rounded-[10px] bg-md-code-bg"
style={frameStyle}
>
{failed ? (
<Text className="text-xs text-foreground-muted">Image unavailable</Text>
) : (
<ActivityIndicator />
)}
</Pressable>
</MediaActionsMenu>
) : (
<PresentationSource identifier={sourceIdentifier} style={{ alignSelf: "flex-start" }}>
<MediaActionsMenu media={mediaActions}>
<Pressable
accessibilityRole="imagebutton"
accessibilityLabel={props.alt ?? "Markdown image"}
accessibilityHint={
mediaActions.actions.length > 0 ? "Touch and hold for media actions" : undefined
}
onPress={() =>
props.onPressPreview({
kind: "image",
uri: props.uri!,
name: props.actionsSource?.name ?? props.alt ?? "Image",
sourceIdentifier,
actionsSource: props.actionsSource,
})
}
style={{ alignSelf: "flex-start" }}
>
<View
className="items-center justify-center overflow-hidden rounded-[10px] bg-md-code-bg"
style={frameStyle}
>
<ThreadMarkdownImageRequest
key={props.uri}
uri={props.uri}
onLoad={setDecodedSize}
onError={() => setFailedUri(props.uri)}
/>
</View>
</Pressable>
</MediaActionsMenu>
</PresentationSource>
)}
{props.alt ? (
<Text selectable className="text-xs text-foreground-muted">
{props.alt}
</Text>
) : null}
</View>
);
}
function ThreadMarkdownImageRequest(props: {
readonly uri: string;
readonly onLoad: (sourceSize: { width: number; height: number }) => void;
readonly onError: () => void;
}) {
const [loaded, setLoaded] = useState(false);
return (
<>
<Image
source={{ uri: props.uri }}
resizeMode="contain"
accessible={false}
onLoad={(event) => {
setLoaded(true);
props.onLoad(event.nativeEvent.source);
}}
onError={props.onError}
style={{ width: "100%", height: "100%", opacity: loaded ? 1 : 0 }}
/>
{loaded ? null : (
<View
pointerEvents="none"
style={[StyleSheet.absoluteFill, { alignItems: "center", justifyContent: "center" }]}
>
<Text className="text-xs text-foreground-muted">Loading image…</Text>
</View>
)}
</>
);
}
/** Environment-hosted image that loads through a signed asset URL. */
export function ThreadMarkdownImage(props: {
readonly environmentId: EnvironmentId;
readonly resource: Extract<
AssetResource,
{ readonly _tag: "attachment" | "media-file" | "tool-output-image" }
>;
readonly alt: string | null;
readonly srcFragment?: string;
readonly actionsSource?: MediaActionsSource;
readonly onPressPreview: (source: FilePreviewSource) => void;
}) {
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
return (
<ThreadMarkdownImageView
uri={assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null}
sourceKey={
props.resource._tag === "attachment"
? `attachment:${props.resource.attachmentId}`
: props.resource._tag === "tool-output-image"
? `tool-output:${props.resource.itemId}:${props.resource.index}`
: `workspace:${props.resource.path}`
}
unavailable={assetUrl._tag === "Failure"}
knownSize={assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined}
alt={props.alt}
actionsSource={props.actionsSource}
onPressPreview={props.onPressPreview}
/>
);
}
export function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {
return (
<ThreadMarkdownImageView
uri={null}
sourceKey="unavailable"
unavailable
alt={props.alt}
onPressPreview={() => undefined}
/>
);
}