import { useEffect, useState } from 'react' import { Button, Card, Col, Container, Form, Row } from 'react-bootstrap' import { Link, useParams } from 'react-router-dom' import { createPost, getThread, listPostsByThread } from '../api/client' import { useAuth } from '../context/AuthContext' import { useTranslation } from 'react-i18next' export default function ThreadView() { const { id } = useParams() const { token } = useAuth() const [thread, setThread] = useState(null) const [posts, setPosts] = useState([]) const [error, setError] = useState('') const [loading, setLoading] = useState(true) const [body, setBody] = useState('') const [saving, setSaving] = useState(false) const { t } = useTranslation() useEffect(() => { setLoading(true) Promise.all([getThread(id), listPostsByThread(id)]) .then(([threadData, postData]) => { setThread(threadData) setPosts(postData) }) .catch((err) => setError(err.message)) .finally(() => setLoading(false)) }, [id]) const handleSubmit = async (event) => { event.preventDefault() setSaving(true) setError('') try { await createPost({ body, threadId: id }) setBody('') const updated = await listPostsByThread(id) setPosts(updated) } catch (err) { setError(err.message) } finally { setSaving(false) } } return ( {loading &&

{t('thread.loading')}

} {error &&

{error}

} {thread && ( <>

{t('thread.label')}

{thread.title}

{thread.body}

{thread.forum && (

{t('thread.category')}{' '} {thread.forum.name || t('thread.back_to_category')}

)}

{t('thread.replies')}

{posts.length === 0 && (

{t('thread.empty')}

)} {posts.map((post) => ( {post.body} {post.author?.username || t('thread.anonymous')} ))}

{t('thread.reply')}

{!token && (

{t('thread.login_hint')}

)}
{t('form.message')} setBody(event.target.value)} disabled={!token || saving} required />
)}
) }