fixing thsu frontend views

This commit is contained in:
Micha
2026-01-16 01:43:07 +01:00
parent fd29b928d8
commit f9de433545
27 changed files with 538 additions and 51 deletions

View File

@@ -1,16 +1,18 @@
import { useEffect, useMemo, useState } from 'react'
import { Badge, Container } from 'react-bootstrap'
import { Container } from 'react-bootstrap'
import { Link } from 'react-router-dom'
import { getCurrentUser, listAllForums, listThreads } from '../api/client'
import { fetchStats, getCurrentUser, listAllForums, listThreads } from '../api/client'
import { useTranslation } from 'react-i18next'
import { useAuth } from '../context/AuthContext'
export default function Home() {
const [forums, setForums] = useState([])
const [threads, setThreads] = useState([])
const [stats, setStats] = useState({ threads: 0, posts: 0, users: 0 })
const [error, setError] = useState('')
const [loadingForums, setLoadingForums] = useState(true)
const [loadingThreads, setLoadingThreads] = useState(true)
const [loadingStats, setLoadingStats] = useState(true)
const [profile, setProfile] = useState(null)
const { token, roles, email } = useAuth()
const { t } = useTranslation()
@@ -29,6 +31,21 @@ export default function Home() {
.finally(() => setLoadingThreads(false))
}, [])
useEffect(() => {
fetchStats()
.then((data) => {
setStats({
threads: data?.threads ?? 0,
posts: data?.posts ?? 0,
users: data?.users ?? 0,
})
})
.catch(() => {
setStats({ threads: 0, posts: 0, users: 0 })
})
.finally(() => setLoadingStats(false))
}, [])
useEffect(() => {
if (!token) {
setProfile(null)
@@ -110,6 +127,19 @@ export default function Home() {
return t('portal.user_role_member')
}, [roles, t])
const formatDateTime = (value) => {
if (!value) return '—'
const date = new Date(value)
if (Number.isNaN(date.getTime())) return '—'
const day = String(date.getDate()).padStart(2, '0')
const month = String(date.getMonth() + 1).padStart(2, '0')
const year = String(date.getFullYear())
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')
return `${day}.${month}.${year} ${hours}:${minutes}:${seconds}`
}
const resolveForumName = (thread) => {
if (!thread?.forum) return t('portal.unknown_forum')
const parts = thread.forum.split('/')
@@ -138,7 +168,7 @@ export default function Home() {
<Link to={`/forum/${node.id}`} className="bb-forum-link fw-semibold">
{node.name}
</Link>
<div className="bb-muted">{node.description || t('forum.no_description')}</div>
<div className="bb-muted">{node.description || ''}</div>
</div>
</div>
</div>
@@ -165,11 +195,15 @@ export default function Home() {
<div className="bb-portal-card-title">{t('portal.stats')}</div>
<div className="bb-portal-stat">
<span>{t('portal.stat_threads')}</span>
<strong>{threads.length}</strong>
<strong>{loadingStats ? '—' : stats.threads}</strong>
</div>
<div className="bb-portal-stat">
<span>{t('portal.stat_forums')}</span>
<strong>{forums.length}</strong>
<span>{t('portal.stat_users')}</span>
<strong>{loadingStats ? '—' : stats.users}</strong>
</div>
<div className="bb-portal-stat">
<span>{t('portal.stat_posts')}</span>
<strong>{loadingStats ? '—' : stats.posts}</strong>
</div>
</div>
</aside>
@@ -201,9 +235,18 @@ export default function Home() {
</Link>
<div className="bb-portal-topic-meta">
<span>{t('thread.by')}</span>
<Badge bg="secondary">
{thread.user_name || t('thread.anonymous')}
</Badge>
{thread.user_id ? (
<Link
to={`/profile/${thread.user_id}`}
className="bb-portal-topic-author"
>
{thread.user_name || t('thread.anonymous')}
</Link>
) : (
<span className="bb-portal-topic-author">
{thread.user_name || t('thread.anonymous')}
</span>
)}
<span className="bb-portal-topic-forum">
{t('portal.forum_label')}{' '}
{resolveForumId(thread) ? (
@@ -220,10 +263,30 @@ export default function Home() {
</div>
</div>
</div>
<div className="bb-portal-topic-cell">0</div>
<div className="bb-portal-topic-cell"></div>
<div className="bb-portal-topic-cell">{thread.posts_count ?? 0}</div>
<div className="bb-portal-topic-cell">{thread.views_count ?? 0}</div>
<div className="bb-portal-topic-cell">
{thread.created_at?.slice(0, 10) || '—'}
<div className="bb-portal-last">
<span className="bb-portal-last-by">
{t('thread.by')}{' '}
{thread.last_post_user_id ? (
<Link
to={`/profile/${thread.last_post_user_id}`}
className="bb-portal-last-link"
>
{thread.last_post_user_name || t('thread.anonymous')}
<i className="bi bi-box-arrow-up-right" aria-hidden="true" />
</Link>
) : (
<span className="bb-portal-last-link">
{thread.last_post_user_name || t('thread.anonymous')}
</span>
)}
</span>
<span className="bb-portal-last-date">
{formatDateTime(thread.last_post_at || thread.created_at)}
</span>
</div>
</div>
</div>
))}