|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { Post } from "@site/src/data/posts"; |
| 3 | +import { FaBookmark } from "react-icons/fa"; |
| 4 | +import { HiOutlineAdjustments } from "react-icons/hi"; |
| 5 | +import { Tooltip } from "react-tooltip"; |
| 6 | + |
| 7 | +interface SidebarProps { |
| 8 | + posts: Post[]; |
| 9 | + selectedTags: string[]; |
| 10 | + setSelectedTags: React.Dispatch<React.SetStateAction<string[]>>; |
| 11 | + sortBy: "Latest" | "Popular" | "Oldest"; |
| 12 | + setSortBy: React.Dispatch<React.SetStateAction<"Latest" | "Popular" | "Oldest">>; |
| 13 | + clearFilters: () => void; |
| 14 | + showSavedOnly: boolean; |
| 15 | + setShowSavedOnly: React.Dispatch<React.SetStateAction<boolean>>; |
| 16 | + savedIds: number[]; |
| 17 | + onHideSidebar: () => void; |
| 18 | + tagCounts: { [tag: string]: number }; |
| 19 | +} |
| 20 | + |
| 21 | +const MAX_VISIBLE_TAGS = 5; |
| 22 | + |
| 23 | +const NewsletterSidebar: React.FC<SidebarProps> = ({ |
| 24 | + posts, |
| 25 | + selectedTags, |
| 26 | + setSelectedTags, |
| 27 | + sortBy, |
| 28 | + setSortBy, |
| 29 | + clearFilters, |
| 30 | + showSavedOnly, |
| 31 | + setShowSavedOnly, |
| 32 | + savedIds, |
| 33 | + onHideSidebar, |
| 34 | +}) => { |
| 35 | + const [collapsed, setCollapsed] = useState(false); |
| 36 | + |
| 37 | + const allTags = Array.from(new Set(posts.flatMap((p) => p.tags))); |
| 38 | + const visibleTags = allTags.slice(0, MAX_VISIBLE_TAGS); |
| 39 | + const hiddenTags = allTags.slice(MAX_VISIBLE_TAGS); |
| 40 | + |
| 41 | + const toggleTag = (tag: string) => |
| 42 | + setSelectedTags((prev) => |
| 43 | + prev.includes(tag) ? prev.filter((t) => t !== tag) : [...prev, tag] |
| 44 | + ); |
| 45 | + |
| 46 | + const tagCounts = posts.reduce((acc: { [tag: string]: number }, post) => { |
| 47 | + post.tags.forEach((tag) => { |
| 48 | + acc[tag] = (acc[tag] || 0) + 1; |
| 49 | + }); |
| 50 | + return acc; |
| 51 | + }, {}); |
| 52 | + |
| 53 | + return ( |
| 54 | + <aside className="w-full md:w-64 p-4 sticky top-0 bg-white border-r border-gray-200 min-h-screen transition-all duration-300"> |
| 55 | + {/* Collapse Toggle */} |
| 56 | + <button |
| 57 | + onClick={() => setCollapsed((c) => !c)} |
| 58 | + className="w-full flex justify-between items-center px-4 py-2 text-sm font-medium text-white bg-gradient-to-r from-purple-600 to-indigo-600 rounded-md shadow hover:opacity-90 transition mb-4" |
| 59 | + > |
| 60 | + <span>{collapsed ? "Show Filters" : "Hide Filters"}</span> |
| 61 | + <svg |
| 62 | + className={`w-4 h-4 transform transition-transform duration-200 ${ |
| 63 | + collapsed ? "rotate-0" : "rotate-180" |
| 64 | + }`} |
| 65 | + fill="none" |
| 66 | + stroke="currentColor" |
| 67 | + strokeWidth={2} |
| 68 | + viewBox="0 0 24 24" |
| 69 | + > |
| 70 | + <path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" /> |
| 71 | + </svg> |
| 72 | + </button> |
| 73 | + |
| 74 | + {!collapsed && ( |
| 75 | + <div className="space-y-5"> |
| 76 | + {/* Sort by */} |
| 77 | + <div className="flex items-center gap-1 bg-gray-100 p-1 rounded-full shadow-inner border border-gray-200"> |
| 78 | + {["Latest", "Popular", "Oldest"].map((option) => ( |
| 79 | + <button |
| 80 | + key={option} |
| 81 | + onClick={() => setSortBy(option as "Latest" | "Popular" | "Oldest")} |
| 82 | + className={`px-3 py-2 rounded-full text-sm font-medium transition-all duration-300 ${ |
| 83 | + sortBy === option ? "bg-black text-white shadow" : "text-gray-600 hover:bg-white" |
| 84 | + }`} |
| 85 | + > |
| 86 | + {option} |
| 87 | + </button> |
| 88 | + ))} |
| 89 | + </div> |
| 90 | + |
| 91 | + {/* Tags */} |
| 92 | + <div> |
| 93 | + <label className="font-semibold mb-1 block text-gray-800">Tags</label> |
| 94 | + <div className="flex flex-wrap gap-2"> |
| 95 | + {visibleTags.map((tag) => ( |
| 96 | + <button |
| 97 | + key={tag} |
| 98 | + className={`flex items-center justify-between px-3 py-1.5 rounded-md text-sm transition ${ |
| 99 | + selectedTags.includes(tag) ? "bg-black text-white" : "hover:bg-gray-100" |
| 100 | + }`} |
| 101 | + onClick={() => toggleTag(tag)} |
| 102 | + > |
| 103 | + <span>{tag}</span> |
| 104 | + <span className="text-xs text-gray-500 ml-2">({tagCounts[tag]})</span> |
| 105 | + </button> |
| 106 | + ))} |
| 107 | + |
| 108 | + {hiddenTags.length > 0 && ( |
| 109 | + <> |
| 110 | + <div className="relative group"> |
| 111 | + <button |
| 112 | + className="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 rounded-md text-sm text-gray-700" |
| 113 | + data-tooltip-id="tag-tooltip" |
| 114 | + data-tooltip-html={hiddenTags |
| 115 | + .map( |
| 116 | + (tag) => |
| 117 | + `<div style="cursor:pointer; margin:4px 0;" onclick="document.getElementById('tag-${tag}').click()"> |
| 118 | + ${tag} (${tagCounts[tag]}) |
| 119 | + </div>` |
| 120 | + ) |
| 121 | + .join("")} |
| 122 | + > |
| 123 | + +{hiddenTags.length} more |
| 124 | + </button> |
| 125 | + <Tooltip |
| 126 | + id="tag-tooltip" |
| 127 | + place="top" |
| 128 | + className="whitespace-pre-line z-50 max-w-xs text-sm" |
| 129 | + clickable |
| 130 | + /> |
| 131 | + </div> |
| 132 | + |
| 133 | + {/* Hidden buttons (to trigger via onclick in tooltip) */} |
| 134 | + {hiddenTags.map((tag) => ( |
| 135 | + <button |
| 136 | + key={tag} |
| 137 | + id={`tag-${tag}`} |
| 138 | + className="hidden" |
| 139 | + onClick={() => toggleTag(tag)} |
| 140 | + /> |
| 141 | + ))} |
| 142 | + </> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + |
| 147 | + {/* Show Saved Only Toggle */} |
| 148 | + <div className="pt-2"> |
| 149 | + <button |
| 150 | + onClick={() => setShowSavedOnly(!showSavedOnly)} |
| 151 | + className={`flex items-center gap-2 px-4 py-2 text-sm font-medium rounded-full border shadow-sm transition-all w-full justify-center ${ |
| 152 | + showSavedOnly |
| 153 | + ? "bg-green-100 text-green-800 border-green-300" |
| 154 | + : "bg-white text-gray-700 border-gray-300 hover:bg-gray-100" |
| 155 | + }`} |
| 156 | + > |
| 157 | + <FaBookmark className={showSavedOnly ? "text-green-600" : "text-gray-500"} /> |
| 158 | + {showSavedOnly ? "Showing Saved" : "Show Saved Only"} |
| 159 | + </button> |
| 160 | + </div> |
| 161 | + |
| 162 | + {/* Hide Sidebar */} |
| 163 | + <div className="pt-2"> |
| 164 | + <button |
| 165 | + onClick={onHideSidebar} |
| 166 | + className="w-full flex items-center justify-center gap-2 text-sm font-medium px-4 py-2 bg-pink-100 text-pink-700 border border-pink-300 rounded-full shadow-sm hover:bg-pink-200 transition-all" |
| 167 | + > |
| 168 | + <HiOutlineAdjustments className="text-pink-500" /> |
| 169 | + Hide Sidebar (Full Screen) |
| 170 | + </button> |
| 171 | + </div> |
| 172 | + |
| 173 | + {/* Clear Filters */} |
| 174 | + {(selectedTags.length > 0 || sortBy !== "Latest" || showSavedOnly) && ( |
| 175 | + <button |
| 176 | + onClick={clearFilters} |
| 177 | + className="text-red-500 text-sm hover:underline hover:text-red-700 transition" |
| 178 | + > |
| 179 | + Clear Filters |
| 180 | + </button> |
| 181 | + )} |
| 182 | + </div> |
| 183 | + )} |
| 184 | + </aside> |
| 185 | + ); |
| 186 | +}; |
| 187 | + |
| 188 | +export default NewsletterSidebar; |
0 commit comments