fix liveportrait thumbnails

This commit is contained in:
bflatastic committed 2024-08-21 14:52:20 +07:00
1 parent 94524b6993
commit 2cc44ba616
1 file changed
+23 -19
@@ -13,13 +13,13 @@ const OutputThumbnailImage: React.FC<OutputThumbnailImageProps> = ({
style,
draggable,
}) => {
const [isThumbReady, setIsThumbReady] = useState(false);
const [attempts, setAttempts] = useState(0);
const [thumbnailSrc, setThumbnailSrc] = useState(src);
const [thumbnailSrc, setThumbnailSrc] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
let isMounted = true;
const maxAttempts = 5;
let attempts = 0;
const checkImage = () => {
const img = new Image();
@@ -28,42 +28,46 @@ const OutputThumbnailImage: React.FC<OutputThumbnailImageProps> = ({
img.onload = () => {
if (isMounted) {
console.log("Thumbnail loaded successfully");
setIsThumbReady(true);
setThumbnailSrc(thumbSrc);
setIsLoading(false);
}
};
img.onerror = () => {
if (isMounted && !isThumbReady && attempts < maxAttempts) {
if (isMounted && attempts < maxAttempts) {
console.log(`Attempt ${attempts + 1} failed, retrying...`);
setTimeout(checkImage, 1000); // Retry after 1 second if the image is not available
setAttempts(prev => prev + 1);
setTimeout(checkImage, 1000);
attempts += 1;
} else {
console.log("Max attempts reached or component unmounted");
setIsLoading(false);
}
};
};
if (src && src.toLowerCase().endsWith(".mp4")) {
checkImage();
} else {
setThumbnailSrc(src);
setIsLoading(false);
}
return () => {
isMounted = false;
};
}, [src, isThumbReady, attempts]);
}, [src]);
if (isLoading || !thumbnailSrc) {
return null;
}
return (
<>
{isThumbReady ? (
<img
key={src}
src={thumbnailSrc}
alt={alt}
style={style}
draggable={draggable}
/>
) : null}
</>
<img
key={thumbnailSrc}
src={thumbnailSrc}
alt={alt}
style={style}
draggable={draggable}
/>
);
};