|
| 1 | +"use client" |
| 2 | + |
| 3 | +import Link from "next/link" |
| 4 | +import { useState } from "react" |
| 5 | +import { BookOpen, Code, RotateCcw, FileText, File } from "lucide-react" |
| 6 | + |
| 7 | +type BlogPost = { |
| 8 | + path: string |
| 9 | + data: { |
| 10 | + title: string |
| 11 | + description?: string |
| 12 | + category?: "devlog" | "updates" | "other" |
| 13 | + date?: string | Date |
| 14 | + url: string |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type BlogSidebarProps = { |
| 19 | + posts: BlogPost[] |
| 20 | +} |
| 21 | + |
| 22 | +export function BlogSidebar({ posts }: BlogSidebarProps) { |
| 23 | + const categories = [ |
| 24 | + "All Posts", |
| 25 | + ...[...new Set(posts.map((post) => post.data.category))].filter( |
| 26 | + (category): category is "devlog" | "updates" | "other" => Boolean(category), |
| 27 | + ), |
| 28 | + ] |
| 29 | + |
| 30 | + const [selectedCategory, setSelectedCategory] = useState("All Posts") |
| 31 | + |
| 32 | + const getCategoryIcon = (category: string) => { |
| 33 | + switch (category) { |
| 34 | + case "All Posts": |
| 35 | + return <BookOpen className="w-4 h-4" /> |
| 36 | + case "devlog": |
| 37 | + return <Code className="w-4 h-4" /> |
| 38 | + case "updates": |
| 39 | + return <RotateCcw className="w-4 h-4" /> |
| 40 | + case "other": |
| 41 | + return <FileText className="w-4 h-4" /> |
| 42 | + default: |
| 43 | + return <File className="w-4 h-4" /> |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const filteredPosts = |
| 48 | + selectedCategory === "All Posts" |
| 49 | + ? posts |
| 50 | + : posts.filter((post) => post.data.category === selectedCategory) |
| 51 | + |
| 52 | + return ( |
| 53 | + <main className="container mx-auto my-12 space-y-10"> |
| 54 | + <div className="mx-auto flex w-full gap-16 px-8"> |
| 55 | + <aside className="w-48 flex-shrink-0"> |
| 56 | + <h1 className="text-5xl font-semibold tracking-tight">Blog</h1> |
| 57 | + <nav className="mt-12 flex flex-col space-y-4 text-sm uppercase text-neutral-500"> |
| 58 | + {categories.map((category) => ( |
| 59 | + <button |
| 60 | + key={category} |
| 61 | + onClick={() => setSelectedCategory(category)} |
| 62 | + className={`flex items-center gap-2 text-left transition-colors hover:text-white uppercase text-md ${ |
| 63 | + selectedCategory === category ? "text-white" : "" |
| 64 | + }`} |
| 65 | + > |
| 66 | + {getCategoryIcon(category)} |
| 67 | + {category} |
| 68 | + </button> |
| 69 | + ))} |
| 70 | + </nav> |
| 71 | + </aside> |
| 72 | + <div className="flex flex-1 flex-col space-y-6 mt-22"> |
| 73 | + {filteredPosts.map((post) => ( |
| 74 | + <Link href={post.data.url} key={post.path} className="border border-white/20 p-4"> |
| 75 | + <article className="group"> |
| 76 | + <div className="flex flex-col space-y-3 p-6 transition-colors group-hover:border-white/20"> |
| 77 | + <h2 className="text-2xl font-semibold text-white group-hover:text-white/90"> |
| 78 | + {post.data.title} |
| 79 | + </h2> |
| 80 | + <p className="text-sm text-neutral-400">{post.data.description}</p> |
| 81 | + <p className="text-xs uppercase tracking-wide text-neutral-500 flex items-center gap-2"> |
| 82 | + {getCategoryIcon(post.data.category ?? "All Posts")} {post.data.category} ·{" "} |
| 83 | + {new Date( |
| 84 | + (typeof post.data.date === "string" |
| 85 | + ? post.data.date |
| 86 | + : post.data.date?.toISOString()) ?? |
| 87 | + post.path.split("/").pop()?.replace(".mdx", "") ?? |
| 88 | + "", |
| 89 | + ).toLocaleDateString("en-US", { |
| 90 | + month: "short", |
| 91 | + day: "numeric", |
| 92 | + year: "numeric", |
| 93 | + })} |
| 94 | + </p> |
| 95 | + </div> |
| 96 | + </article> |
| 97 | + </Link> |
| 98 | + ))} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + </main> |
| 102 | + ) |
| 103 | +} |
0 commit comments