Spaces:
Runtime error
Runtime error
feat: add image generation result component
Browse files
src/components/ImageGenerationResult.tsx
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"use client";
|
2 |
+
import { LoadingIcon } from "@/components/LoadingIcon";
|
3 |
+
import { Skeleton } from "@/components/ui/skeleton";
|
4 |
+
import { checkStatus } from "@/server/generate";
|
5 |
+
import { useEffect, useState } from "react";
|
6 |
+
|
7 |
+
export function ImageGenerationResult({ runId }: { runId: string; }) {
|
8 |
+
const [image, setImage] = useState("");
|
9 |
+
const [status, setStatus] = useState<string>("preparing");
|
10 |
+
const [loading, setLoading] = useState(true);
|
11 |
+
|
12 |
+
// Polling in frontend to check for the
|
13 |
+
useEffect(() => {
|
14 |
+
if (!runId) return;
|
15 |
+
const interval = setInterval(() => {
|
16 |
+
checkStatus(runId).then((res) => {
|
17 |
+
if (res) setStatus(res.status);
|
18 |
+
if (res && res.status === "success") {
|
19 |
+
console.log(res.outputs[0]?.data);
|
20 |
+
setImage(res.outputs[0]?.data?.images[0].url);
|
21 |
+
setLoading(false);
|
22 |
+
clearInterval(interval);
|
23 |
+
}
|
24 |
+
});
|
25 |
+
}, 2000);
|
26 |
+
return () => clearInterval(interval);
|
27 |
+
}, [runId]);
|
28 |
+
|
29 |
+
return (
|
30 |
+
<div className="border border-gray-200 w-full square w-full rounded-lg relative">
|
31 |
+
{!loading && image && (
|
32 |
+
<img
|
33 |
+
className="w-full h-full object-contain"
|
34 |
+
src={image}
|
35 |
+
alt="Generated image"
|
36 |
+
></img>
|
37 |
+
)}
|
38 |
+
{!image && status && (
|
39 |
+
<div className="absolute top-0 left-0 w-full h-full flex items-center justify-center gap-2">
|
40 |
+
{status} <LoadingIcon />
|
41 |
+
</div>
|
42 |
+
)}
|
43 |
+
{loading && <Skeleton className="w-full h-full" />}
|
44 |
+
</div>
|
45 |
+
);
|
46 |
+
}
|