Skip to content

Commit a1ac0e6

Browse files
Add skeleton loading state to GalleryItem for improved image loading experience
1 parent 5515046 commit a1ac0e6

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

src/components/GalleryItem.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useNavigate } from "react-router-dom";
55
import { useLike } from "@/hooks/useLike";
66
import { useCopy } from "@/hooks/useCopy";
77
import { Prompt } from "@/types";
8+
import { Skeleton } from "./ui/skeleton";
89

910
interface GalleryItemProps {
1011
prompt: Prompt;
@@ -15,6 +16,7 @@ const GalleryItem: React.FC<GalleryItemProps> = ({ prompt, index }) => {
1516
const navigate = useNavigate();
1617
const { isLiked: initialIsLiked, handleLike } = useLike(prompt.liked);
1718
const { isCopied, handleCopy } = useCopy();
19+
const [imageLoaded, setImageLoaded] = useState(false);
1820

1921
// Local state to track like status and like count
2022
const [isLiked, setIsLiked] = useState(initialIsLiked);
@@ -40,19 +42,12 @@ const GalleryItem: React.FC<GalleryItemProps> = ({ prompt, index }) => {
4042
}
4143
>
4244
<div className="relative group">
45+
{!imageLoaded && <Skeleton className="w-full h-[300px]" />}
4346
<img
4447
className="w-full h-auto max-w-full max-h-[600px] object-cover"
45-
src={
46-
prompt.images?.[0] ??
47-
"https://www.astria.ai/assets/logo-b4e21f646fb5879eb91113a70eae015a7413de8920960799acb72c60ad4eaa99.png"
48-
}
48+
src={prompt.images?.[0]}
4949
alt={`Image ${prompt.id}`}
50-
loading="lazy"
51-
onError={(e) => {
52-
e.currentTarget.onerror = null;
53-
e.currentTarget.src =
54-
"https://www.astria.ai/assets/logo-b4e21f646fb5879eb91113a70eae015a7413de8920960799acb72c60ad4eaa99.png";
55-
}}
50+
onLoad={() => setImageLoaded(true)}
5651
/>
5752
<div className="absolute bottom-0 left-0 w-full bg-gradient-to-t from-black from-0% bg-opacity-50 p-1 md:p-4 flex justify-between items-center md:opacity-0 md:group-hover:opacity-100 md:transition-opacity md:duration-300">
5853
<div className="text-sm text-white font-medium hover:bg-white hover:bg-opacity-20 px-4 py-2 rounded-full cursor-pointer">
@@ -62,8 +57,8 @@ const GalleryItem: React.FC<GalleryItemProps> = ({ prompt, index }) => {
6257
<button
6358
className={`p-2 rounded-full transition-colors ${isCopied ? "bg-green-500" : "hover:bg-white/20"}`}
6459
onClick={(e) => {
65-
e.stopPropagation();
66-
handleCopy(prompt);
60+
e.stopPropagation();
61+
handleCopy(prompt);
6762
}}
6863
aria-label="Copy prompt text"
6964
>

0 commit comments

Comments
 (0)