'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' import { useRouter } from '@/i18n/navigation' import { RichTextArea } from './RichTextArea' export function PostActions({ postId, initialText, deleted = false, }: { postId: number initialText: string deleted?: boolean }) { const t = useTranslations('Forum') const router = useRouter() const [editing, setEditing] = useState(false) const [text, setText] = useState(initialText) const [busy, setBusy] = useState(false) const [error, setError] = useState(null) async function save() { if (busy || !text.trim()) return setBusy(true) setError(null) try { const res = await fetch('/api/forum/post', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ postId, text }), }) const data: { success?: boolean; error?: string } = await res.json() if (data.success) { setEditing(false) router.refresh() } else { setError(t(data.error === 'tooShort' ? 'tooShort' : data.error === 'topicLocked' ? 'topicLocked' : 'genericError')) } } catch { setError(t('genericError')) } finally { setBusy(false) } } async function restore() { if (busy) return setBusy(true) setError(null) try { const res = await fetch('/api/forum/post', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ postId }), }) if ((await res.json()).success) router.refresh() else setError(t('genericError')) } catch { setError(t('genericError')) } finally { setBusy(false) } } async function remove() { if (busy || !confirm(t('confirmDeletePost'))) return setBusy(true) setError(null) try { const res = await fetch('/api/forum/post', { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ postId }), }) const data: { success?: boolean } = await res.json() if (data.success) router.refresh() else setError(t('genericError')) } catch { setError(t('genericError')) } finally { setBusy(false) } } if (editing) { return (

{' '} {error &&

{error}

}
) } if (deleted) { return (
{error && {error}}
) } return (
setEditing(true)} className="second-brown" style={{ cursor: 'pointer' }}> {t('edit')} {' '} { if (!busy) remove() }} className="second-brown" style={{ cursor: 'pointer' }}> {t('delete')} {error && {error}}
) }