Files
aragrp/app/blog/[slug]/page.tsx
T
2026-02-05 14:54:19 +03:30

133 lines
5.1 KiB
TypeScript

import type { Metadata } from "next";
import Link from "next/link";
import Image from "next/image";
import { notFound } from "next/navigation";
import { getPostBySlug, getFeaturedImageUrl } from "@/lib/wp-api";
import { formatDateShamsi } from "@/lib/format-date";
import { ChevronRight } from "lucide-react";
import { parseContentHeadings, getReadingTimeMinutes } from "@/lib/blog-utils";
import { BlogToc } from "@/components/blog-toc";
import { BlogPostMeta } from "@/components/blog-post-meta";
import { BlogCollapseSection } from "@/components/blog-collapse-section";
import { ScrollToTop } from "@/components/scroll-to-top";
interface BlogPostPageProps {
params: Promise<{ slug: string }>;
}
export async function generateMetadata({
params,
}: BlogPostPageProps): Promise<Metadata> {
const { slug } = await params;
const post = await getPostBySlug(slug);
if (!post) return { title: "پست یافت نشد" };
const title = post.title?.rendered ?? "پست";
const excerpt = post.excerpt?.rendered?.replace(/<[^>]*>/g, "").trim().slice(0, 160) ?? "";
return {
title,
description: excerpt,
};
}
export default async function BlogPostPage({ params }: BlogPostPageProps) {
const { slug } = await params;
let post;
try {
post = await getPostBySlug(slug);
} catch {
notFound();
}
if (!post) notFound();
const date = post.date ? formatDateShamsi(post.date) : null;
const modifiedDate = post.modified
? formatDateShamsi(post.modified)
: null;
const authorName = post._embedded?.author?.[0]?.name ?? null;
const thumbnailUrl = getFeaturedImageUrl(post, "full");
const media = post._embedded?.["wp:featuredmedia"]?.[0];
const altText = media?.alt_text || post.title?.rendered?.replace(/<[^>]*>/g, "") || "";
const { toc, segments } = parseContentHeadings(post.content.rendered);
const readingTimeMinutes = getReadingTimeMinutes(post.content.rendered);
const postTitle = post.title?.rendered?.replace(/<[^>]*>/g, "") ?? "";
const postPath = `/blog/${slug}`;
return (
<>
{/* تصویر شاخص تمام‌عرض بالای صفحه */}
{thumbnailUrl && (
<div className="pt-28 md:pt-36">
<div className="relative w-full aspect-[21/5] min-h-[10rem] md:min-h-[10rem] bg-muted">
<Image
src={thumbnailUrl}
alt={altText}
fill
className="object-cover"
sizes="100vw"
priority
/>
</div>
</div>
)}
<section className="bg-background pt-8 pb-20 md:pt-12 md:pb-28">
<div className="mx-auto max-w-6xl px-6">
{/* Single grid so sticky TOC stays for whole page scroll */}
<div className="lg:grid lg:grid-cols-[16rem_1fr] lg:gap-10">
<BlogToc items={toc} className="mb-6 lg:mb-0" />
<div className="min-w-0 max-w-3xl space-y-0">
<div className="pb-8 md:pb-12">
<nav className="mb-6" aria-label="مسیر">
<Link
href="/blog"
className="text-muted-foreground hover:text-foreground text-sm inline-flex items-center gap-1 transition-colors"
>
<ChevronRight className="size-4" aria-hidden />
بلاگ
</Link>
</nav>
<h1
className="text-2xl font-bold md:text-3xl leading-tight tracking-normal"
dangerouslySetInnerHTML={{ __html: post.title.rendered }}
/>
<BlogPostMeta
url={postPath}
title={postTitle}
readingTimeMinutes={readingTimeMinutes}
date={date}
dateTime={post.date}
modifiedDate={modifiedDate}
authorName={authorName}
className="mt-4"
/>
</div>
<article className="blog-content text-justify space-y-6 text-foreground [&_h2]:text-2xl [&_h2]:font-bold [&_h2]:mt-8 [&_h2]:mb-4 [&_h3]:text-xl [&_h3]:font-bold [&_h3]:mt-6 [&_h3]:mb-3 [&_p]:leading-relaxed [&_p]:text-muted-foreground [&_a]:text-primary [&_a]:no-underline hover:[&_a]:underline [&_ul]:list-disc [&_ul]:pr-6 [&_ol]:list-decimal [&_ol]:pr-6 [&_li]:my-1 [&_h2]:scroll-mt-24 [&_h3]:scroll-mt-24 [&_h4]:scroll-mt-24">
{segments.map((seg, i) =>
seg.type === "html" ? (
<div
key={i}
dangerouslySetInnerHTML={{ __html: seg.content }}
/>
) : seg.blockType === "collapse" ? (
<BlogCollapseSection
key={i}
section={seg.payload}
idPrefix={`blog-${slug}-${i}`}
/>
) : (
/* سایر بلاک‌ها (blockquote, code, ...) بعداً با کامپوننت خودشان رندر می‌شوند */
null
)
)}
</article>
</div>
</div>
</div>
</section>
<ScrollToTop />
</>
);
}