Skip to content

Commit 742a3bc

Browse files
voida6claude
andauthored
fix(mobile): markdown images no longer shake on Android (#27943)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d7171d3 commit 742a3bc

2 files changed

Lines changed: 69 additions & 11 deletions

File tree

‎apps/mobile/src/features/threads/ThreadMarkdownImage.test.tsx‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import { expect, it, vi } from "vite-plus/test";
55

66
const harness = vi.hoisted(() => ({
77
failImage: () => {},
8+
loadImage: (_event: { source: { width: number; height: number } }) => {},
89
Passthrough: ({ children }: { children?: unknown }) => children,
910
}));
1011

1112
vi.mock("react-native", () => ({
1213
Pressable: harness.Passthrough,
1314
StyleSheet: { absoluteFill: {} },
14-
View: harness.Passthrough,
15+
View: ({ children, style }: { children?: ReactNode; style?: unknown }) => (
16+
<div data-style={JSON.stringify(style)}>{children}</div>
17+
),
1518
}));
1619
vi.mock("expo-image", () => ({
17-
Image: (props: { onError: () => void }) => {
20+
Image: (props: {
21+
onError: () => void;
22+
onLoad: (event: { source: { width: number; height: number } }) => void;
23+
}) => {
1824
harness.failImage = props.onError;
25+
harness.loadImage = props.onLoad;
1926
return <img alt="" />;
2027
},
2128
}));
@@ -41,7 +48,43 @@ import { AtomRegistry } from "effect/reactivity";
4148
import { fetch } from "expo/fetch";
4249
import { parse } from "react-native-svg";
4350

44-
import { ThreadMarkdownImageView } from "./ThreadMarkdownImage";
51+
import { MarkdownImageAvailableWidthContext, ThreadMarkdownImageView } from "./ThreadMarkdownImage";
52+
53+
it("keeps the frame size when the same image reports a different decoded width", async () => {
54+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
55+
const container = document.createElement("div");
56+
const root = createRoot(container);
57+
const frameSize = () =>
58+
[...container.querySelectorAll<HTMLElement>("div[data-style]")]
59+
.map((element) => JSON.parse(element.dataset.style ?? "null") as Record<string, unknown>)
60+
.find((style) => typeof style?.width === "number");
61+
try {
62+
await act(async () =>
63+
root.render(
64+
<MarkdownImageAvailableWidthContext value={345}>
65+
<ThreadMarkdownImageView
66+
uri="https://example.test/hero.png"
67+
sourceKey="workspace:hero.png"
68+
unavailable={false}
69+
alt={null}
70+
knownSize={{ width: 1000, height: 585 }}
71+
onPressPreview={() => {}}
72+
/>
73+
</MarkdownImageAvailableWidthContext>,
74+
),
75+
);
76+
// Android reports the bitmap downsampled for the current frame, so the same file
77+
// alternates between widths (969 and 970 of 1000) as the frame changes size.
78+
await act(async () => harness.loadImage({ source: { width: 970, height: 567 } }));
79+
const first = frameSize();
80+
expect(first).toBeDefined();
81+
82+
await act(async () => harness.loadImage({ source: { width: 969, height: 567 } }));
83+
expect(frameSize()).toEqual(first);
84+
} finally {
85+
await act(async () => root.unmount());
86+
}
87+
});
4588

4689
it("retries a failed image once its URI comes back through null", async () => {
4790
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

‎apps/mobile/src/features/threads/ThreadMarkdownImage.tsx‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,16 @@ export function ThreadMarkdownImageView(props: {
192192
key={props.sourceKey}
193193
uri={props.uri}
194194
cacheKey={props.cacheKey}
195-
onLoad={(size) => setDecoded({ sourceKey: props.sourceKey, size })}
195+
onLoad={(size) =>
196+
// Only the first decode counts: later loads report the bitmap
197+
// downsampled for the current frame (969 vs 970 wide), so keeping
198+
// them resizes the frame, which reloads the image, and so on.
199+
setDecoded((current) =>
200+
current?.sourceKey === props.sourceKey
201+
? current
202+
: { sourceKey: props.sourceKey, size },
203+
)
204+
}
196205
onError={() => setFailure({ uri: props.uri, failed: true })}
197206
/>
198207
)}
@@ -269,6 +278,18 @@ export function ThreadMarkdownImage(props: {
269278
}) {
270279
const withGallery = useFilePreviewGallery();
271280
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
281+
const resourceKey =
282+
props.resource._tag === "attachment"
283+
? `attachment:${props.resource.attachmentId}`
284+
: props.resource._tag === "tool-output-image"
285+
? `tool-output:${props.resource.itemId}:${props.resource.index}`
286+
: `workspace:${props.resource.path}`;
287+
// The view keeps the first decoded size per key, so a file edited in place needs a
288+
// new key; a re-signed URL for the same revision must not get one.
289+
const sourceKey =
290+
assetUrl._tag === "Success" && assetUrl.fileRevision !== undefined
291+
? `${resourceKey}@${assetUrl.fileRevision}`
292+
: resourceKey;
272293

273294
return (
274295
<ThreadMarkdownImageView
@@ -278,13 +299,7 @@ export function ThreadMarkdownImage(props: {
278299
? assetImageCacheKey(props.environmentId, props.resource, assetUrl.fileRevision)
279300
: undefined
280301
}
281-
sourceKey={
282-
props.resource._tag === "attachment"
283-
? `attachment:${props.resource.attachmentId}`
284-
: props.resource._tag === "tool-output-image"
285-
? `tool-output:${props.resource.itemId}:${props.resource.index}`
286-
: `workspace:${props.resource.path}`
287-
}
302+
sourceKey={sourceKey}
288303
unavailable={assetUrl._tag === "Failure"}
289304
unavailableDetail={
290305
assetUrl._tag === "Failure" && assetUrl.reason === "missing-file-access"

0 commit comments

Comments
 (0)