Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/components/ui/Display/BaseImage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use client';

import { Component, useState, type ReactNode } from 'react';
import { Component, useEffect, useState, type ReactNode } from 'react';
import Image from 'next/image';
import Fallback from 'public/bottle.svg';

const SKELETON_DELAY_MS = 100;

class ImageErrorBoundary extends Component<
{ fallback: ReactNode; children: ReactNode },
{ hasError: boolean }
Expand Down Expand Up @@ -51,6 +53,22 @@ const BaseImage = ({
}: Props) => {
const [imgSrc, setImgSrc] = useState(src || Fallback);
const [isLoading, setIsLoading] = useState(true);
const [showSkeleton, setShowSkeleton] = useState(false);

useEffect(() => {
if (!isLoading) {
setShowSkeleton(false);
return;
}

const timer = window.setTimeout(() => {
setShowSkeleton(true);
}, SKELETON_DELAY_MS);

return () => {
window.clearTimeout(timer);
};
}, [isLoading]);

const handleError = () => {
setImgSrc(Fallback);
Expand Down Expand Up @@ -86,7 +104,7 @@ const BaseImage = ({
/>
}
>
{isLoading && (
{showSkeleton && (
<div className="absolute inset-0 bg-gray-200 animate-pulse" />
)}
<Image
Expand Down
Loading