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(web): prevent composer image thumbnail flicker (#28499)
Yash Singh committed
Oct 31, 2026 at 23:21 UTC
ce48e459001817092f45f70f334212bb2b973288
1 file changed
+66
-20
apps/web/src/components/chat/ComposerImageThumbnail.tsx
+66
-20
@@ -1,24 +1,15 @@
1
-import { memo, Suspense, use, type ReactNode } from "react";
1
+import { memo, useLayoutEffect, useState, type ReactNode } from "react";
2
+
3
+import { cn } from "~/lib/utils";
4
5
import {
6
createComposerImageThumbnail,
7
+ isHeicImageFile,
8
+ MAX_COMPRESSIBLE_SOURCE_BYTES,
9
readComposerImageThumbnail,
10
} from "../../lib/imageCompression";
11
8
-export const ComposerImageThumbnail = memo(function ComposerImageThumbnail(props: {
9
- file: File;
10
- alt: string;
11
- className: string;
12
- fallback: ReactNode;
13
-}) {
14
- return (
15
- <Suspense fallback={props.fallback}>
16
- <ThumbnailImage {...props} />
17
- </Suspense>
18
- );
19
-});
20
-
21
-function ThumbnailImage({
12
+export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
13
file,
14
alt,
15
className,
@@ -29,8 +20,63 @@ function ThumbnailImage({
20
className: string;
21
fallback: ReactNode;
22
}) {
32
- // A settled thumbnail paints synchronously instead of suspending for a tick.
33
- const settled = readComposerImageThumbnail(file);
34
- const src = settled === undefined ? use(createComposerImageThumbnail(file)) : settled;
35
- return src ? <img src={src} alt={alt} className={className} /> : fallback;
36
-}
23
+ const [preview, setPreview] = useState(() => ({
24
+ file,
25
+ src: readComposerImageThumbnail(file),
26
+ }));
27
+ const [loadedSrc, setLoadedSrc] = useState<string | null>(null);
28
+ const [failedSrc, setFailedSrc] = useState<string | null>(null);
29
+ const src = preview.file === file ? preview.src : readComposerImageThumbnail(file);
30
+
31
+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the local blob URL and decodes its replacement before swapping the visible image
32
+ useLayoutEffect(() => {
33
+ let disposed = false;
34
+ let localUrl: string | undefined;
35
+ const cached = readComposerImageThumbnail(file);
36
+ if (
37
+ file.size <= MAX_COMPRESSIBLE_SOURCE_BYTES &&
38
+ !isHeicImageFile(file) &&
39
+ cached === undefined
40
+ ) {
41
+ localUrl = URL.createObjectURL(file);
42
+ // oxlint-disable-next-line react/set-state-in-effect -- the blob URL is created and revoked by this effect
43
+ setPreview({ file, src: localUrl });
44
+ }
45
+ if (cached === undefined) {
46
+ void createComposerImageThumbnail(file).then(async (thumbnail) => {
47
+ if (disposed) return;
48
+ if (!thumbnail) {
49
+ if (!localUrl) setPreview({ file, src: null });
50
+ return;
51
+ }
52
+ const image = new Image();
53
+ image.src = thumbnail;
54
+ try {
55
+ await image.decode();
56
+ if (!disposed) setPreview({ file, src: thumbnail });
57
+ } catch {
58
+ // Keep the local preview if the thumbnail cannot be decoded.
59
+ }
60
+ });
61
+ }
62
+ return () => {
63
+ disposed = true;
64
+ if (localUrl) URL.revokeObjectURL(localUrl);
65
+ };
66
+ }, [file]);
67
+
68
+ return src && failedSrc !== src ? (
69
+ <img
70
+ ref={(image) => {
71
+ if (image?.complete && image.naturalWidth > 0) setLoadedSrc(src);
72
+ }}
73
+ src={src}
74
+ alt={alt}
75
+ onLoad={() => setLoadedSrc(src)}
76
+ onError={() => setFailedSrc(src)}
77
+ className={cn(className, loadedSrc !== src && "invisible")}
78
+ />
79
+ ) : src === undefined ? null : (
80
+ fallback
81
+ );
82
+});