Spaces:
Running
Running
File size: 10,431 Bytes
60e766b e22b981 60e766b d438808 c173c06 60e766b e22b981 60e766b 4e6ece3 e22b981 60e766b c173c06 af2571a e22b981 af2571a 60e766b e22b981 60e766b af2571a e22b981 af2571a 60e766b 4e6ece3 e22b981 4e6ece3 60e766b 4e6ece3 e22b981 60e766b e22b981 60e766b 4e6ece3 e22b981 4e6ece3 e22b981 4e6ece3 e22b981 60e766b 946dcc2 60e766b 946dcc2 4e6ece3 946dcc2 60e766b af2571a 60e766b c173c06 60e766b c173c06 60e766b c173c06 60e766b c173c06 60e766b e22b981 60e766b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | "use client";
import Link from "next/link";
import { useEffect, useMemo, useState } from "react";
const PAGE_SIZE = 20;
type IdeaCard = {
id: string;
title: string;
problemStatement: string;
tags: string[];
upvotesCount: number;
comment_count: number;
createdAt: string;
submitter_label?: string;
idea_working_count?: number;
working_developers?: string[];
};
function buildPageItems(currentPage: number, totalPages: number): Array<number | string> {
if (totalPages <= 7) {
return Array.from({ length: totalPages }, (_, index) => index + 1);
}
const items: Array<number | string> = [1];
const windowStart = Math.max(2, currentPage - 1);
const windowEnd = Math.min(totalPages - 1, currentPage + 1);
if (windowStart > 2) {
items.push("left-ellipsis");
}
for (let page = windowStart; page <= windowEnd; page += 1) {
items.push(page);
}
if (windowEnd < totalPages - 1) {
items.push("right-ellipsis");
}
items.push(totalPages);
return items;
}
export function HomeList() {
const [sort, setSort] = useState<"hot" | "new">("hot");
const [loading, setLoading] = useState(true);
const [items, setItems] = useState<IdeaCard[]>([]);
const [totalCount, setTotalCount] = useState<number | null>(null);
const [page, setPage] = useState(1);
const [totalPages, setTotalPages] = useState(0);
const [error, setError] = useState("");
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
const [queryInput, setQueryInput] = useState("");
const [query, setQuery] = useState("");
function changePage(nextPage: number) {
setPage(nextPage);
window.scrollTo({ top: 0, behavior: "smooth" });
}
useEffect(() => {
const timer = setTimeout(() => {
setQuery(queryInput.trim());
}, 250);
return () => clearTimeout(timer);
}, [queryInput]);
useEffect(() => {
setPage(1);
}, [sort, query]);
useEffect(() => {
let active = true;
setLoading(true);
setError("");
const params = new URLSearchParams({ sort });
params.set("page", String(page));
params.set("limit", String(PAGE_SIZE));
if (query) {
params.set("q", query);
}
fetch(`/api/ideas?${params.toString()}`)
.then(async (response) => {
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || "Failed to load ideas");
}
return data;
})
.then((data) => {
if (active) {
const nextItems = data.items || [];
setItems(nextItems);
setTotalCount(typeof data.totalCount === "number" ? data.totalCount : nextItems.length);
setTotalPages(typeof data.totalPages === "number" ? data.totalPages : 0);
if (typeof data.currentPage === "number") {
setPage(data.currentPage);
}
}
})
.catch((err) => {
if (active) {
setItems([]);
setTotalCount(0);
setTotalPages(0);
setError(err instanceof Error ? err.message : "Failed to load ideas");
}
})
.finally(() => {
if (active) setLoading(false);
});
return () => {
active = false;
};
}, [page, sort, query]);
const empty = useMemo(() => !loading && !error && items.length === 0, [loading, error, items.length]);
const visibleCount = items.length;
const startItem = visibleCount > 0 ? (page - 1) * PAGE_SIZE + 1 : 0;
const endItem = visibleCount > 0 ? startItem + visibleCount - 1 : 0;
const summaryLabel =
totalCount !== null && visibleCount > 0 && visibleCount < totalCount
? `Showing ${startItem}-${endItem} of ${totalCount} ideas`
: `${totalCount ?? visibleCount} ideas`;
const pageItems = buildPageItems(page, totalPages);
return (
<div className="space-y-4">
<div className="card p-4 md:p-5">
<label className="text-sm font-semibold uppercase tracking-[0.16em] text-zinc-600" htmlFor="idea-search-input">
Search Ideas
</label>
<p className="subtle mt-1 text-sm">Fuzzy search supports approximate terms and minor typos.</p>
<div className="mt-3 flex flex-col gap-2 md:flex-row md:items-center">
<input
id="idea-search-input"
type="search"
className="input w-full rounded-full px-5 py-3 text-base"
placeholder="Search by problem, keywords, or rough phrasing"
value={queryInput}
onChange={(event) => setQueryInput(event.target.value)}
aria-label="Search ideas"
/>
{queryInput ? (
<button type="button" className="btn rounded-full px-4" onClick={() => setQueryInput("")}>
Clear
</button>
) : null}
</div>
</div>
<div className="flex flex-wrap items-center justify-between gap-2">
<div className="flex items-center gap-2 rounded-full border border-white/70 bg-white/60 p-1">
<button onClick={() => setSort("hot")} className={`btn rounded-full px-4 ${sort === "hot" ? "btn-primary" : ""}`}>
Hot
</button>
<button onClick={() => setSort("new")} className={`btn rounded-full px-4 ${sort === "new" ? "btn-primary" : ""}`}>
New
</button>
</div>
<p className="subtle text-sm">
{summaryLabel}
{query ? ` matching "${query}"` : ""}
</p>
</div>
{loading ? <p className="subtle text-sm">Loading ideas...</p> : null}
{error ? <p className="text-sm text-red-700">{error}</p> : null}
{empty ? (
<p className="subtle text-sm">{query ? `No ideas found for "${query}".` : "No ideas yet. Be the first to submit one."}</p>
) : null}
<div className="grid gap-3">
{items.map((idea, index) => {
const workers = idea.working_developers || [];
const isExpanded = Boolean(expanded[idea.id]);
return (
<article key={idea.id} className="card reveal-up" style={{ animationDelay: `${Math.min(index * 45, 240)}ms` }}>
<div className="flex flex-wrap items-start justify-between gap-2">
<Link href={`/ideas/${idea.id}`} className="text-base font-semibold md:text-lg hover:underline">
{idea.title}
</Link>
<span className="subtle text-xs">{new Date(idea.createdAt).toLocaleString()}</span>
</div>
<p className="subtle mt-2 text-sm">{idea.problemStatement}</p>
<p className="subtle mt-2 text-xs">Submitter: {idea.submitter_label || "Anonymous"}</p>
{idea.tags.length ? (
<div className="mt-4 flex flex-wrap gap-1.5">
{idea.tags.slice(0, 6).map((tag) => (
<span key={tag} className="badge">
{tag}
</span>
))}
</div>
) : null}
<div className="mt-4 grid grid-cols-3 gap-2 md:max-w-md">
<div className="metric">
<p className="text-xs uppercase tracking-[0.16em] text-zinc-500">Upvotes</p>
<p className="mt-1 text-sm font-semibold">{idea.upvotesCount}</p>
</div>
<div className="metric">
<p className="text-xs uppercase tracking-[0.16em] text-zinc-500">Comments</p>
<p className="mt-1 text-sm font-semibold">{idea.comment_count}</p>
</div>
<button
type="button"
className="metric text-left"
onClick={() =>
setExpanded((prev) => ({
...prev,
[idea.id]: !prev[idea.id],
}))
}
>
<p className="text-xs uppercase tracking-[0.16em] text-zinc-500">Developers working</p>
<p className="mt-1 text-sm font-semibold">{idea.idea_working_count ?? workers.length}</p>
</button>
</div>
{isExpanded ? (
<div className="mt-3 rounded-lg border border-zinc-200 bg-white/85 p-3">
<p className="text-xs font-semibold uppercase tracking-[0.16em] text-zinc-500">Developer list</p>
{workers.length > 0 ? (
<ul className="mt-2 list-disc space-y-1 pl-5 text-sm">
{workers.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
) : (
<p className="subtle mt-2 text-sm">No developers have marked this topic yet.</p>
)}
</div>
) : null}
</article>
);
})}
</div>
{totalPages > 1 ? (
<div className="flex flex-wrap items-center justify-between gap-3">
<p className="subtle text-sm">
Page {page} of {totalPages}
</p>
<nav className="flex flex-wrap items-center gap-2" aria-label="Ideas pagination">
<button
type="button"
className="btn rounded-full px-4"
onClick={() => changePage(page - 1)}
disabled={page <= 1 || loading}
>
Previous
</button>
{pageItems.map((item) =>
typeof item === "number" ? (
<button
key={item}
type="button"
className={`btn min-w-10 rounded-full px-4 ${item === page ? "btn-primary" : ""}`}
onClick={() => changePage(item)}
aria-current={item === page ? "page" : undefined}
disabled={loading}
>
{item}
</button>
) : (
<span key={item} className="subtle px-1 text-sm" aria-hidden="true">
...
</span>
)
)}
<button
type="button"
className="btn rounded-full px-4"
onClick={() => changePage(page + 1)}
disabled={page >= totalPages || loading}
>
Next
</button>
</nav>
</div>
) : null}
</div>
);
}
|