|
| 1 | +import { notFound } from 'next/navigation'; |
| 2 | +import { blog } from '@/app/source'; |
| 3 | +import defaultMdxComponents from 'fumadocs-ui/mdx'; |
| 4 | +import { HomeLayout } from 'fumadocs-ui/layouts/home'; |
| 5 | +import { baseOptions } from '@/app/layout.config'; |
| 6 | +import Link from 'next/link'; |
| 7 | +import { ArrowLeft } from 'lucide-react'; |
| 8 | + |
| 9 | +// Extended type for blog post data |
| 10 | +interface BlogPostData { |
| 11 | + title: string; |
| 12 | + description?: string; |
| 13 | + author?: string; |
| 14 | + date?: string; |
| 15 | + tags?: string[]; |
| 16 | + body: React.ComponentType; |
| 17 | +} |
| 18 | + |
| 19 | +export default async function BlogPage({ |
| 20 | + params, |
| 21 | +}: { |
| 22 | + params: Promise<{ lang: string; slug?: string[] }>; |
| 23 | +}) { |
| 24 | + const { slug } = await params; |
| 25 | + |
| 26 | + // If no slug, show blog index |
| 27 | + if (!slug || slug.length === 0) { |
| 28 | + const posts = blog.getPages(); |
| 29 | + |
| 30 | + return ( |
| 31 | + <HomeLayout {...baseOptions}> |
| 32 | + <main className="container max-w-5xl mx-auto px-4 py-16"> |
| 33 | + <div className="mb-12"> |
| 34 | + <h1 className="text-4xl font-bold mb-4">Blog</h1> |
| 35 | + <p className="text-lg text-fd-foreground/80"> |
| 36 | + Insights, updates, and best practices from the ObjectStack team. |
| 37 | + </p> |
| 38 | + </div> |
| 39 | + |
| 40 | + <div className="grid gap-8 md:grid-cols-2"> |
| 41 | + {posts.map((post) => { |
| 42 | + const postData = post.data as unknown as BlogPostData; |
| 43 | + return ( |
| 44 | + <Link |
| 45 | + key={post.url} |
| 46 | + href={post.url} |
| 47 | + className="group block rounded-lg border border-fd-border bg-fd-card p-6 transition-all hover:border-fd-primary/30 hover:shadow-md" |
| 48 | + > |
| 49 | + <div className="mb-3"> |
| 50 | + <h2 className="text-2xl font-semibold mb-2 group-hover:text-fd-primary transition-colors"> |
| 51 | + {postData.title} |
| 52 | + </h2> |
| 53 | + {postData.description && ( |
| 54 | + <p className="text-fd-foreground/70"> |
| 55 | + {postData.description} |
| 56 | + </p> |
| 57 | + )} |
| 58 | + </div> |
| 59 | + |
| 60 | + <div className="flex items-center gap-4 text-sm text-fd-foreground/70"> |
| 61 | + {postData.date && ( |
| 62 | + <time dateTime={postData.date}> |
| 63 | + {new Date(postData.date).toLocaleDateString('en-US', { |
| 64 | + year: 'numeric', |
| 65 | + month: 'long', |
| 66 | + day: 'numeric', |
| 67 | + })} |
| 68 | + </time> |
| 69 | + )} |
| 70 | + {postData.author && ( |
| 71 | + <span>By {postData.author}</span> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + |
| 75 | + {postData.tags && postData.tags.length > 0 && ( |
| 76 | + <div className="mt-4 flex flex-wrap gap-2"> |
| 77 | + {postData.tags.map((tag: string) => ( |
| 78 | + <span |
| 79 | + key={tag} |
| 80 | + className="inline-flex items-center rounded-full bg-fd-primary/10 px-2.5 py-0.5 text-xs font-medium text-fd-primary" |
| 81 | + > |
| 82 | + {tag} |
| 83 | + </span> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + )} |
| 87 | + </Link> |
| 88 | + ); |
| 89 | + })} |
| 90 | + </div> |
| 91 | + |
| 92 | + {posts.length === 0 && ( |
| 93 | + <div className="text-center py-12"> |
| 94 | + <p className="text-fd-foreground/70">No blog posts yet. Check back soon!</p> |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </main> |
| 98 | + </HomeLayout> |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // Show individual blog post |
| 103 | + const page = blog.getPage(slug); |
| 104 | + |
| 105 | + if (!page) { |
| 106 | + notFound(); |
| 107 | + } |
| 108 | + |
| 109 | + const pageData = page.data as unknown as BlogPostData; |
| 110 | + const MDX = page.data.body; |
| 111 | + |
| 112 | + return ( |
| 113 | + <HomeLayout {...baseOptions}> |
| 114 | + <main className="container max-w-4xl mx-auto px-4 py-16"> |
| 115 | + <Link |
| 116 | + href="/blog" |
| 117 | + className="inline-flex items-center gap-2 text-sm text-fd-foreground/70 hover:text-fd-foreground mb-8 transition-colors" |
| 118 | + > |
| 119 | + <ArrowLeft className="w-4 h-4" /> |
| 120 | + Back to Blog |
| 121 | + </Link> |
| 122 | + |
| 123 | + <article className="prose prose-neutral dark:prose-invert max-w-none"> |
| 124 | + <header className="mb-8 pb-8 border-b border-fd-border"> |
| 125 | + <h1 className="text-4xl font-bold mb-4">{pageData.title}</h1> |
| 126 | + |
| 127 | + {pageData.description && ( |
| 128 | + <p className="text-xl text-fd-foreground/80 mb-6"> |
| 129 | + {pageData.description} |
| 130 | + </p> |
| 131 | + )} |
| 132 | + |
| 133 | + <div className="flex items-center gap-4 text-sm text-fd-foreground/70"> |
| 134 | + {pageData.date && ( |
| 135 | + <time dateTime={pageData.date}> |
| 136 | + {new Date(pageData.date).toLocaleDateString('en-US', { |
| 137 | + year: 'numeric', |
| 138 | + month: 'long', |
| 139 | + day: 'numeric', |
| 140 | + })} |
| 141 | + </time> |
| 142 | + )} |
| 143 | + {pageData.author && ( |
| 144 | + <span>By {pageData.author}</span> |
| 145 | + )} |
| 146 | + </div> |
| 147 | + |
| 148 | + {pageData.tags && pageData.tags.length > 0 && ( |
| 149 | + <div className="mt-4 flex flex-wrap gap-2"> |
| 150 | + {pageData.tags.map((tag: string) => ( |
| 151 | + <span |
| 152 | + key={tag} |
| 153 | + className="inline-flex items-center rounded-full bg-fd-primary/10 px-3 py-1 text-sm font-medium text-fd-primary" |
| 154 | + > |
| 155 | + {tag} |
| 156 | + </span> |
| 157 | + ))} |
| 158 | + </div> |
| 159 | + )} |
| 160 | + </header> |
| 161 | + |
| 162 | + <MDX components={defaultMdxComponents} /> |
| 163 | + </article> |
| 164 | + </main> |
| 165 | + </HomeLayout> |
| 166 | + ); |
| 167 | +} |
| 168 | + |
| 169 | +export async function generateStaticParams() { |
| 170 | + return blog.getPages().map((page) => ({ |
| 171 | + slug: page.slugs, |
| 172 | + })); |
| 173 | +} |
| 174 | + |
| 175 | +export async function generateMetadata({ |
| 176 | + params, |
| 177 | +}: { |
| 178 | + params: Promise<{ slug?: string[] }>; |
| 179 | +}) { |
| 180 | + const { slug } = await params; |
| 181 | + |
| 182 | + // If no slug, return default metadata for blog index |
| 183 | + if (!slug || slug.length === 0) { |
| 184 | + return { |
| 185 | + title: 'Blog', |
| 186 | + description: 'Insights, updates, and best practices from the ObjectStack team.', |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + const page = blog.getPage(slug); |
| 191 | + |
| 192 | + if (!page) { |
| 193 | + notFound(); |
| 194 | + } |
| 195 | + |
| 196 | + return { |
| 197 | + title: page.data.title, |
| 198 | + description: page.data.description, |
| 199 | + }; |
| 200 | +} |
0 commit comments