Iot / src /app /(dashboard) /create /page.tsx
veltrixcode's picture
Upload 74 files
64fa001 verified
Raw History Blame Contribute Delete
18.3 kB
"use client"
import { useState, useTransition } from "react"
import { useRouter } from "next/navigation"
import Image from "next/image"
import { Button } from "@/components/ui/button"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { Loader2, Download, Sparkles, AlertCircle, Wand2 } from "lucide-react"
export default function CreatePage() {
const router = useRouter()
const [isPending, startTransition] = useTransition()
const [prompt, setPrompt] = useState("")
const [model, setModel] = useState("flux-dev")
const [width, setWidth] = useState("1024")
const [height, setHeight] = useState("1024")
const [isCustomWidth, setIsCustomWidth] = useState(false)
const [isCustomHeight, setIsCustomHeight] = useState(false)
const [resultUrl, setResultUrl] = useState<string | null>(null)
const [error, setError] = useState<string | null>(null)
async function handleGenerate() {
if (!prompt) return
setError(null)
setResultUrl(null)
startTransition(async () => {
try {
const res = await fetch("/api/generate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
prompt,
model,
width: parseInt(width),
height: parseInt(height),
}),
})
const data = await res.json()
if (!res.ok) {
setError(data.error || "Generation failed")
} else if (data.url) {
setResultUrl(data.url)
router.refresh() // Update credits in navbar
} else {
setError("No image URL returned")
}
} catch (err) {
setError("Something went wrong")
}
})
}
return (
<div className="container py-8 md:py-16 max-w-7xl min-h-[calc(100vh-4rem)]">
{/* Background elements */}
<div className="fixed inset-0 -z-20 mesh-gradient opacity-30 dark:opacity-20" />
<div className="grid grid-cols-1 lg:grid-cols-5 gap-8 items-start">
{/* Left Column: Control Panel */}
<div className="lg:col-span-2 space-y-6">
<div className="space-y-1 pl-1">
<h1 className="text-4xl font-black tracking-tighter uppercase text-stroke text-transparent bg-clip-text bg-gradient-to-br from-foreground to-muted-foreground">Studio</h1>
<p className="text-sm font-bold text-muted-foreground uppercase tracking-widest">
Cost: 5 Credits / Gen
</p>
</div>
<div className="glass-panel p-6 rounded-none space-y-6 relative overflow-hidden group">
<div className="absolute top-0 right-0 w-24 h-24 bg-primary/10 rounded-full blur-2xl group-hover:bg-primary/20 transition-all duration-500" />
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="model" className="uppercase text-xs font-bold tracking-wider">Model Architecture</Label>
<Select value={model} onValueChange={setModel}>
<SelectTrigger id="model" className="h-12 border-2 border-border rounded-none font-bold focus:ring-offset-0 focus:ring-0 focus:border-primary bg-background/50">
<SelectValue placeholder="Select model" />
</SelectTrigger>
<SelectContent className="rounded-none border-2 border-border neo-brutalist">
<SelectItem value="flux-dev" className="font-medium focus:bg-primary/20">Flux 2 Dev</SelectItem>
<SelectItem value="flux-klein-9b" className="font-medium focus:bg-primary/20">Flux Klein 9B</SelectItem>
<SelectItem value="flux-klein-4b" className="font-medium focus:bg-primary/20">Flux Klein 4B</SelectItem>
<SelectItem value="lucid-origin" className="font-medium focus:bg-primary/20">Lucid Origin</SelectItem>
<SelectItem value="dreamshaper" className="font-medium focus:bg-primary/20">DreamShaper 8</SelectItem>
<SelectItem value="stable-diffusion-xl-lightning" className="font-medium focus:bg-primary/20">SDXL Lightning (Fast)</SelectItem>
</SelectContent>
</Select>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="width" className="uppercase text-xs font-bold tracking-wider">Width</Label>
<Select
value={isCustomWidth ? "custom" : width}
onValueChange={(val) => {
if (val === "custom") setIsCustomWidth(true)
else {
setIsCustomWidth(false)
setWidth(val)
}
}}
>
<SelectTrigger id="width" className="h-12 border-2 border-border rounded-none font-bold bg-background/50">
<SelectValue placeholder="Width" />
</SelectTrigger>
<SelectContent className="rounded-none border-2 border-border neo-brutalist">
<SelectItem value="512">512px</SelectItem>
<SelectItem value="768">768px</SelectItem>
<SelectItem value="1024">1024px</SelectItem>
<SelectItem value="custom" className="text-primary font-bold">Custom...</SelectItem>
</SelectContent>
</Select>
{/* Custom Input */}
{isCustomWidth && (
<input
type="number"
value={width}
onChange={(e) => setWidth(e.target.value)}
className="w-full h-10 border-2 border-primary/50 rounded-none px-3 font-medium text-sm bg-background mt-2 focus:outline-none focus:border-primary transition-colors"
placeholder="Width (256-1440)"
min="256"
max="1440"
step="32"
/>
)}
</div>
<div className="space-y-2">
<Label htmlFor="height" className="uppercase text-xs font-bold tracking-wider">Height</Label>
<Select
value={isCustomHeight ? "custom" : height}
onValueChange={(val) => {
if (val === "custom") setIsCustomHeight(true)
else {
setIsCustomHeight(false)
setHeight(val)
}
}}
>
<SelectTrigger id="height" className="h-12 border-2 border-border rounded-none font-bold bg-background/50">
<SelectValue placeholder="Height" />
</SelectTrigger>
<SelectContent className="rounded-none border-2 border-border neo-brutalist">
<SelectItem value="512">512px</SelectItem>
<SelectItem value="768">768px</SelectItem>
<SelectItem value="1024">1024px</SelectItem>
<SelectItem value="custom" className="text-primary font-bold">Custom...</SelectItem>
</SelectContent>
</Select>
{/* Custom Input */}
{isCustomHeight && (
<input
type="number"
value={height}
onChange={(e) => setHeight(e.target.value)}
className="w-full h-10 border-2 border-primary/50 rounded-none px-3 font-medium text-sm bg-background mt-2 focus:outline-none focus:border-primary transition-colors"
placeholder="Height (256-1440)"
min="256"
max="1440"
step="32"
/>
)}
</div>
</div>
<div className="space-y-2 pt-2">
<Label htmlFor="prompt" className="uppercase text-xs font-bold tracking-wider flex justify-between">
Prompt
<span className="text-muted-foreground text-[10px] font-normal tracking-normal normal-case">Be descriptive for best results</span>
</Label>
<Textarea
id="prompt"
placeholder="Describe your imagination in detail... (e.g. A futuristic neon city with flying cars, cyberpunk style, cinematic lighting)"
className="min-h-[140px] resize-none border-2 border-border rounded-none font-medium bg-background/50 focus-visible:ring-0 focus-visible:border-primary p-4 leading-relaxed"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
/>
</div>
{error && (
<div className="p-4 text-sm font-bold text-destructive bg-destructive/10 border-2 border-destructive/20 flex items-center gap-2 animate-in slide-in-from-left-2">
<AlertCircle className="h-4 w-4" />
{error}
</div>
)}
</div>
<div className="pt-2">
<Button
onClick={handleGenerate}
className="w-full h-14 text-lg bg-primary hover:bg-primary/90 text-primary-foreground font-black uppercase tracking-widest rounded-none md:neo-brutalist"
disabled={isPending || !prompt}
>
{isPending ? (
<>
<Loader2 className="mr-3 h-5 w-5 animate-spin" />
Dreaming...
</>
) : (
<>
<Wand2 className="mr-3 h-5 w-5" />
Generate Art
</>
)}
</Button>
</div>
</div>
</div>
{/* Right Column: Canvas */}
<div className="lg:col-span-3 h-full">
<div className="h-full min-h-[500px] glass-panel rounded-none p-2 md:p-4 flex flex-col relative group">
<div className="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-primary via-secondary to-accent opacity-50" />
<div className="flex-1 flex items-center justify-center bg-secondary/5 border-2 border-dashed border-border/30 relative overflow-hidden min-h-[400px]">
{/* Grid Pattern Background */}
<div className="absolute inset-0 bg-[linear-gradient(to_right,#80808012_1px,transparent_1px),linear-gradient(to_bottom,#80808012_1px,transparent_1px)] bg-[size:24px_24px] pointer-events-none" />
{resultUrl ? (
<div className="relative w-full h-full min-h-[inherit] flex items-center justify-center p-4">
<Image
src={resultUrl}
alt={prompt}
width={1024}
height={1024}
className="object-contain max-h-[70vh] w-auto h-auto shadow-2xl border-4 border-black dark:border-white"
unoptimized
/>
<div className="absolute bottom-4 right-4 flex gap-3 opacity-0 group-hover:opacity-100 transition-opacity duration-300">
<Button variant="secondary" className="neo-brutalist h-10 w-10 p-0 rounded-full" asChild>
<a href={resultUrl} target="_blank" rel="noopener noreferrer">
<Sparkles className="h-4 w-4" />
</a>
</Button>
<Button className="neo-brutalist h-10 w-10 p-0 rounded-full bg-primary text-white" asChild>
<a href={resultUrl} download="wild-art-generated.png">
<Download className="h-4 w-4" />
</a>
</Button>
</div>
</div>
) : (
<div className="text-center text-muted-foreground p-8 max-w-sm relative z-10">
{isPending ? (
<div className="flex flex-col items-center gap-6">
<div className="relative">
<div className="absolute inset-0 bg-primary/20 blur-xl animate-pulse rounded-full" />
<Loader2 className="h-16 w-16 animate-spin text-primary relative z-10" />
</div>
<div className="space-y-2">
<p className="font-bold text-xl animate-pulse text-foreground">Materializing...</p>
<p className="text-xs uppercase tracking-widest opacity-70">Computing Tensors • Denoising • Upscaling</p>
</div>
</div>
) : (
<div className="flex flex-col items-center gap-4 group/placeholder cursor-default">
<div className="h-24 w-24 rounded-full bg-secondary/20 flex items-center justify-center mb-4 group-hover/placeholder:scale-110 transition-transform duration-500 border-2 border-dashed border-muted-foreground/30">
<Sparkles className="h-10 w-10 text-muted-foreground/50" />
</div>
<h3 className="font-bold text-xl text-foreground">Canvas Empty</h3>
<p className="text-sm leading-relaxed opacity-70">
Your imagination is the only limit.<br />
Configure the settings on the left and hit <span className="text-primary font-bold">GENERATE</span>.
</p>
</div>
)}
</div>
)}
</div>
</div>
</div>
</div>
</div>
)
}