Skip to content

Commit

Permalink
feat: filter posts by query tags
Browse files Browse the repository at this point in the history
  • Loading branch information
SidonieBouthors committed Aug 17, 2024
1 parent 128b53d commit b882457
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
17 changes: 8 additions & 9 deletions app/share/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ArticleBlock from "@components/ArticleBlock";
import { posts, Post } from "@content";
import { Metadata } from "next";
import TagFilteredList from "@/components/TagFilteredList";

export const metadata: Metadata = {
title: "Share",
Expand All @@ -19,15 +20,13 @@ export default function Share() {
return (
<div className="share-page">
<h1>Share</h1>
<ul>
<hr />
{displayPosts.map((post: Post) => (
<li key={post.slug}>
<ArticleBlock article={post}></ArticleBlock>
<hr />
</li>
))}
</ul>
<TagFilteredList>
{displayPosts.map((post: Post) => ({
content: <ArticleBlock article={post} />,
tags: post.tags,
slug: post.slug,
}))}
</TagFilteredList>
</div>
);
}
42 changes: 42 additions & 0 deletions components/TagFilteredList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use client";

import { Post } from "@content";
import { useSearchParams } from "next/navigation";
import ArticleBlock from "@components/ArticleBlock";

interface TaggedItem {
content: React.ReactNode;
tags: string[];
slug: string;
}

interface TagFilteredListProps {
children: TaggedItem[];
}

/**
* Displays a list of items, filtering them based on the tags in the URL query.
* @param children The items to display, each with a `content`, `tags`, and `slug` property.
*/
export default function TagFilteredList({ children }: TagFilteredListProps) {
const selectedTags = useSearchParams().get("tags")?.split(",") || [];

let filteredItems = children;
if (selectedTags.length > 0) {
filteredItems = children.filter((child: TaggedItem) =>
child.tags.some((tag) => selectedTags.includes(tag))
);
}

return (
<ul>
<hr />
{filteredItems.map((item: TaggedItem) => (
<li key={item.slug}>
{item.content}
<hr />
</li>
))}
</ul>
);
}

0 comments on commit b882457

Please sign in to comment.