import { useState, useEffect } from 'react'; import { api } from '../utils/api.js'; function generateCaptcha() { const a = Math.floor(Math.random() * 9) + 1; const b = Math.floor(Math.random() * 9) + 1; const ops = [ { label: `${a} + ${b}`, answer: a + b }, { label: `${a + b} - ${b}`, answer: a }, { label: `${a} × ${b}`, answer: a * b }, ]; return ops[Math.floor(Math.random() * ops.length)]; } export default function SupportModal({ onClose }) { const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [message, setMessage] = useState(''); const [captchaAnswer, setCaptchaAnswer] = useState(''); const [captcha, setCaptcha] = useState(generateCaptcha); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const [sent, setSent] = useState(false); const refreshCaptcha = () => { setCaptcha(generateCaptcha()); setCaptchaAnswer(''); }; const handleSubmit = async (e) => { e.preventDefault(); setError(''); if (!name.trim() || !email.trim() || !message.trim()) { return setError('Please fill in all fields.'); } if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { return setError('Please enter a valid email address.'); } if (parseInt(captchaAnswer, 10) !== captcha.answer) { setError('Incorrect answer — please try again.'); refreshCaptcha(); return; } setLoading(true); try { await api.submitSupport({ name, email, message }); setSent(true); } catch (err) { setError(err.message || 'Failed to send. Please try again.'); refreshCaptcha(); } finally { setLoading(false); } }; return (
e.target === e.currentTarget && onClose()}>
{sent ? ( /* Success state */

Message Sent

Your message has been received. An administrator will follow up with you shortly.

) : ( /* Form state */ <>

Contact Support

Fill out the form below and an administrator will get back to you.

setName(e.target.value)} autoComplete="new-password" maxLength={100} />
setEmail(e.target.value)} autoComplete="new-password" maxLength={200} />