|
1 |
| -import { BlogCard } from "@/components/card-component"; |
2 |
| -import { Article } from "@/types/article"; |
| 1 | +"use client"; |
| 2 | +import React, { useState, useMemo } from "react"; |
| 3 | +import { UnifiedCard } from "@/components/card-component"; |
| 4 | +import { |
| 5 | + UnifiedContent, |
| 6 | + isGame, |
| 7 | + isArticle, |
| 8 | + sortLevel, |
| 9 | +} from "@/types/unifiedContent"; |
| 10 | + |
| 11 | +export function BlogComponent({ |
| 12 | + contents, |
| 13 | + showFilters = false, |
| 14 | +}: { |
| 15 | + contents: UnifiedContent[]; |
| 16 | + showFilters?: boolean; |
| 17 | +}) { |
| 18 | + const [searchTerm, setSearchTerm] = useState(""); |
| 19 | + const [contentType, setContentType] = useState("all"); |
| 20 | + const [sortBy, setSortBy] = useState("title"); |
| 21 | + |
| 22 | + const filteredAndSortedContents = useMemo(() => { |
| 23 | + return contents |
| 24 | + .filter((c) => { |
| 25 | + const matchesSearch = |
| 26 | + c.content.title.toLowerCase().includes(searchTerm.toLowerCase()) || |
| 27 | + c.content.synopsis.toLowerCase().includes(searchTerm.toLowerCase()); |
| 28 | + const matchesType = |
| 29 | + contentType === "all" || |
| 30 | + (contentType === "game" && isGame(c)) || |
| 31 | + (contentType === "article" && isArticle(c)); |
| 32 | + return matchesSearch && matchesType; |
| 33 | + }) |
| 34 | + .sort((a, b) => { |
| 35 | + if (sortBy === "title") { |
| 36 | + return a.content.title.localeCompare(b.content.title); |
| 37 | + } else if (sortBy === "level") { |
| 38 | + return sortLevel(a, b); |
| 39 | + } else if (sortBy === "level_desc") { |
| 40 | + return -1 * sortLevel(a, b); |
| 41 | + } |
| 42 | + return 0; |
| 43 | + }); |
| 44 | + }, [contents, searchTerm, contentType, sortBy]); |
3 | 45 |
|
4 |
| -export function BlogComponent(props: { blogs: Article[] }) { |
5 | 46 | return (
|
6 |
| - <div className="flex flex-wrap flex-row"> |
7 |
| - {props.blogs.map((article: Article) => ( |
8 |
| - <BlogCard key={article.title} article={article} /> |
9 |
| - ))} |
| 47 | + <div className="space-y-4"> |
| 48 | + {showFilters && ( |
| 49 | + <div className="flex flex-wrap gap-4 items-center"> |
| 50 | + <input |
| 51 | + type="text" |
| 52 | + placeholder="Search..." |
| 53 | + value={searchTerm} |
| 54 | + onChange={(e) => setSearchTerm(e.target.value)} |
| 55 | + className="w-full sm:w-auto p-2 border rounded" |
| 56 | + /> |
| 57 | + <select |
| 58 | + value={contentType} |
| 59 | + onChange={(e) => setContentType(e.target.value)} |
| 60 | + className="w-full sm:w-auto p-2 border rounded" |
| 61 | + > |
| 62 | + <option value="all">All Types</option> |
| 63 | + <option value="game">Games</option> |
| 64 | + <option value="article">Articles</option> |
| 65 | + </select> |
| 66 | + <select |
| 67 | + value={sortBy} |
| 68 | + onChange={(e) => setSortBy(e.target.value)} |
| 69 | + className="w-full sm:w-auto p-2 border rounded" |
| 70 | + > |
| 71 | + <option value="title">Sort by Title</option> |
| 72 | + <option value="level">Sort by Level</option> |
| 73 | + <option value="level_desc">Sort by Level (DESC)</option> |
| 74 | + </select> |
| 75 | + </div> |
| 76 | + )} |
| 77 | + <div className="grid gap-2 sm:grid-cols-1 md:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4"> |
| 78 | + {filteredAndSortedContents.map((c: UnifiedContent) => ( |
| 79 | + <UnifiedCard key={c.content.title} content={c} /> |
| 80 | + ))} |
| 81 | + </div> |
10 | 82 | </div>
|
11 | 83 | );
|
12 | 84 | }
|
0 commit comments