ImageCard.tsx
7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React, { useState, useRef, useEffect } from 'react';
import { ImageItem } from '../types';
import { Download, Heart, Video, Hourglass } from 'lucide-react';
interface ImageCardProps {
image: ImageItem;
onClick: (image: ImageItem) => void;
onLike: (image: ImageItem) => void;
currentUser?: string;
isVideo?: boolean;
}
const ImageCard: React.FC<ImageCardProps> = ({ image, onClick, onLike, currentUser, isVideo = false }) => {
const [isLoaded, setIsLoaded] = useState(false);
const [isHovered, setIsHovered] = useState(false);
const [videoStarted, setVideoStarted] = useState(false);
const videoRef = useRef<HTMLVideoElement>(null);
const handleLike = (e: React.MouseEvent) => {
e.stopPropagation();
onLike(image);
};
const handleMouseEnter = () => {
setIsHovered(true);
// Video will be played via autoPlay once rendered
}
const handleMouseLeave = () => {
setIsHovered(false);
setVideoStarted(false);
}
// Content is considered "ready" if the main image/video is loaded
// OR if we have a thumbnail to show for a video.
const showContent = isLoaded || (isVideo && !!image.thumbnail);
return (
<div
className="group relative mb-4 break-inside-avoid rounded-2xl overflow-hidden bg-gray-100 cursor-zoom-in shadow-sm hover:shadow-xl transition-all duration-300"
onClick={() => onClick(image)}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
{/* Placeholder / Skeleton */}
{!showContent && (
<div className="absolute inset-0 bg-gray-200 animate-pulse min-h-[150px]" />
)}
{/* Main Content */}
{isVideo ? (
<div className="relative w-full overflow-hidden bg-gray-100">
{/* Always show thumbnail first */}
<img
src={image.thumbnail || (isVideo ? '' : image.url)}
alt={image.prompt}
className={`w-full h-auto object-cover transition-opacity duration-500 ${videoStarted ? 'opacity-0' : 'opacity-100'} ${!image.thumbnail && isVideo ? 'opacity-0' : ''}`}
onLoad={() => setIsLoaded(true)}
loading="lazy"
/>
{isVideo && !image.thumbnail && !videoStarted && (
<div className="absolute inset-0 flex items-center justify-center bg-gray-200 text-gray-400">
<Video size={32} />
</div>
)}
{/* Load video only on hover */}
{isHovered && (
<video
ref={videoRef}
src={image.url}
className={`absolute inset-0 w-full h-full object-cover transition-opacity duration-300 ${videoStarted ? 'opacity-100' : 'opacity-0'}`}
onLoadedData={() => setVideoStarted(true)}
autoPlay
loop
muted
playsInline
/>
)}
{/* Video Indicator Icon (When not hovered) */}
{!isHovered && (
<div className="absolute top-3 left-3 p-1.5 bg-black/30 backdrop-blur-md rounded-lg text-white/90">
<Video size={14} />
</div>
)}
</div>
) : (
<img
src={image.url}
alt={image.prompt}
className={`w-full h-auto object-cover transition-transform duration-700 ease-in-out group-hover:scale-105 ${isLoaded ? 'opacity-100' : 'opacity-0'}`}
onLoad={() => setIsLoaded(true)}
loading="lazy"
/>
)}
{/* Hover Overlay */}
<div className="absolute inset-0 bg-gradient-to-t from-black/90 via-black/20 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex flex-col justify-end p-4 md:p-5">
{/* Top Right: Download */}
<div className="absolute top-4 right-4 translate-y-[-10px] opacity-0 group-hover:translate-y-0 group-hover:opacity-100 transition-all duration-300 delay-75">
<button
className="p-2 bg-white/20 backdrop-blur-md hover:bg-white/40 rounded-full text-white transition-colors"
title="Download"
onClick={(e) => {
e.stopPropagation();
const link = document.createElement('a');
// Append ?download=true to force server to send attachment header
link.href = `${image.url}?download=true`;
// Filename is optional here as server header takes precedence, but good for fallback
link.download = `z-${isVideo ? 'video' : 'image'}-${image.id}.${isVideo ? 'mp4' : 'png'}`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}}
>
<Download size={16} />
</button>
</div>
{isVideo && (
<div className="absolute top-4 left-4">
<Video size={16} className="text-white/80" />
</div>
)}
{/* Content Info */}
<div className="text-white transform translate-y-4 group-hover:translate-y-0 transition-transform duration-300">
<p className="font-medium text-sm line-clamp-2 mb-2 text-shadow-sm">
{image.prompt}
</p>
{!isVideo ? (
<div className="flex justify-between items-end">
{/* Author Info */}
<div className="text-xs text-gray-300 font-mono flex flex-col gap-1">
<span className="opacity-75">ID: {image.authorId || 'UNKNOWN'}</span>
<span className="bg-white/10 px-1.5 py-0.5 rounded w-fit">{image.width}x{image.height}</span>
</div>
{/* Like Button */}
<button
onClick={handleLike}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-full backdrop-blur-md transition-all duration-200 ${image.isLikedByCurrentUser ? 'bg-red-500/80 text-white' : 'bg-white/20 text-white hover:bg-white/30'}`}
title={currentUser ? "Like this" : "Login to like"}
>
<Heart size={14} fill={image.isLikedByCurrentUser ? "currentColor" : "none"} />
<span className="text-xs font-bold">{image.likes}</span>
</button>
</div>
) : (
<div className="flex justify-between items-end">
<div className="text-xs text-gray-300 font-mono flex flex-col gap-1">
<span className="opacity-75">ID: {image.authorId || 'UNKNOWN'}</span>
{image.generationTime && (
<span className="bg-white/10 px-1.5 py-0.5 rounded w-fit flex items-center gap-1"><Hourglass size={10} />{image.generationTime.toFixed(1)}s</span>
)}
</div>
<button
onClick={handleLike}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-full backdrop-blur-md transition-all duration-200 ${image.isLikedByCurrentUser ? 'bg-red-500/80 text-white' : 'bg-white/20 text-white hover:bg-white/30'}`}
title={currentUser ? "Like this video" : "Login to like"}
>
<Heart size={14} fill={image.isLikedByCurrentUser ? "currentColor" : "none"} />
<span className="text-xs font-bold">{image.likes}</span>
</button>
</div>
)}
</div>
</div>
</div>
);
};
export default ImageCard;