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" ;
24
35import {
46 createComposerImageThumbnail ,
7+ isHeicImageFile ,
8+ MAX_COMPRESSIBLE_SOURCE_BYTES ,
59 readComposerImageThumbnail ,
610} from "../../lib/imageCompression" ;
711
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 ( {
2213 file,
2314 alt,
2415 className,
@@ -29,8 +20,63 @@ function ThumbnailImage({
2920 className : string ;
3021 fallback : ReactNode ;
3122} ) {
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+ } ) ;
0 commit comments