Skip to content

Commit 970cf54

Browse files
committed
add RecentArticles component to display recent blog posts
1 parent 166a3b1 commit 970cf54

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

app/blog/[slug]/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { notFound } from "next/navigation";
22

33
import PostHero from "@/components/blocks/post-hero";
4+
import RecentArticles from "@/components/blocks/recent-articles";
45

56
import PortableTextRenderer from "@/components/portable-text-renderer";
67
import {
@@ -47,6 +48,7 @@ export default async function PostPage(props: {
4748
<PostHero {...post} />
4849
{post.body && <PortableTextRenderer value={post.body} />}
4950
</article>
51+
<RecentArticles currentSlug={params.slug} />
5052
</div>
5153
</section>
5254
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Link from "next/link";
2+
import PostCard from "@/components/ui/post-card";
3+
import { fetchSanityPosts } from "@/sanity/lib/fetch";
4+
5+
export default async function RecentArticles({
6+
currentSlug,
7+
title = "Recent articles",
8+
}: {
9+
currentSlug: string;
10+
title?: string;
11+
}) {
12+
const posts = await fetchSanityPosts();
13+
const recent = posts
14+
.filter((p) => p?.slug?.current && p.slug.current !== currentSlug)
15+
.slice(0, 3);
16+
17+
if (!recent.length) return null;
18+
19+
return (
20+
<div className="mt-16 border-t pt-10">
21+
<h2 className="text-2xl font-semibold mb-6">{title}</h2>
22+
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
23+
{recent.map((post) => (
24+
<Link
25+
key={post?.slug?.current}
26+
className="flex w-full rounded-3xl ring-offset-background focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
27+
href={`/blog/${post?.slug?.current}`}
28+
>
29+
<PostCard
30+
title={post?.title ?? ""}
31+
excerpt={post?.excerpt ?? ""}
32+
image={post?.image ?? null}
33+
/>
34+
</Link>
35+
))}
36+
</div>
37+
</div>
38+
);
39+
}

0 commit comments

Comments
 (0)