Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerImageThumbnail.tsx

36 lines896 Bytes

1import { memo, Suspense, use, type ReactNode } from "react";
2​
3import {
4 createComposerImageThumbnail,
5 readComposerImageThumbnail,
6} from "../../lib/imageCompression";
7​
8export 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​
21function ThumbnailImage({
22 file,
23 alt,
24 className,
25 fallback,
26}: {
27 file: File;
28 alt: string;
29 className: string;
30 fallback: ReactNode;
31}) {
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;