TypeScript
fix(web): prevent composer image thumbnail flicker (#28499)
Yash Singhce48e4590018
diff --git a/apps/web/src/components/chat/ComposerImageThumbnail.tsx b/apps/web/src/components/chat/ComposerImageThumbnail.tsxindex e9de2aa93b3..68158c7151b 100644--- a/apps/web/src/components/chat/ComposerImageThumbnail.tsx+++ b/apps/web/src/components/chat/ComposerImageThumbnail.tsx@@ -1,24 +1,15 @@-import { memo, Suspense, use, type ReactNode } from "react";+import { memo, useLayoutEffect, useState, type ReactNode } from "react";++import { cn } from "~/lib/utils";import {createComposerImageThumbnail,+ isHeicImageFile,+ MAX_COMPRESSIBLE_SOURCE_BYTES,readComposerImageThumbnail,} from "../../lib/imageCompression";-export const ComposerImageThumbnail = memo(function ComposerImageThumbnail(props: {- file: File;- alt: string;- className: string;- fallback: ReactNode;-}) {- return (- <Suspense fallback={props.fallback}>- <ThumbnailImage {...props} />- </Suspense>- );-});--function ThumbnailImage({+export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({file,alt,className,@@ -29,8 +20,63 @@ function ThumbnailImage({className: string;fallback: ReactNode;}) {- // A settled thumbnail paints synchronously instead of suspending for a tick.- const settled = readComposerImageThumbnail(file);- const src = settled === undefined ? use(createComposerImageThumbnail(file)) : settled;- return src ? <img src={src} alt={alt} className={className} /> : fallback;-}+ const [preview, setPreview] = useState(() => ({+ file,+ src: readComposerImageThumbnail(file),+ }));+ const [loadedSrc, setLoadedSrc] = useState<string | null>(null);+ const [failedSrc, setFailedSrc] = useState<string | null>(null);+ const src = preview.file === file ? preview.src : readComposerImageThumbnail(file);++ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the local blob URL and decodes its replacement before swapping the visible image+ useLayoutEffect(() => {+ let disposed = false;+ let localUrl: string | undefined;+ const cached = readComposerImageThumbnail(file);+ if (+ file.size <= MAX_COMPRESSIBLE_SOURCE_BYTES &&+ !isHeicImageFile(file) &&+ cached === undefined+ ) {+ localUrl = URL.createObjectURL(file);+ // oxlint-disable-next-line react/set-state-in-effect -- the blob URL is created and revoked by this effect+ setPreview({ file, src: localUrl });+ }+ if (cached === undefined) {+ void createComposerImageThumbnail(file).then(async (thumbnail) => {+ if (disposed) return;+ if (!thumbnail) {+ if (!localUrl) setPreview({ file, src: null });+ return;+ }+ const image = new Image();+ image.src = thumbnail;+ try {+ await image.decode();+ if (!disposed) setPreview({ file, src: thumbnail });+ } catch {+ // Keep the local preview if the thumbnail cannot be decoded.+ }+ });+ }+ return () => {+ disposed = true;+ if (localUrl) URL.revokeObjectURL(localUrl);+ };+ }, [file]);++ return src && failedSrc !== src ? (+ <img+ ref={(image) => {+ if (image?.complete && image.naturalWidth > 0) setLoadedSrc(src);+ }}+ src={src}+ alt={alt}+ onLoad={() => setLoadedSrc(src)}+ onError={() => setFailedSrc(src)}+ className={cn(className, loadedSrc !== src && "invisible")}+ />+ ) : src === undefined ? null : (+ fallback+ );+});