-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathGenerateImage.tsx
More file actions
111 lines (102 loc) · 3.12 KB
/
GenerateImage.tsx
File metadata and controls
111 lines (102 loc) · 3.12 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
"use client";
import { useAuth } from "@clerk/nextjs";
import { useState } from "react";
import { Button } from "./ui/button";
import { Textarea } from "@/components/ui/textarea";
import axios from "axios";
import { BACKEND_URL } from "@/app/config";
import { SelectModel } from "./Models";
import toast from "react-hot-toast";
import { motion } from "framer-motion";
import { Sparkles } from "lucide-react";
import { useCredits } from "@/hooks/use-credits";
import { useRouter } from "next/navigation";
import CustomLabel from "./ui/customLabel";
import { GlowEffect } from "./GlowEffect";
import { useRequestStore } from "@/store/useRequestStore";
export function GenerateImage() {
const [prompt, setPrompt] = useState("");
const [selectedModel, setSelectedModel] = useState<string>();
const [isGenerating, setIsGenerating] = useState(false);
const { addRequestId } = useRequestStore();
const { getToken } = useAuth();
const { credits } = useCredits();
const router = useRouter();
const handleGenerate = async () => {
if (!prompt || !selectedModel) return;
if (credits <= 0) {
router.push("/pricing");
return;
}
setIsGenerating(true);
try {
const token = await getToken();
const response = await axios.post(
`${BACKEND_URL}/ai/generate`,
{
prompt,
modelId: selectedModel,
num: 1,
},
{
headers: { Authorization: `Bearer ${token}` },
}
);
addRequestId(response.data.requestId);
toast.success("Image generation started!");
setPrompt("");
} catch (error) {
toast.error("Failed to generate image");
} finally {
setIsGenerating(false);
}
};
return (
<motion.div
className="space-y-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
<div className="space-y-4">
<SelectModel
selectedModel={selectedModel}
setSelectedModel={setSelectedModel}
/>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.2 }}
className="relative w-full"
>
<CustomLabel label="Enter your prompt here..." />
<Textarea
className="w-full min-h-24"
onChange={(e) => setPrompt(e.target.value)}
/>
</motion.div>
<div className="flex justify-end pt-4">
<div className="relative">
<Button
onClick={handleGenerate}
disabled={isGenerating || !prompt || !selectedModel}
variant={"outline"}
className="relative z-20 cursor-pointer"
>
Generate Image <Sparkles size={24} />
</Button>
{(prompt && selectedModel) && (
<GlowEffect
colors={["#FF5733", "#33FF57", "#3357FF", "#F1C40F"]}
mode="colorShift"
blur="soft"
duration={3}
scale={0.9}
/>
)}
</div>
</div>
</div>
</motion.div>
);
}