|
| 1 | +// src/app/blog/[slug]/page.tsx |
| 2 | +import { notFound } from "next/navigation"; |
| 3 | +import Link from "next/link"; |
| 4 | +import type { Metadata } from "next"; |
| 5 | +import { Navbar } from "../../../../components/layout/Navbar"; |
| 6 | +import { Footer } from "../../../../components/layout/Footer"; |
| 7 | +import { formatDate, getAllPosts, getPostBySlug } from "@/lib/post"; |
| 8 | +import { PostBody } from "@/components/PostBody"; |
| 9 | +import { PostCard } from "@/components/ PostCard"; |
| 10 | + |
| 11 | +// Static generation: pre-render all slugs at build time |
| 12 | +export async function generateStaticParams() { |
| 13 | + return getAllPosts().map((p) => ({ slug: p.slug })); |
| 14 | +} |
| 15 | + |
| 16 | +// Per-post metadata |
| 17 | +export async function generateMetadata({ |
| 18 | + params, |
| 19 | +}: { |
| 20 | + params: { slug: string }; |
| 21 | +}): Promise<Metadata> { |
| 22 | + const post = getPostBySlug(params.slug); |
| 23 | + if (!post) return {}; |
| 24 | + return { |
| 25 | + title: `${post.title} — Stellar Suite Blog`, |
| 26 | + description: post.excerpt, |
| 27 | + openGraph: { |
| 28 | + title: post.title, |
| 29 | + description: post.excerpt, |
| 30 | + type: "article", |
| 31 | + publishedTime: post.date, |
| 32 | + }, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +export default function PostPage({ params }: { params: { slug: string } }) { |
| 37 | + const post = getPostBySlug(params.slug); |
| 38 | + if (!post) notFound(); |
| 39 | + |
| 40 | + const allPosts = getAllPosts(); |
| 41 | + const currentIndex = allPosts.findIndex((p) => p.slug === post.slug); |
| 42 | + const related = allPosts |
| 43 | + .filter( |
| 44 | + (p) => |
| 45 | + p.slug !== post.slug && |
| 46 | + (p.category === post.category || |
| 47 | + p.tags.some((t) => post.tags.includes(t))), |
| 48 | + ) |
| 49 | + .slice(0, 2); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="min-h-screen bg-background"> |
| 53 | + <Navbar /> |
| 54 | + |
| 55 | + <main id="main-content" className="pt-28 pb-20 px-6"> |
| 56 | + <div className="mx-auto max-w-3xl"> |
| 57 | + {/* Breadcrumb */} |
| 58 | + <nav className="flex items-center gap-2 mb-8 text-sm font-body text-muted-foreground"> |
| 59 | + <Link href="/" className="hover:text-foreground transition-colors"> |
| 60 | + Home |
| 61 | + </Link> |
| 62 | + <span className="text-muted-foreground/40">/</span> |
| 63 | + <Link |
| 64 | + href="/blog" |
| 65 | + className="hover:text-foreground transition-colors" |
| 66 | + > |
| 67 | + Blog |
| 68 | + </Link> |
| 69 | + <span className="text-muted-foreground/40">/</span> |
| 70 | + <span className="text-foreground truncate max-w-[200px]"> |
| 71 | + {post.title} |
| 72 | + </span> |
| 73 | + </nav> |
| 74 | + |
| 75 | + {/* Category badge */} |
| 76 | + <div className="mb-5"> |
| 77 | + <Link |
| 78 | + href={`/blog?category=${post.category}`} |
| 79 | + className="inline-flex items-center rounded-full bg-primary/10 px-3 py-1 text-xs font-semibold text-primary font-display tracking-wide hover:bg-primary/20 transition-colors" |
| 80 | + > |
| 81 | + {post.category} |
| 82 | + </Link> |
| 83 | + </div> |
| 84 | + |
| 85 | + {/* Title */} |
| 86 | + <h1 className="text-3xl md:text-4xl lg:text-5xl font-display font-extrabold text-foreground leading-tight tracking-tight mb-6"> |
| 87 | + {post.title} |
| 88 | + </h1> |
| 89 | + |
| 90 | + {/* Meta row */} |
| 91 | + <div className="flex flex-wrap items-center gap-x-5 gap-y-2 mb-3 pb-8 border-b border-border"> |
| 92 | + <div className="flex items-center gap-2"> |
| 93 | + <div className="h-7 w-7 rounded-full bg-primary/20 flex items-center justify-center"> |
| 94 | + <span className="text-xs font-bold text-primary font-display"> |
| 95 | + {post.author.name[0]} |
| 96 | + </span> |
| 97 | + </div> |
| 98 | + <span className="text-sm font-semibold text-foreground font-body"> |
| 99 | + {post.author.name} |
| 100 | + </span> |
| 101 | + </div> |
| 102 | + <span className="text-muted-foreground text-sm font-body"> |
| 103 | + {formatDate(post.date)} |
| 104 | + </span> |
| 105 | + <span className="text-muted-foreground text-sm font-body"> |
| 106 | + {post.readingTime} |
| 107 | + </span> |
| 108 | + </div> |
| 109 | + |
| 110 | + {/* Tags */} |
| 111 | + <div className="flex flex-wrap gap-2 mb-10"> |
| 112 | + {post.tags.map((tag) => ( |
| 113 | + <Link |
| 114 | + key={tag} |
| 115 | + href={`/blog?tag=${tag}`} |
| 116 | + className="text-xs font-mono text-muted-foreground border border-border rounded px-2 py-1 hover:border-primary/40 hover:text-primary transition-colors" |
| 117 | + > |
| 118 | + #{tag} |
| 119 | + </Link> |
| 120 | + ))} |
| 121 | + </div> |
| 122 | + |
| 123 | + {/* Excerpt (lead) */} |
| 124 | + <p className="text-lg font-body text-muted-foreground leading-relaxed mb-10 pb-10 border-b border-border"> |
| 125 | + {post.excerpt} |
| 126 | + </p> |
| 127 | + |
| 128 | + {/* Post content */} |
| 129 | + <PostBody content={post.content} /> |
| 130 | + |
| 131 | + {/* Post nav: prev/next */} |
| 132 | + <div className="mt-16 pt-8 border-t border-border flex items-center justify-between gap-4"> |
| 133 | + {currentIndex < allPosts.length - 1 ? ( |
| 134 | + <Link |
| 135 | + href={`/blog/${allPosts[currentIndex + 1].slug}`} |
| 136 | + className="group flex items-center gap-2 text-sm font-body text-muted-foreground hover:text-foreground transition-colors" |
| 137 | + > |
| 138 | + <svg |
| 139 | + width="16" |
| 140 | + height="16" |
| 141 | + fill="none" |
| 142 | + viewBox="0 0 24 24" |
| 143 | + stroke="currentColor" |
| 144 | + strokeWidth={2} |
| 145 | + className="group-hover:-translate-x-0.5 transition-transform" |
| 146 | + > |
| 147 | + <path |
| 148 | + strokeLinecap="round" |
| 149 | + strokeLinejoin="round" |
| 150 | + d="M7 16l-4-4m0 0l4-4m-4 4h18" |
| 151 | + /> |
| 152 | + </svg> |
| 153 | + <span className="truncate max-w-[200px]"> |
| 154 | + {allPosts[currentIndex + 1].title} |
| 155 | + </span> |
| 156 | + </Link> |
| 157 | + ) : ( |
| 158 | + <div /> |
| 159 | + )} |
| 160 | + {currentIndex > 0 ? ( |
| 161 | + <Link |
| 162 | + href={`/blog/${allPosts[currentIndex - 1].slug}`} |
| 163 | + className="group flex items-center gap-2 text-sm font-body text-muted-foreground hover:text-foreground transition-colors text-right" |
| 164 | + > |
| 165 | + <span className="truncate max-w-[200px]"> |
| 166 | + {allPosts[currentIndex - 1].title} |
| 167 | + </span> |
| 168 | + <svg |
| 169 | + width="16" |
| 170 | + height="16" |
| 171 | + fill="none" |
| 172 | + viewBox="0 0 24 24" |
| 173 | + stroke="currentColor" |
| 174 | + strokeWidth={2} |
| 175 | + className="group-hover:translate-x-0.5 transition-transform" |
| 176 | + > |
| 177 | + <path |
| 178 | + strokeLinecap="round" |
| 179 | + strokeLinejoin="round" |
| 180 | + d="M17 8l4 4m0 0l-4 4m4-4H3" |
| 181 | + /> |
| 182 | + </svg> |
| 183 | + </Link> |
| 184 | + ) : ( |
| 185 | + <div /> |
| 186 | + )} |
| 187 | + </div> |
| 188 | + |
| 189 | + {/* Back to blog */} |
| 190 | + <div className="mt-8 text-center"> |
| 191 | + <Link href="/blog" className="btn-outline text-sm"> |
| 192 | + ← Back to Blog |
| 193 | + </Link> |
| 194 | + </div> |
| 195 | + </div> |
| 196 | + |
| 197 | + {/* Related posts */} |
| 198 | + {related.length > 0 && ( |
| 199 | + <div className="mx-auto max-w-6xl mt-20 pt-12 border-t border-border"> |
| 200 | + <h2 className="text-xl font-display font-bold text-foreground mb-8"> |
| 201 | + Related Posts |
| 202 | + </h2> |
| 203 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-6"> |
| 204 | + {related.map((p) => ( |
| 205 | + <PostCard key={p.slug} post={p} /> |
| 206 | + ))} |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + )} |
| 210 | + </main> |
| 211 | + |
| 212 | + <Footer /> |
| 213 | + </div> |
| 214 | + ); |
| 215 | +} |
0 commit comments