'use client'; import { useState, type FormEvent } from 'react'; import { useRouter } from 'next/navigation'; import { ORDER_MESSAGE_LIMIT } from '@/app/lib/order-messages'; export function CustomerOrderMessageForm({ orderId, proofId }: { orderId: string; proofId?: string }) { const router = useRouter(); const [note, setNote] = useState(''); const [sending, setSending] = useState(false); const [feedback, setFeedback] = useState<{ text: string; error?: boolean } | null>(null); const send = async (event: FormEvent) => { event.preventDefault(); if (sending || !note.trim()) return; setSending(true); setFeedback(null); try { const response = await fetch(`/api/orders/${orderId}/message`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ note: note.trim(), generationId: proofId, question: !!proofId }), }); const result = await response.json().catch(() => null) as { ok?: boolean; emailSent?: boolean; proofReopened?: boolean; error?: string } | null; if (!response.ok || !result?.ok) throw new Error(result?.error ?? 'Could not send your message.'); setNote(''); const saved = result.proofReopened ? 'Feedback saved. Approval is paused while the studio reviews it.' : proofId ? 'Question sent.' : 'Message saved.'; setFeedback({ text: result.emailSent ? saved : `${saved} We couldn’t email the studio a notification.` }); router.refresh(); } catch (error) { setFeedback({ text: error instanceof Error ? error.message : 'Could not send your message.', error: true }); } finally { setSending(false); } }; return (