129 lines
4.0 KiB
TypeScript
129 lines
4.0 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { api, ApiError } from "@/lib/api";
|
|
import { useAuth } from "@/lib/auth";
|
|
|
|
export default function CreatePage() {
|
|
const { user, loading } = useAuth();
|
|
const router = useRouter();
|
|
|
|
const [title, setTitle] = useState("");
|
|
const [prompt, setPrompt] = useState("");
|
|
const [mode, setMode] = useState<"instrumental" | "vocals">("instrumental");
|
|
const [lyrics, setLyrics] = useState("");
|
|
const [isPublic, setIsPublic] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!loading && !user) router.push("/login");
|
|
}, [loading, user, router]);
|
|
|
|
const onSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setBusy(true);
|
|
try {
|
|
await api.createSong({
|
|
title: title || prompt.slice(0, 60) || "Untitled",
|
|
prompt,
|
|
mode,
|
|
lyrics: mode === "vocals" ? lyrics : null,
|
|
is_public: isPublic,
|
|
});
|
|
router.push("/library");
|
|
} catch (err) {
|
|
setError(err instanceof ApiError ? err.message : "Something went wrong");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
if (loading || !user) return null;
|
|
|
|
return (
|
|
<div className="mx-auto max-w-2xl px-6 py-16">
|
|
<h1 className="text-3xl font-bold">Create a song</h1>
|
|
<p className="mt-2 text-white/50">
|
|
Describe the vibe — genre, mood, instruments, story. Generation happens in the background;
|
|
you'll see it appear in your library once it's ready.
|
|
</p>
|
|
|
|
<form onSubmit={onSubmit} className="mt-8 flex flex-col gap-5">
|
|
<label className="flex flex-col gap-1.5 text-sm text-white/70">
|
|
Title
|
|
<input
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
className="input"
|
|
placeholder="Midnight Drive"
|
|
/>
|
|
</label>
|
|
|
|
<label className="flex flex-col gap-1.5 text-sm text-white/70">
|
|
Prompt
|
|
<textarea
|
|
required
|
|
value={prompt}
|
|
onChange={(e) => setPrompt(e.target.value)}
|
|
rows={4}
|
|
className="textarea"
|
|
placeholder="Dreamy synthwave with driving bass, nostalgic and hopeful"
|
|
/>
|
|
</label>
|
|
|
|
<div className="flex gap-2 text-sm">
|
|
{(["instrumental", "vocals"] as const).map((m) => (
|
|
<button
|
|
type="button"
|
|
key={m}
|
|
onClick={() => setMode(m)}
|
|
className={`rounded-full px-4 py-1.5 font-medium capitalize transition-colors ${
|
|
mode === m ? "bg-white text-black" : "border border-white/15 text-white/70 hover:bg-white/10"
|
|
}`}
|
|
>
|
|
{m}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{mode === "vocals" && (
|
|
<label className="flex flex-col gap-1.5 text-sm text-white/70">
|
|
Lyrics
|
|
<textarea
|
|
required
|
|
value={lyrics}
|
|
onChange={(e) => setLyrics(e.target.value)}
|
|
rows={6}
|
|
className="textarea"
|
|
placeholder={"[Verse 1]\nWrite your lyrics here…"}
|
|
/>
|
|
</label>
|
|
)}
|
|
|
|
<label className="flex items-center gap-2 text-sm text-white/70">
|
|
<input
|
|
type="checkbox"
|
|
checked={isPublic}
|
|
onChange={(e) => setIsPublic(e.target.checked)}
|
|
className="h-4 w-4 rounded border-white/30 bg-transparent"
|
|
/>
|
|
Share to public feed
|
|
</label>
|
|
|
|
{error && <p className="text-sm text-red-400">{error}</p>}
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={busy}
|
|
className="mt-2 rounded-full bg-gradient-to-r from-fuchsia-500 to-cyan-400 px-6 py-3 text-sm font-semibold text-black hover:opacity-90 disabled:opacity-50"
|
|
>
|
|
{busy ? "Queuing generation…" : "Generate song"}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|