82 lines
2.9 KiB
TypeScript
82 lines
2.9 KiB
TypeScript
import type { Metadata } from "next";
|
||||
|
|
import Link from "next/link";
|
|||
|
|
import { notFound, redirect } from "next/navigation";
|
|||
|
|
|
|||
|
|
import { ThreadList } from "@/components/forum/ThreadList";
|
|||
|
|
import type { TopicPage } from "@/lib/forum";
|
|||
|
|
import { forumGet } from "@/lib/forumServer";
|
|||
|
|
|
|||
|
|
type Props = { params: Promise<{ tema: string }>; searchParams: Promise<{ oldal?: string }> };
|
|||
|
|
|
|||
|
|
async function load(slug: string, page: number) {
|
|||
|
|
return forumGet<TopicPage>(`/v1/forum/topics/${encodeURIComponent(slug)}?page=${page}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function pageOf(raw: string | undefined): number {
|
|||
|
|
const n = Number(raw ?? 1);
|
|||
|
|
return Number.isInteger(n) && n > 0 ? n : 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
|||
|
|
const { tema } = await params;
|
|||
|
|
const res = await load(tema, 1);
|
|||
|
|
if (!res.ok) return { title: "Fórum" };
|
|||
|
|
const { topic } = res.data;
|
|||
|
|
// Members-only topics stay out of search engines.
|
|||
|
|
return {
|
|||
|
|
title: topic.name,
|
|||
|
|
description: topic.description,
|
|||
|
|
robots: topic.visibility === "public" ? undefined : { index: false, follow: false },
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** A topic's threads: pinned ones first, then by the latest post. */
|
|||
|
|
export default async function TopicPageView({ params, searchParams }: Props) {
|
|||
|
|
const { tema } = await params;
|
|||
|
|
const page = pageOf((await searchParams).oldal);
|
|||
|
|
const res = await load(tema, page);
|
|||
|
|
if (!res.ok) {
|
|||
|
|
if (res.status === 401) redirect(`/belepes?vissza=/forum/${tema}`);
|
|||
|
|
notFound();
|
|||
|
|
}
|
|||
|
|
const { topic, threads, has_more, viewer } = res.data;
|
|||
|
|
return (
|
|||
|
|
<div className="fh">
|
|||
|
|
<nav className="forum-crumbs" aria-label="Morzsamenü">
|
|||
|
|
<Link href="/forum">Fórum</Link>
|
|||
|
|
</nav>
|
|||
|
|
<header className="ftopic-head">
|
|||
|
|
<div>
|
|||
|
|
<h1>{topic.name}</h1>
|
|||
|
|
<p className="muted">{topic.description}</p>
|
|||
|
|
{topic.listening && (
|
|||
|
|
<p className="hint">
|
|||
|
|
Meghallgatós téma: itt nincs ⌄, és a szálak alapból meghallgatást kérnek, nem tanácsot.
|
|||
|
|
</p>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
<Link
|
|||
|
|
href={viewer.logged_in ? `/forum/uj?tema=${topic.slug}` : `/belepes?vissza=/forum/uj?tema=${topic.slug}`}
|
|||
|
|
className="btn btn-primary"
|
|||
|
|
>
|
|||
|
|
Új szál
|
|||
|
|
</Link>
|
|||
|
|
</header>
|
|||
|
|
{threads.length === 0 ? (
|
|||
|
|
<p className="fh-card fh-empty">Ebben a témában még nincs szál. Nyisd meg te az elsőt!</p>
|
|||
|
|
) : (
|
|||
|
|
<div className="fh-card fh-flush">
|
|||
|
|
<ThreadList threads={threads} showTopic={false} />
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
{(page > 1 || has_more) && (
|
|||
|
|
<nav className="forum-pager" aria-label="Lapozás">
|
|||
|
|
{page > 1 ? <Link href={`/forum/${topic.slug}?oldal=${page - 1}`}>‹ Újabbak</Link> : <span />}
|
|||
|
|
<span className="hint">{page}. oldal</span>
|
|||
|
|
{has_more ? <Link href={`/forum/${topic.slug}?oldal=${page + 1}`}>Régebbiek ›</Link> : <span />}
|
|||
|
|
</nav>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
);
|
|||
|
|
}
|