import { notFound } from 'next/navigation' import { getTranslations, setRequestLocale } from 'next-intl/server' import { Link } from '@/i18n/navigation' import { getForum, getForumPath, countTopics, getTopics, resolvePosters } from '@/lib/forum' import { formatForumDate } from '@/lib/forum-format' import { getSession } from '@/lib/session' import { forumIsModerator } from '@/lib/forum-perm' import { PageShell } from '@/components/PageShell' import { ForumBreadcrumb } from '@/components/ForumBreadcrumb' import { Pagination } from '@/components/Pagination' export const dynamic = 'force-dynamic' const PER_PAGE = 10 export default async function SubforumPage({ params, searchParams, }: { params: Promise<{ locale: string; forumId: string }> searchParams: Promise<{ page?: string }> }) { const { locale, forumId } = await params setRequestLocale(locale) const t = await getTranslations('Forum') const id = Number(forumId) const forum = await getForum(id, locale) if (!forum) notFound() const session = await getSession() const isMod = await forumIsModerator(session) const canCreate = Boolean(session.username) const total = await countTopics(id) const totalPages = Math.max(1, Math.ceil(total / PER_PAGE)) const page = Math.min(Math.max(1, Number((await searchParams).page) || 1), totalPages) const topics = await getTopics(id, (page - 1) * PER_PAGE, PER_PAGE, isMod) const posters = await resolvePosters( topics.flatMap((tp) => [tp.poster, tp.last_poster].filter(Boolean) as number[]), ) const name = (pid: number | null) => (pid != null ? posters.get(pid)?.name ?? `#${pid}` : '') const path = await getForumPath(id, locale) const hrefFor = (p: number) => `/forum/${id}?page=${p}` return (
{path && }

{forum.name}

{forum.description &&

{forum.description}

}

{total} {t('topics')}

{canCreate ? ( {t('createTopic')} ) : ( )}
{topics.length === 0 ? (

{t('noTopics')}

) : ( topics.map((tp) => (
  • {tp.deleted && `[${t('deleted')}] `} {tp.sticky && `[${t('sticky')}] `} {tp.locked && `[${t('locked')}] `} {tp.name}

    {t('createdBy')} {name(tp.poster)},{' '} {formatForumDate(tp.created, locale)}

  • {t('by')} {name(tp.last_poster)}

    {formatForumDate(tp.time_updated, locale)}
)) )}
) }