import { useEffect, useState } from 'react' import { Button, Badge, Card, Col, Container, Form, Modal, Row } from 'react-bootstrap' import { Link, useParams } from 'react-router-dom' import { createThread, getForum, listForumsByParent, listThreadsByForum } from '../api/client' import { useAuth } from '../context/AuthContext' import { useTranslation } from 'react-i18next' export default function ForumView() { const { id } = useParams() const { token } = useAuth() const [forum, setForum] = useState(null) const [children, setChildren] = useState([]) const [threads, setThreads] = useState([]) const [error, setError] = useState('') const [loading, setLoading] = useState(true) const [showModal, setShowModal] = useState(false) const [title, setTitle] = useState('') const [body, setBody] = useState('') const [saving, setSaving] = useState(false) const { t } = useTranslation() const renderChildRows = (nodes) => nodes.map((node) => (
{node.name}
{node.description || t('forum.no_description')}
{t('thread.no_replies')}
)) useEffect(() => { let active = true const loadData = async () => { setLoading(true) setError('') try { const forumData = await getForum(id) if (!active) return setForum(forumData) const childData = await listForumsByParent(id) if (!active) return setChildren(childData) if (forumData.type === 'forum') { const threadData = await listThreadsByForum(id) if (!active) return setThreads(threadData) } else { setThreads([]) } } catch (err) { if (active) setError(err.message) } finally { if (active) setLoading(false) } } loadData() return () => { active = false } }, [id]) const handleSubmit = async (event) => { event.preventDefault() setSaving(true) setError('') try { await createThread({ title, body, forumId: id }) setTitle('') setBody('') const updated = await listThreadsByForum(id) setThreads(updated) setShowModal(false) } catch (err) { setError(err.message) } finally { setSaving(false) } } return ( {loading &&

{t('forum.loading')}

} {error &&

{error}

} {forum && ( <> {forum.type !== 'forum' && (
{forum.name}
{t('portal.topic')} {t('thread.views')} {t('thread.last_post')}
{children.length > 0 ? ( renderChildRows(children) ) : (
{t('forum.empty_children')}
)}
)} {forum.type === 'forum' && ( <>