Skip to content

Commit 01354d4

Browse files
committed
fix(mobile): render workspace images in file previews
1 parent 2daff8c commit 01354d4

5 files changed

Lines changed: 388 additions & 157 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { useEffect, useState } from "react";
3+
import { ActivityIndicator, Image, StyleSheet, View, type ViewStyle } from "react-native";
4+
import { TouchableOpacity } from "react-native-gesture-handler";
5+
6+
import { useAssetUrlState } from "../state/assets";
7+
import {
8+
MARKDOWN_IMAGE_MAX_WIDTH,
9+
resolveMarkdownImageDisplaySize,
10+
} from "../features/threads/markdownImageSize";
11+
import { AppText as Text } from "./AppText";
12+
13+
export function ThreadMarkdownImageView(props: {
14+
readonly uri: string | null;
15+
readonly sourceKey: string;
16+
readonly unavailable: boolean;
17+
readonly alt: string | null;
18+
readonly onPressImage?: ((uri: string) => void) | undefined;
19+
}) {
20+
const [availableWidth, setAvailableWidth] = useState(0);
21+
const [sourceSize, setSourceSize] = useState<{ width: number; height: number } | null>(null);
22+
const [failedUri, setFailedUri] = useState<string | null>(null);
23+
24+
useEffect(() => {
25+
setSourceSize(null);
26+
}, [props.sourceKey]);
27+
28+
useEffect(() => {
29+
setFailedUri(null);
30+
}, [props.uri]);
31+
32+
const displaySize =
33+
sourceSize === null
34+
? null
35+
: resolveMarkdownImageDisplaySize({
36+
sourceWidth: sourceSize.width,
37+
sourceHeight: sourceSize.height,
38+
availableWidth,
39+
});
40+
const failed = props.unavailable || (props.uri !== null && failedUri === props.uri);
41+
const placeholderWidth: ViewStyle["width"] =
42+
availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";
43+
const frameStyle: ViewStyle = displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 };
44+
45+
const image =
46+
props.uri === null || failed ? (
47+
<View
48+
className="items-center justify-center rounded-[10px] bg-md-code-bg"
49+
style={{
50+
...frameStyle,
51+
}}
52+
>
53+
{failed ? (
54+
<Text className="text-xs text-foreground-muted">Image unavailable</Text>
55+
) : (
56+
<ActivityIndicator />
57+
)}
58+
</View>
59+
) : (
60+
<View
61+
className="items-center justify-center overflow-hidden rounded-[10px] bg-md-code-bg"
62+
style={{
63+
...frameStyle,
64+
}}
65+
>
66+
<ThreadMarkdownImageRequest
67+
key={props.uri}
68+
uri={props.uri}
69+
onLoad={setSourceSize}
70+
onError={() => setFailedUri(props.uri)}
71+
/>
72+
</View>
73+
);
74+
75+
return (
76+
<View
77+
onLayout={(event) => setAvailableWidth(event.nativeEvent.layout.width)}
78+
style={{ alignSelf: "stretch", gap: 6 }}
79+
>
80+
{props.uri !== null && !failed && props.onPressImage ? (
81+
<TouchableOpacity
82+
accessibilityRole="imagebutton"
83+
accessibilityLabel={props.alt ?? "Markdown image"}
84+
activeOpacity={0.7}
85+
onPress={() => props.onPressImage?.(props.uri!)}
86+
style={{ alignSelf: "flex-start" }}
87+
>
88+
{image}
89+
</TouchableOpacity>
90+
) : (
91+
image
92+
)}
93+
{props.alt ? (
94+
<Text selectable className="text-xs text-foreground-muted">
95+
{props.alt}
96+
</Text>
97+
) : null}
98+
</View>
99+
);
100+
}
101+
102+
function ThreadMarkdownImageRequest(props: {
103+
readonly uri: string;
104+
readonly onLoad: (sourceSize: { width: number; height: number }) => void;
105+
readonly onError: () => void;
106+
}) {
107+
const [loaded, setLoaded] = useState(false);
108+
109+
return (
110+
<>
111+
<Image
112+
source={{ uri: props.uri }}
113+
resizeMode="contain"
114+
accessible={false}
115+
onLoad={(event) => {
116+
setLoaded(true);
117+
props.onLoad(event.nativeEvent.source);
118+
}}
119+
onError={props.onError}
120+
style={{ width: "100%", height: "100%", opacity: loaded ? 1 : 0 }}
121+
/>
122+
{loaded ? null : (
123+
<View
124+
pointerEvents="none"
125+
style={[StyleSheet.absoluteFill, { alignItems: "center", justifyContent: "center" }]}
126+
>
127+
<Text className="text-xs text-foreground-muted">Loading image…</Text>
128+
</View>
129+
)}
130+
</>
131+
);
132+
}
133+
134+
/** Markdown image whose src is a workspace file. It loads through a signed asset URL. */
135+
export function ThreadMarkdownImage(props: {
136+
readonly environmentId: EnvironmentId;
137+
readonly threadId: ThreadId;
138+
readonly path: string;
139+
readonly alt: string | null;
140+
readonly onPressImage?: ((uri: string) => void) | undefined;
141+
}) {
142+
const assetUrl = useAssetUrlState(props.environmentId, {
143+
_tag: "workspace-file",
144+
threadId: props.threadId,
145+
path: props.path,
146+
});
147+
148+
return (
149+
<ThreadMarkdownImageView
150+
uri={assetUrl._tag === "Success" ? assetUrl.url : null}
151+
sourceKey={props.path}
152+
unavailable={assetUrl._tag === "Failure"}
153+
alt={props.alt}
154+
onPressImage={props.onPressImage}
155+
/>
156+
);
157+
}
158+
159+
export function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {
160+
return <ThreadMarkdownImageView uri={null} sourceKey="unavailable" unavailable alt={props.alt} />;
161+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { isValidElement, type ReactElement, type ReactNode } from "react";
3+
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4+
5+
const testState = vi.hoisted(() => ({
6+
useNativeRenderer: false,
7+
resources: [] as Array<unknown>,
8+
}));
9+
10+
vi.mock("react", async (importOriginal) => ({
11+
...(await importOriginal<typeof import("react")>()),
12+
useCallback: <Value,>(value: Value) => value,
13+
useEffect: () => {},
14+
useMemo: <Value,>(factory: () => Value) => factory(),
15+
useState: <Value,>(initial: Value | (() => Value)) => [
16+
typeof initial === "function" ? (initial as () => Value)() : initial,
17+
vi.fn(),
18+
],
19+
}));
20+
vi.mock("react-native", () => ({
21+
ActivityIndicator: "ActivityIndicator",
22+
Image: "Image",
23+
RefreshControl: "RefreshControl",
24+
ScrollView: "ScrollView",
25+
StyleSheet: { absoluteFill: {} },
26+
Text: "Text",
27+
View: "View",
28+
}));
29+
vi.mock("react-native-gesture-handler", () => ({ TouchableOpacity: "TouchableOpacity" }));
30+
vi.mock("react-native-nitro-markdown", () => ({
31+
Markdown: (props: {
32+
readonly children: string;
33+
readonly renderers: {
34+
readonly image: (input: {
35+
readonly node: { readonly href: string; readonly alt: string; readonly title: null };
36+
}) => ReactNode;
37+
};
38+
}) => {
39+
const image = markdownImage(props.children);
40+
return image === null ? null : props.renderers.image({ node: image });
41+
},
42+
}));
43+
vi.mock("../../components/AppText", () => ({ AppText: "Text" }));
44+
vi.mock("../../lib/openExternalUrl", () => ({ tryOpenExternalUrl: vi.fn() }));
45+
vi.mock("../../lib/useFontFamily", () => ({ useFontFamily: () => "System" }));
46+
vi.mock("../../lib/useUniwindTheme", () => ({
47+
useUniwindTheme: () => new Proxy<Record<string, string>>({}, { get: () => "#000000" }),
48+
}));
49+
vi.mock("../../native/SelectableMarkdownText", () => ({
50+
hasNativeSelectableMarkdownText: () => testState.useNativeRenderer,
51+
SelectableMarkdownText: (props: {
52+
readonly markdown: string;
53+
readonly renderImage: (image: {
54+
readonly href: string;
55+
readonly alt: string | null;
56+
readonly title: string | null;
57+
}) => ReactNode;
58+
}) => {
59+
const image = markdownImage(props.markdown);
60+
return image === null ? null : props.renderImage(image);
61+
},
62+
}));
63+
vi.mock("../../state/assets", () => ({
64+
useAssetUrlState: (_environmentId: unknown, resource: unknown) => {
65+
testState.resources.push(resource);
66+
return { _tag: "Success", url: "https://signed.test/diagram.png" };
67+
},
68+
}));
69+
vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
70+
useAppearancePreferences: () => ({ appearance: { baseFontSize: 14 } }),
71+
}));
72+
73+
import { FileMarkdownPreview } from "./FileMarkdownPreview";
74+
75+
function markdownImage(markdown: string): {
76+
readonly href: string;
77+
readonly alt: string;
78+
readonly title: null;
79+
} | null {
80+
const match = /!\[([^\]]*)]\(([^)]+)\)/.exec(markdown);
81+
return match?.[2] ? { href: match[2], alt: match[1] ?? "", title: null } : null;
82+
}
83+
84+
function renderTree(node: ReactNode): void {
85+
if (Array.isArray(node)) {
86+
node.forEach(renderTree);
87+
return;
88+
}
89+
if (!isValidElement(node)) return;
90+
const element = node as ReactElement<Record<string, unknown>>;
91+
if (typeof element.type === "function") {
92+
const render = element.type as (props: Record<string, unknown>) => ReactNode;
93+
renderTree(render(element.props));
94+
return;
95+
}
96+
renderTree(element.props.children as ReactNode);
97+
}
98+
99+
function renderPreview(input: {
100+
readonly cwd: string;
101+
readonly relativePath: string;
102+
readonly markdown: string;
103+
}): void {
104+
renderTree(
105+
FileMarkdownPreview({
106+
cwd: input.cwd,
107+
relativePath: input.relativePath,
108+
markdown: input.markdown,
109+
environmentId: EnvironmentId.make("environment-1"),
110+
threadId: ThreadId.make("thread-1"),
111+
}),
112+
);
113+
}
114+
115+
describe.each([
116+
["native iOS", true],
117+
["JavaScript fallback", false],
118+
])("FileMarkdownPreview workspace images with the %s renderer", (_label, useNativeRenderer) => {
119+
beforeEach(() => {
120+
testState.useNativeRenderer = useNativeRenderer;
121+
testState.resources = [];
122+
});
123+
124+
it.each([
125+
["/workspace/project", "README.md", "/workspace/project/images/diagram.png"],
126+
["/workspace/project", "docs/README.md", "/workspace/project/docs/images/diagram.png"],
127+
[
128+
"C:\\Users\\dara\\project",
129+
"docs\\README.md",
130+
"C:\\Users\\dara\\project\\docs\\images\\diagram.png",
131+
],
132+
])("requests an image relative to %s and %s", (cwd, relativePath, expectedPath) => {
133+
renderPreview({
134+
cwd,
135+
relativePath,
136+
markdown: "![diagram](images/diagram.png)",
137+
});
138+
139+
expect(testState.resources).toEqual([
140+
{
141+
_tag: "workspace-file",
142+
threadId: ThreadId.make("thread-1"),
143+
path: expectedPath,
144+
},
145+
]);
146+
});
147+
148+
it("leaves remote images with the renderer that already handles them", () => {
149+
renderPreview({
150+
cwd: "/workspace/project",
151+
relativePath: "docs/README.md",
152+
markdown: "![remote](https://example.com/diagram.png)",
153+
});
154+
155+
expect(testState.resources).toEqual([]);
156+
});
157+
});

0 commit comments

Comments
 (0)