The best way to control Claude Code, Codex and more from any computer or phone
| 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 | |
| 12 | export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({ |
| 13 | file, |
| 14 | alt, |
| 15 | className, |
| 16 | fallback, |
| 17 | }: { |
| 18 | file: File; |
| 19 | alt: string; |
| 20 | className: string; |
| 21 | fallback: ReactNode; |
| 22 | }) { |
| 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 (file.size <= MAX_COMPRESSIBLE_SOURCE_BYTES && !isHeicImageFile(file) && !cached) { |
| 37 | localUrl = URL.createObjectURL(file); |
| 38 | // oxlint-disable-next-line react/set-state-in-effect -- the blob URL is created and revoked by this effect |
| 39 | setPreview({ file, src: localUrl }); |
| 40 | } |
| 41 | if (cached === undefined) { |
| 42 | void createComposerImageThumbnail(file).then(async (thumbnail) => { |
| 43 | if (disposed) return; |
| 44 | if (!thumbnail) { |
| 45 | if (!localUrl) setPreview({ file, src: null }); |
| 46 | return; |
| 47 | } |
| 48 | const image = new Image(); |
| 49 | image.src = thumbnail; |
| 50 | try { |
| 51 | await image.decode(); |
| 52 | if (!disposed) setPreview({ file, src: thumbnail }); |
| 53 | } catch { |
| 54 | // Keep the local preview if the thumbnail cannot be decoded. |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | return () => { |
| 59 | disposed = true; |
| 60 | if (localUrl) URL.revokeObjectURL(localUrl); |
| 61 | }; |
| 62 | }, [file]); |
| 63 | |
| 64 | return src && failedSrc !== src ? ( |
| 65 | <span className="relative block size-full"> |
| 66 | {loadedSrc !== src && fallback} |
| 67 | <img |
| 68 | ref={(image) => { |
| 69 | if (image?.complete && image.naturalWidth > 0) setLoadedSrc(src); |
| 70 | }} |
| 71 | src={src} |
| 72 | alt={alt} |
| 73 | onLoad={() => setLoadedSrc(src)} |
| 74 | onError={() => setFailedSrc(src)} |
| 75 | className={cn(className, loadedSrc !== src && "invisible absolute inset-0")} |
| 76 | /> |
| 77 | </span> |
| 78 | ) : ( |
| 79 | fallback |
| 80 | ); |
| 81 | }); |