"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export default function NewQuestionPage() { const router = useRouter(); const [title, setTitle] = useState(""); const [text, setText] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!title.trim() || !text.trim()) return; setLoading(true); setError(""); try { const res = await fetch("/api/questions", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, text }), }); if (!res.ok) { const data = await res.json(); throw new Error(data.error ?? "Ошибка при создании вопроса"); } const q = await res.json(); router.push(`/questions/${q.id}`); } catch (err) { setError(err instanceof Error ? err.message : "Ошибка"); } finally { setLoading(false); } } return (