-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathCamera.tsx
More file actions
267 lines (244 loc) · 8.6 KB
/
Camera.tsx
File metadata and controls
267 lines (244 loc) · 8.6 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"use client";
import { useAuth } from "@clerk/nextjs";
import { useEffect, useState } from "react";
import { ImageCard } from "./ImageCard";
import { motion } from "framer-motion";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
import { Button } from "./ui/button";
import { Download, ChevronLeft, ChevronRight } from "lucide-react";
import Image from "next/image";
import { useRequestStore } from "@/store/useRequestStore";
import { getImage, getOldImages } from "../services/image.service";
export interface TImage {
id: string;
imageUrl: string;
modelId: string;
userId: string;
prompt: string;
falAiRequestId: string;
status: string;
createdAt: string;
updatedAt: string;
}
export interface CameraProps {
requestIds: string[];
}
export function Camera() {
const { requestIds, removeRequestId } = useRequestStore();
const [images, setImages] = useState<TImage[]>([]);
const [imagesLoading, setImagesLoading] = useState(false);
const [selectedImage, setSelectedImage] = useState<TImage | null>(null);
const [currentImageIndex, setCurrentImageIndex] = useState<number>(0);
const [isDownloading, setIsDownloading] = useState(false);
const { getToken } = useAuth();
const formatDate = (dateString: string) => {
return new Date(dateString).toLocaleString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
});
};
const fetchImages = async (requestId: string) => {
try {
setImagesLoading(true);
const token: string | null = await getToken();
if (!token) return;
const response = await getImage(token, requestId);
if (response.data.images) {
setImages(prev => [...prev, response.data.images]);
removeRequestId(requestId);
} else if (response.status === 404) {
removeRequestId(requestId);
}
setImagesLoading(false);
} catch (error) {
console.error("Failed to fetch images:", error);
setImagesLoading(false);
}
};
const fetchOldImages = async () => {
const token: string | null = await getToken();
if (!token) return;
const response = await getOldImages(token);
setImages(response.images);
};
useEffect(() => {
fetchOldImages();
}, []);
useEffect(() => {
if (!requestIds) {
return;
}
const pollInterval = setInterval(() => {
requestIds?.forEach((requestId: string) => {
fetchImages(requestId);
});
}, 10000);
requestIds?.forEach((requestId: string) => {
fetchImages(requestId);
});
return () => clearInterval(pollInterval);
}, [requestIds]);
const handleImageClick = (image: TImage, index: number) => {
setSelectedImage(image);
setCurrentImageIndex(index);
};
const handleDownload = async (imageUrl: string, imageName: string) => {
if (!imageUrl) return;
try {
setIsDownloading(true);
const response = await fetch(imageUrl);
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${imageName}.png`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (error) {
console.error("Error downloading image:", error);
} finally {
setIsDownloading(false);
}
};
const handleNavigation = (direction: "previous" | "next") => {
const newIndex =
direction === "previous" ? currentImageIndex - 1 : currentImageIndex + 1;
if (newIndex >= 0 && newIndex < images.length) {
setCurrentImageIndex(newIndex);
setSelectedImage(images[newIndex] || null);
}
};
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<h2 className="text-2xl font-semibold">Your Gallery</h2>
<span className="text-xs select-none bg-secondary/40 font-semibold border border-secondary text-muted-foreground px-2 py-1 rounded-full">
{images.length} images
</span>
</div>
<motion.div
className="columns-1 md:columns-3 lg:columns-3 gap-4"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
{imagesLoading
? [...Array(8)].map((_, i) => (
<motion.div
key={i}
className="bg-neutral-300 h-48 rounded-lg animate-pulse"
/>
))
: images.map((image, index) => (
<div
key={image.id + index}
className="cursor-pointer transition-transform mb-4 hover:scale-[1.02]"
onClick={() => handleImageClick(image, index)}
>
<ImageCard
id={image.id}
status={image.status}
imageUrl={image.imageUrl}
onClick={() => handleImageClick(image, index)}
modelId={image.modelId}
userId={image.userId}
prompt={image.prompt}
falAiRequestId={image.falAiRequestId}
createdAt={image.createdAt}
updatedAt={image.updatedAt}
/>
</div>
))}
</motion.div>
{!imagesLoading && images.length === 0 && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
className="text-center py-12"
>
<p className="text-muted-foreground">
No images yet. Start by generating some!
</p>
</motion.div>
)}
{selectedImage && (
<Dialog
open={!!selectedImage}
onOpenChange={(open) => !open && setSelectedImage(null)}
>
<DialogContent className="max-w-5xl p-10 overflow-hidden bg-black/90 backdrop-blur-xl">
<DialogTitle className="sr-only">Image Preview</DialogTitle>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="relative w-full h-full flex flex-col items-center justify-center"
>
<div className="absolute top-4 left-4 right-4 text-white">
<p className="text-lg font-medium truncate">
{selectedImage?.prompt}
</p>
<p className="text-sm">{formatDate(selectedImage.createdAt)}</p>
</div>
<div className="relative aspect-square w-full">
<Image
src={selectedImage.imageUrl}
alt={selectedImage.prompt || "Generated image"}
fill
className="object-contain"
priority
quality={100}
sizes="(max-width: 768px) 85vw, (max-width: 1200px) 80vw, 1200px"
/>
</div>
<div className="absolute bottom-4 left-0 right-0 flex justify-center">
<Button
variant="default"
onClick={() =>
handleDownload(
selectedImage.imageUrl,
selectedImage.prompt || "generated-image"
)
}
disabled={isDownloading || !selectedImage.imageUrl}
className="relative z-10 hover:cursor-pointer"
>
<Download className="h-4 w-4 mr-2" />
Download Image
</Button>
</div>
<div className="absolute inset-0 flex items-center justify-between p-4">
{currentImageIndex > 0 && (
<Button
variant="ghost"
size="icon"
onClick={() => handleNavigation("previous")}
className="h-10 w-10 rounded-full bg-black/50 hover:bg-black/70 hover:cursor-pointer"
>
<ChevronLeft className="h-6 w-6" />
</Button>
)}
{currentImageIndex < images.length - 1 && (
<Button
variant="ghost"
size="icon"
onClick={() => handleNavigation("next")}
className="h-10 w-10 rounded-full bg-black/50 hover:bg-black/70 hover:cursor-pointer"
>
<ChevronRight className="h-6 w-6" />
</Button>
)}
</div>
</motion.div>
</DialogContent>
</Dialog>
)}
</div>
);
}