Spaces:
Running
Running
File size: 6,188 Bytes
60e766b d438808 60e766b 374bf33 60e766b d438808 60e766b d438808 60e766b 374bf33 60e766b d438808 60e766b ae601ea 60e766b d438808 60e766b d989816 60e766b | 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 | "use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useMemo, useState } from "react";
import { fetchWithAnon } from "@/lib/client-anon";
function parseErrorMessage(error: unknown): string {
if (typeof error === "string") return error;
if (!error || typeof error !== "object") return "Failed to generate requirements.";
const value = error as Record<string, unknown>;
if (typeof value.error === "string") return value.error;
if (typeof value.detail === "string") return value.detail;
return "Failed to generate requirements.";
}
export function SubmitForm({ canShowIdentity }: { canShowIdentity: boolean }) {
const router = useRouter();
const [rawText, setRawText] = useState("");
const [targetUsers, setTargetUsers] = useState("");
const [platform, setPlatform] = useState("Any");
const [constraints, setConstraints] = useState("");
const [showName, setShowName] = useState(false);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const chars = useMemo(() => rawText.length, [rawText.length]);
async function onSubmit(e: React.FormEvent) {
e.preventDefault();
setError("");
setLoading(true);
try {
const response = await fetchWithAnon("/api/ideas", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
raw_input_text: rawText,
target_users: targetUsers || undefined,
platform: platform === "Any" ? undefined : platform,
constraints: constraints || undefined,
show_name: showName,
}),
});
const data = await response.json();
if (!response.ok) {
throw new Error(parseErrorMessage(data));
}
if (data.merged && data.idea?.id) {
const reason = encodeURIComponent(data.reason || "DUPLICATE");
router.push(`/ideas/${data.idea.id}?merged=1&reason=${reason}`);
} else {
router.push(`/ideas/${data.idea.id}`);
}
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : "Unknown error.");
} finally {
setLoading(false);
}
}
return (
<form onSubmit={onSubmit} className="card reveal-up space-y-5" style={{ animationDelay: "80ms" }}>
<div className="grid gap-5 md:grid-cols-2">
<div className="md:col-span-2">
<div className="mb-1.5 flex items-center justify-between gap-2">
<label className="text-sm font-semibold">Idea text</label>
<span className={`text-xs ${chars < 20 ? "text-red-700" : "subtle"}`}>{chars}/3000</span>
</div>
<textarea
required
minLength={20}
maxLength={3000}
value={rawText}
onChange={(e) => setRawText(e.target.value)}
className="input min-h-52"
placeholder="Example: A neighborhood demand board where residents post local pain points and developers build solutions..."
/>
<p className="subtle mt-2 text-xs">Be concrete about the problem and expected outcome to get better generated tasks.</p>
</div>
<div>
<label className="mb-1.5 block text-sm font-semibold">Target users (optional)</label>
<input
value={targetUsers}
onChange={(e) => setTargetUsers(e.target.value)}
className="input"
placeholder="Independent creators, student teams, local shops..."
maxLength={300}
/>
</div>
<div>
<label className="mb-1.5 block text-sm font-semibold">Platform preference (optional)</label>
<select value={platform} onChange={(e) => setPlatform(e.target.value)} className="input">
<option>Any</option>
<option>Web</option>
<option>Mobile</option>
<option>Desktop</option>
</select>
</div>
<div className="md:col-span-2">
<label className="mb-1.5 block text-sm font-semibold">Constraints (optional)</label>
<input
value={constraints}
onChange={(e) => setConstraints(e.target.value)}
className="input"
placeholder="No login, privacy-first, low budget, integrate with GitHub only..."
maxLength={500}
/>
</div>
<div className="md:col-span-2 rounded-xl border border-zinc-200 bg-white/70 p-3">
<label className="inline-flex items-center gap-2 text-sm font-medium">
<input
type="checkbox"
checked={showName}
onChange={(e) => setShowName(e.target.checked)}
disabled={!canShowIdentity}
/>
Show my name publicly as idea submitter
</label>
{!canShowIdentity ? (
<p className="subtle mt-2 text-xs">
Login is required to publish with your visible name. <Link href="/login" className="underline">Sign in</Link>
</p>
) : (
<p className="subtle mt-2 text-xs">Default remains anonymous even when logged in.</p>
)}
</div>
</div>
<div className="rounded-xl border border-amber-200 bg-amber-50/80 p-3 text-xs text-amber-900">
Disclaimer: this is a best-effort generated specification. Review tasks and open questions before building.
</div>
<div className="flex flex-wrap items-center gap-3">
<button disabled={loading} className="btn btn-primary min-w-52">
{loading ? (
<span className="inline-flex items-center gap-2">
<span className="h-3.5 w-3.5 animate-spin rounded-full border-2 border-white/80 border-t-transparent" />
Generating requirements...
</span>
) : (
"Generate Requirements"
)}
</button>
<p className="subtle text-xs">Rate-limited to 20 submissions/hour per anonymous identity and IP.</p>
</div>
{error ? <p className="rounded-lg border border-red-200 bg-red-50 p-3 text-sm text-red-700">{error}</p> : null}
</form>
);
}
|