"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Github, Loader2, ArrowRight } from "lucide-react"; const GITHUB_URL_REGEX = /^https?:\/\/(www\.)?github\.com\/[\w.-]+\/[\w.-]+\/?$/; export function RepoInput() { const [url, setUrl] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const router = useRouter(); const isValid = GITHUB_URL_REGEX.test(url); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!isValid) { setError("Please enter a valid GitHub repository URL"); return; } setError(null); setIsLoading(true); try { const response = await fetch("/api/generate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ repoUrl: url.trim() }), }); if (!response.ok) { const data = await response.json(); throw new Error(data.error || "Failed to start generation"); } const data = await response.json(); router.push(`/generate?repo=${encodeURIComponent(url)}&id=${data.id}`); } catch (err) { setError( err instanceof Error ? err.message : "An unexpected error occurred" ); setIsLoading(false); } }; return (
{ setUrl(e.target.value); setError(null); }} placeholder="https://github.com/user/repo" className="w-full pl-12 pr-4 py-4 bg-black/40 border border-white/10 rounded-xl text-white placeholder-zinc-500 focus:outline-none focus:border-blue-500/50 focus:ring-2 focus:ring-blue-500/20 transition-all" disabled={isLoading} />
{error && (
{error}
)}
{isValid ? "Valid GitHub URL" : "Enter a public GitHub repository URL"}
); }