36a4354ff7
- lib/forum.ts: countTopics/countPosts + offset/limit en getTopics/getPosts (mismos límites que Django: FORUM_TOPICS_PER_PAGE=20, FORUM_POSTS_PER_PAGE=15). - components/Pagination.tsx: paginación reutilizable (ventana ±2, primera/última, prev/next) con enlaces ?page=N; no renderiza si solo hay una página. - Foro y tema aceptan ?page=N (clamp al rango válido). - El formulario de respuesta solo aparece en la última página; enlace a ella si no. - i18n: namespace Common (prev/next/page) + replyOnLastPage. Build OK. Sin datos de foro en acore_web (estado vacío correcto). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
import { notFound } from 'next/navigation'
|
|
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
|
import { Link } from '@/i18n/navigation'
|
|
import { getForum, getTopics, countTopics } from '@/lib/forum'
|
|
import { getSession } from '@/lib/session'
|
|
import { NewTopicForm } from '@/components/NewTopicForm'
|
|
import { Pagination } from '@/components/Pagination'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
const PER_PAGE = 20
|
|
|
|
export default async function ForumPage({
|
|
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)
|
|
if (!forum) notFound()
|
|
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)
|
|
const session = await getSession()
|
|
const canPost = Boolean(session.username)
|
|
|
|
return (
|
|
<main className="mx-auto max-w-4xl px-4 py-8">
|
|
<p className="mb-2 text-sm">
|
|
<Link href="/forum" className="text-sky-400 hover:underline">
|
|
← {t('backToForum')}
|
|
</Link>
|
|
</p>
|
|
<h1 className="mb-1 text-2xl font-bold text-amber-500">{forum!.name}</h1>
|
|
{forum!.description && <p className="mb-6 text-amber-200/60">{forum!.description}</p>}
|
|
|
|
{topics.length === 0 ? (
|
|
<p className="text-amber-200/70">{t('noTopics')}</p>
|
|
) : (
|
|
<ul className="divide-y divide-amber-900/40">
|
|
{topics.map((topic) => (
|
|
<li key={topic.id} className="flex items-center justify-between gap-4 py-3">
|
|
<div>
|
|
<Link href={`/forum/topic/${topic.id}`} className="font-semibold text-amber-300 hover:text-amber-400">
|
|
{topic.sticky && <span className="mr-2 text-xs text-amber-500">[{t('sticky')}]</span>}
|
|
{topic.locked && <span className="mr-2 text-xs text-red-400">[{t('locked')}]</span>}
|
|
{topic.name}
|
|
</Link>
|
|
<p className="text-xs text-amber-200/60">
|
|
{t('by')} {topic.poster}
|
|
</p>
|
|
</div>
|
|
<div className="whitespace-nowrap text-xs text-amber-200/60">
|
|
{topic.views} {t('views')}
|
|
</div>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
|
|
<Pagination page={page} totalPages={totalPages} hrefFor={(p) => `/forum/${id}?page=${p}`} />
|
|
|
|
{canPost ? (
|
|
<NewTopicForm forumId={id} />
|
|
) : (
|
|
<p className="mt-8 text-sm text-amber-200/60">{t('loginToPost')}</p>
|
|
)}
|
|
</main>
|
|
)
|
|
}
|