import React, { useState, useRef, useCallback } from 'react' import Button from '../ui/Button' import RichTextEditor from './RichTextEditor' import TurnstileField from '../security/TurnstileField' import { buildBotFingerprint } from '../../lib/security/botFingerprint' export default function ReplyForm({ topicKey, prefill = '', quotedAuthor = null, csrfToken, captcha = {} }) { const [content, setContent] = useState(prefill) const [captchaToken, setCaptchaToken] = useState('') const [submitting, setSubmitting] = useState(false) const [error, setError] = useState(null) const formRef = useRef(null) const handleSubmit = useCallback(async (e) => { e.preventDefault() if (submitting || content.trim().length < 2) return setSubmitting(true) setError(null) try { const fingerprint = await buildBotFingerprint() const res = await fetch(`/forum/topic/${topicKey}/reply`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrfToken, 'X-Bot-Fingerprint': fingerprint, 'X-Captcha-Token': captchaToken, 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest', }, credentials: 'same-origin', body: JSON.stringify({ content: content.trim(), homepage_url: '', _bot_fingerprint: fingerprint, [captcha.inputName || 'cf-turnstile-response']: captchaToken, }), }) if (res.ok) { // Reload page to show new reply window.location.reload() } else if (res.status === 422) { const json = await res.json() setError(json.errors?.content?.[0] ?? json.errors?.bot?.[0] ?? json.message ?? 'Validation error.') } else { const json = await res.json().catch(() => ({})) setError(json?.errors?.bot?.[0] ?? json?.message ?? 'Failed to post reply. Please try again.') } } catch { setError('Network error. Please try again.') } setSubmitting(false) }, [content, topicKey, csrfToken, submitting]) return (
) }