The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 08:15 UTC
fix(mobile): markdown images no longer shake on Android (#27943)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
voida6 committed
Nov 2, 2026 at 08:50 UTC
742a3bcf579c5ef6454c7593a899f1e573093d08
2 files changed
+69
-11
apps/mobile/src/features/threads/ThreadMarkdownImage.test.tsx
+46
-3
@@ -5,17 +5,24 @@ import { expect, it, vi } from "vite-plus/test";
5
6
const harness = vi.hoisted(() => ({
7
failImage: () => {},
8
+ loadImage: (_event: { source: { width: number; height: number } }) => {},
9
Passthrough: ({ children }: { children?: unknown }) => children,
10
}));
11
12
vi.mock("react-native", () => ({
13
Pressable: harness.Passthrough,
14
StyleSheet: { absoluteFill: {} },
14
- View: harness.Passthrough,
15
+ View: ({ children, style }: { children?: ReactNode; style?: unknown }) => (
16
+ <div data-style={JSON.stringify(style)}>{children}</div>
17
+ ),
18
}));
19
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
+ }) => {
24
harness.failImage = props.onError;
25
+ harness.loadImage = props.onLoad;
26
return <img alt="" />;
27
},
28
}));
@@ -41,7 +48,43 @@ import { AtomRegistry } from "effect/reactivity";
48
import { fetch } from "expo/fetch";
49
import { parse } from "react-native-svg";
50
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
+});
88
89
it("retries a failed image once its URI comes back through null", async () => {
90
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
apps/mobile/src/features/threads/ThreadMarkdownImage.tsx
+23
-8
@@ -192,7 +192,16 @@ export function ThreadMarkdownImageView(props: {
192
key={props.sourceKey}
193
uri={props.uri}
194
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
+ }
205
onError={() => setFailure({ uri: props.uri, failed: true })}
206
/>
207
)}
@@ -269,6 +278,18 @@ export function ThreadMarkdownImage(props: {
278
}) {
279
const withGallery = useFilePreviewGallery();
280
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;
293
294
return (
295
<ThreadMarkdownImageView
@@ -278,13 +299,7 @@ export function ThreadMarkdownImage(props: {
299
? assetImageCacheKey(props.environmentId, props.resource, assetUrl.fileRevision)
300
: undefined
301
}
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}
303
unavailable={assetUrl._tag === "Failure"}
304
unavailableDetail={
305
assetUrl._tag === "Failure" && assetUrl.reason === "missing-file-access"