|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import PageTransition from "@/components/landing/components/PageTransition"; |
| 4 | +import Navbar from "@/components/shared/navbar"; |
| 5 | +import { PublicProjectCard } from "@/components/shared/public-project-card"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { Card } from "@/components/ui/card"; |
| 8 | +import { Input } from "@/components/ui/input"; |
| 9 | +import { |
| 10 | + Select, |
| 11 | + SelectContent, |
| 12 | + SelectItem, |
| 13 | + SelectTrigger, |
| 14 | + SelectValue, |
| 15 | +} from "@/components/ui/select"; |
| 16 | +import { useDebounce } from "@/hooks/use-debounce"; |
| 17 | +import { useProjects } from "@/store/useProjectStore"; |
| 18 | +import type { Project } from "@/types/project"; |
| 19 | +import { Search, X } from "lucide-react"; |
| 20 | +import { useEffect, useState } from "react"; |
| 21 | + |
| 22 | +export default function ExplorePage() { |
| 23 | + const { projects, isLoading, error, fetchProjects } = useProjects(); |
| 24 | + const [filteredProjects, setFilteredProjects] = useState<Project[]>([]); |
| 25 | + const [searchTerm, setSearchTerm] = useState(""); |
| 26 | + const [categoryFilter, setCategoryFilter] = useState<string>("all"); |
| 27 | + const [statusFilter, setStatusFilter] = useState<string>("all"); |
| 28 | + const debouncedSearchTerm = useDebounce(searchTerm, 500); |
| 29 | + |
| 30 | + // Fetch projects on mount |
| 31 | + useEffect(() => { |
| 32 | + fetchProjects(false); |
| 33 | + }, [fetchProjects]); |
| 34 | + |
| 35 | + // Apply filters whenever projects, search term or filters change |
| 36 | + useEffect(() => { |
| 37 | + if (!projects.length) return; |
| 38 | + |
| 39 | + let results = [...projects]; |
| 40 | + |
| 41 | + // Apply search filter |
| 42 | + if (debouncedSearchTerm) { |
| 43 | + results = results.filter( |
| 44 | + (project) => |
| 45 | + project.title |
| 46 | + .toLowerCase() |
| 47 | + .includes(debouncedSearchTerm.toLowerCase()) || |
| 48 | + project.description |
| 49 | + .toLowerCase() |
| 50 | + .includes(debouncedSearchTerm.toLowerCase()), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + // Apply category filter |
| 55 | + if (categoryFilter !== "all") { |
| 56 | + results = results.filter( |
| 57 | + (project) => |
| 58 | + project.category.toLowerCase() === categoryFilter.toLowerCase(), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + // Apply status filter |
| 63 | + if (statusFilter !== "all") { |
| 64 | + results = results.filter( |
| 65 | + (project) => project.ideaValidation === statusFilter.toUpperCase(), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + setFilteredProjects(results); |
| 70 | + }, [projects, debouncedSearchTerm, categoryFilter, statusFilter]); |
| 71 | + |
| 72 | + // Get unique categories for filter dropdown |
| 73 | + const categories = Array.from( |
| 74 | + new Set(projects.map((project) => project.category)), |
| 75 | + ).sort(); |
| 76 | + |
| 77 | + return ( |
| 78 | + <PageTransition> |
| 79 | + <Navbar /> |
| 80 | + <div className="container max-w-7xl mx-auto px-4 md:px-6 py-12"> |
| 81 | + <div className="mb-12"> |
| 82 | + <h1 className="text-3xl md:text-4xl font-bold tracking-tighter mb-2"> |
| 83 | + Explore Projects |
| 84 | + </h1> |
| 85 | + <p className="text-muted-foreground"> |
| 86 | + Discover and support innovative blockchain projects |
| 87 | + </p> |
| 88 | + </div> |
| 89 | + |
| 90 | + {isLoading ? ( |
| 91 | + <div className="text-center py-10">Loading projects...</div> |
| 92 | + ) : error ? ( |
| 93 | + <div className="text-center py-10 text-destructive"> |
| 94 | + Error: {error} |
| 95 | + </div> |
| 96 | + ) : !projects.length ? ( |
| 97 | + <div className="text-center py-10">No projects found</div> |
| 98 | + ) : ( |
| 99 | + <> |
| 100 | + {/* Search and Filters */} |
| 101 | + <div className="mb-8"> |
| 102 | + <Card className="p-4"> |
| 103 | + <div className="grid grid-cols-1 md:grid-cols-4 gap-4"> |
| 104 | + {/* Search Input */} |
| 105 | + <div className="relative"> |
| 106 | + <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" /> |
| 107 | + <Input |
| 108 | + placeholder="Search projects..." |
| 109 | + className="pl-9" |
| 110 | + value={searchTerm} |
| 111 | + onChange={(e) => setSearchTerm(e.target.value)} |
| 112 | + /> |
| 113 | + {searchTerm && ( |
| 114 | + <Button |
| 115 | + onClick={() => setSearchTerm("")} |
| 116 | + className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground" |
| 117 | + > |
| 118 | + <X className="h-4 w-4" /> |
| 119 | + </Button> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + |
| 123 | + {/* Category Filter */} |
| 124 | + <Select |
| 125 | + value={categoryFilter} |
| 126 | + onValueChange={setCategoryFilter} |
| 127 | + > |
| 128 | + <SelectTrigger> |
| 129 | + <SelectValue placeholder="All Categories" /> |
| 130 | + </SelectTrigger> |
| 131 | + <SelectContent> |
| 132 | + <SelectItem value="all">All Categories</SelectItem> |
| 133 | + {categories.map((category) => ( |
| 134 | + <SelectItem key={category} value={category}> |
| 135 | + {category} |
| 136 | + </SelectItem> |
| 137 | + ))} |
| 138 | + </SelectContent> |
| 139 | + </Select> |
| 140 | + |
| 141 | + {/* Status Filter */} |
| 142 | + <Select value={statusFilter} onValueChange={setStatusFilter}> |
| 143 | + <SelectTrigger> |
| 144 | + <SelectValue placeholder="All Statuses" /> |
| 145 | + </SelectTrigger> |
| 146 | + <SelectContent> |
| 147 | + <SelectItem value="all">All Statuses</SelectItem> |
| 148 | + <SelectItem value="pending">Pending</SelectItem> |
| 149 | + <SelectItem value="validated">Validated</SelectItem> |
| 150 | + <SelectItem value="rejected">Rejected</SelectItem> |
| 151 | + </SelectContent> |
| 152 | + </Select> |
| 153 | + |
| 154 | + {/* Reset Filters */} |
| 155 | + <Button |
| 156 | + variant="outline" |
| 157 | + onClick={() => { |
| 158 | + setSearchTerm(""); |
| 159 | + setCategoryFilter("all"); |
| 160 | + setStatusFilter("all"); |
| 161 | + }} |
| 162 | + disabled={ |
| 163 | + searchTerm === "" && |
| 164 | + categoryFilter === "all" && |
| 165 | + statusFilter === "all" |
| 166 | + } |
| 167 | + > |
| 168 | + Reset Filters |
| 169 | + </Button> |
| 170 | + </div> |
| 171 | + </Card> |
| 172 | + </div> |
| 173 | + |
| 174 | + {/* Results Count */} |
| 175 | + <div className="mb-4 flex justify-between items-center"> |
| 176 | + <div className="text-sm text-muted-foreground"> |
| 177 | + Showing {filteredProjects.length} of {projects.length} projects |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + |
| 181 | + {/* Projects Grid */} |
| 182 | + {filteredProjects.length > 0 ? ( |
| 183 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6"> |
| 184 | + {filteredProjects.map((project) => ( |
| 185 | + <PublicProjectCard key={project.id} project={project} /> |
| 186 | + ))} |
| 187 | + </div> |
| 188 | + ) : ( |
| 189 | + <div className="text-center py-12"> |
| 190 | + <div className="text-muted-foreground mb-4"> |
| 191 | + No projects match your filters |
| 192 | + </div> |
| 193 | + <Button |
| 194 | + variant="outline" |
| 195 | + onClick={() => { |
| 196 | + setSearchTerm(""); |
| 197 | + setCategoryFilter("all"); |
| 198 | + setStatusFilter("all"); |
| 199 | + }} |
| 200 | + > |
| 201 | + Clear filters |
| 202 | + </Button> |
| 203 | + </div> |
| 204 | + )} |
| 205 | + </> |
| 206 | + )} |
| 207 | + </div> |
| 208 | + </PageTransition> |
| 209 | + ); |
| 210 | +} |
0 commit comments