|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import { Search } from "lucide-react"; |
| 5 | +import { Input } from "@/components/ui/input"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { |
| 8 | + Select, |
| 9 | + SelectContent, |
| 10 | + SelectItem, |
| 11 | + SelectTrigger, |
| 12 | + SelectValue, |
| 13 | +} from "@/components/ui/select"; |
| 14 | +import { ContentCard } from "@/components/ContentCard"; |
| 15 | +import { track } from "@vercel/analytics"; |
| 16 | +import debounce from "lodash.debounce"; |
| 17 | +import type { ArticleWithSlug } from "@/types"; |
| 18 | + |
| 19 | +interface BlogClientProps { |
| 20 | + articles: ArticleWithSlug[]; |
| 21 | + years: string[]; |
| 22 | +} |
| 23 | + |
| 24 | +export default function BlogClient({ articles, years }: BlogClientProps) { |
| 25 | + const [searchTerm, setSearchTerm] = useState(""); |
| 26 | + const [selectedYear, setSelectedYear] = useState(""); |
| 27 | + const [filteredArticles, setFilteredArticles] = useState<ArticleWithSlug[]>(articles); |
| 28 | + |
| 29 | + // Debounced tracking function for search analytics |
| 30 | + const debouncedTrack = debounce((query: string) => { |
| 31 | + if (query.trim()) { |
| 32 | + track('blog-search', { term: query }); |
| 33 | + } |
| 34 | + }, 1200); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + let filtered = [...articles]; |
| 38 | + |
| 39 | + // Sort articles by date in chronological order (newest first) |
| 40 | + filtered.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); |
| 41 | + |
| 42 | + if (searchTerm) { |
| 43 | + const term = searchTerm.toLowerCase(); |
| 44 | + filtered = filtered.filter( |
| 45 | + (article) => |
| 46 | + article.title.toLowerCase().includes(term) || |
| 47 | + article.description.toLowerCase().includes(term) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + if (selectedYear && selectedYear !== "All Years") { |
| 52 | + filtered = filtered.filter((article) => article.date.startsWith(selectedYear)); |
| 53 | + } |
| 54 | + |
| 55 | + setFilteredArticles(filtered); |
| 56 | + }, [searchTerm, selectedYear, articles]); |
| 57 | + |
| 58 | + const handleSearchChange = (value: string) => { |
| 59 | + setSearchTerm(value); |
| 60 | + debouncedTrack(value); |
| 61 | + }; |
| 62 | + |
| 63 | + const resetFilters = () => { |
| 64 | + setSearchTerm(""); |
| 65 | + setSelectedYear(""); |
| 66 | + }; |
| 67 | + |
| 68 | + return ( |
| 69 | + <div className="w-full"> |
| 70 | + <div className="container mx-auto px-4 py-16"> |
| 71 | + <div className="text-center mb-12"> |
| 72 | + <h1 className="text-5xl font-bold text-slate-900 dark:text-white mb-4"> |
| 73 | + I write to learn, and publish to share |
| 74 | + </h1> |
| 75 | + <p className="text-xl text-slate-600 dark:text-slate-400 max-w-3xl mx-auto"> |
| 76 | + All of my technical tutorials, musings and developer rants |
| 77 | + </p> |
| 78 | + </div> |
| 79 | + |
| 80 | + <div className="bg-white dark:bg-slate-900 rounded-xl shadow-sm border border-slate-200 dark:border-slate-800 p-4 mb-8 max-w-4xl mx-auto"> |
| 81 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 82 | + <div className="relative"> |
| 83 | + <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-slate-400" size={18} /> |
| 84 | + <Input |
| 85 | + placeholder="Search articles..." |
| 86 | + className="pl-10" |
| 87 | + value={searchTerm} |
| 88 | + onChange={(e) => handleSearchChange(e.target.value)} |
| 89 | + /> |
| 90 | + </div> |
| 91 | + |
| 92 | + <Select value={selectedYear} onValueChange={setSelectedYear}> |
| 93 | + <SelectTrigger className="bg-white dark:bg-slate-800 border-slate-200 dark:border-slate-700"> |
| 94 | + <SelectValue placeholder="Year" /> |
| 95 | + </SelectTrigger> |
| 96 | + <SelectContent className="bg-white dark:bg-slate-800 border-slate-200 dark:border-slate-700"> |
| 97 | + <SelectItem value="All Years">All Years</SelectItem> |
| 98 | + {years.map((year) => ( |
| 99 | + <SelectItem key={year} value={year}> |
| 100 | + {year} |
| 101 | + </SelectItem> |
| 102 | + ))} |
| 103 | + </SelectContent> |
| 104 | + </Select> |
| 105 | + </div> |
| 106 | + |
| 107 | + <div className="flex justify-between items-center mt-4"> |
| 108 | + <Button variant="outline" size="sm" onClick={resetFilters} className="text-slate-600 dark:text-slate-400"> |
| 109 | + Reset Filters |
| 110 | + </Button> |
| 111 | + <span className="text-sm text-slate-500 dark:text-slate-400"> |
| 112 | + {filteredArticles.length} articles found |
| 113 | + </span> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className="space-y-8"> |
| 118 | + <section> |
| 119 | + <h2 className="text-2xl font-semibold text-slate-900 dark:text-white mb-6 text-center">All Articles</h2> |
| 120 | + |
| 121 | + <div className="md:border-l md:border-zinc-100 md:pl-6 md:dark:border-zinc-700/40"> |
| 122 | + <div className="mx-auto grid max-w-2xl grid-cols-1 gap-x-8 gap-y-20 lg:mx-0 lg:max-w-none lg:grid-cols-3"> |
| 123 | + {filteredArticles.map((article) => ( |
| 124 | + <ContentCard key={article.slug} article={article} /> |
| 125 | + ))} |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </section> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ); |
| 133 | +} |
| 134 | + |
0 commit comments