Skip to content

Commit b882457

Browse files
feat: filter posts by query tags
1 parent 128b53d commit b882457

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

app/share/page.tsx

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ArticleBlock from "@components/ArticleBlock";
22
import { posts, Post } from "@content";
33
import { Metadata } from "next";
4+
import TagFilteredList from "@/components/TagFilteredList";
45

56
export const metadata: Metadata = {
67
title: "Share",
@@ -19,15 +20,13 @@ export default function Share() {
1920
return (
2021
<div className="share-page">
2122
<h1>Share</h1>
22-
<ul>
23-
<hr />
24-
{displayPosts.map((post: Post) => (
25-
<li key={post.slug}>
26-
<ArticleBlock article={post}></ArticleBlock>
27-
<hr />
28-
</li>
29-
))}
30-
</ul>
23+
<TagFilteredList>
24+
{displayPosts.map((post: Post) => ({
25+
content: <ArticleBlock article={post} />,
26+
tags: post.tags,
27+
slug: post.slug,
28+
}))}
29+
</TagFilteredList>
3130
</div>
3231
);
3332
}

components/TagFilteredList.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use client";
2+
3+
import { Post } from "@content";
4+
import { useSearchParams } from "next/navigation";
5+
import ArticleBlock from "@components/ArticleBlock";
6+
7+
interface TaggedItem {
8+
content: React.ReactNode;
9+
tags: string[];
10+
slug: string;
11+
}
12+
13+
interface TagFilteredListProps {
14+
children: TaggedItem[];
15+
}
16+
17+
/**
18+
* Displays a list of items, filtering them based on the tags in the URL query.
19+
* @param children The items to display, each with a `content`, `tags`, and `slug` property.
20+
*/
21+
export default function TagFilteredList({ children }: TagFilteredListProps) {
22+
const selectedTags = useSearchParams().get("tags")?.split(",") || [];
23+
24+
let filteredItems = children;
25+
if (selectedTags.length > 0) {
26+
filteredItems = children.filter((child: TaggedItem) =>
27+
child.tags.some((tag) => selectedTags.includes(tag))
28+
);
29+
}
30+
31+
return (
32+
<ul>
33+
<hr />
34+
{filteredItems.map((item: TaggedItem) => (
35+
<li key={item.slug}>
36+
{item.content}
37+
<hr />
38+
</li>
39+
))}
40+
</ul>
41+
);
42+
}

0 commit comments

Comments
 (0)