"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 { if (totalPages <= 7) { return Array.from({ length: totalPages }, (_, index) => index + 1); } const items: Array = [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([]); const [totalCount, setTotalCount] = useState(null); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(0); const [error, setError] = useState(""); const [expanded, setExpanded] = useState>({}); 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 (

Fuzzy search supports approximate terms and minor typos.

setQueryInput(event.target.value)} aria-label="Search ideas" /> {queryInput ? ( ) : null}

{summaryLabel} {query ? ` matching "${query}"` : ""}

{loading ?

Loading ideas...

: null} {error ?

{error}

: null} {empty ? (

{query ? `No ideas found for "${query}".` : "No ideas yet. Be the first to submit one."}

) : null}
{items.map((idea, index) => { const workers = idea.working_developers || []; const isExpanded = Boolean(expanded[idea.id]); return (
{idea.title} {new Date(idea.createdAt).toLocaleString()}

{idea.problemStatement}

Submitter: {idea.submitter_label || "Anonymous"}

{idea.tags.length ? (
{idea.tags.slice(0, 6).map((tag) => ( {tag} ))}
) : null}

Upvotes

{idea.upvotesCount}

Comments

{idea.comment_count}

{isExpanded ? (

Developer list

{workers.length > 0 ? (
    {workers.map((name) => (
  • {name}
  • ))}
) : (

No developers have marked this topic yet.

)}
) : null}
); })}
{totalPages > 1 ? (

Page {page} of {totalPages}

) : null}
); }