cg / hackerproducts

60 files · 5,551 lines · 153.3 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.

Quick jump: page.tsx / page.tsx / globals.css / schema.sql / package.json

root / src / app / page.tsx

src/app/page.tsx · 153 lines · 5.5 KB← back to folder
1import Link from "next/link";
2import { getCurrentUser } from "@/lib/auth";
3import { formatDisplayDate, getDateKey } from "@/lib/format";
4import { listPublishedProducts, type ProductListItem } from "@/lib/products";
5import { ProductIcon } from "@/components/ProductIcon";
6import { UpvoteButton } from "@/components/UpvoteButton";
7
8export const dynamic = "force-dynamic";
9
10const PAGE_SIZE = 25;
11
12function buildPageHref(page: number, sort: "newest" | "trending"): string {
13 const params = new URLSearchParams();
14 if (sort === "trending") params.set("sort", "trending");
15 if (page > 1) params.set("page", String(page));
16 const qs = params.toString();
17 return qs ? `/?${qs}` : "/";
18}
19
20export default async function HomePage({
21 searchParams,
22}: {
23 searchParams: Promise<{ page?: string; sort?: string }>;
24}) {
25 const params = await searchParams;
26 const page = Math.max(1, Number.parseInt(params.page ?? "1", 10) || 1);
27 const sort: "newest" | "trending" = params.sort === "trending" ? "trending" : "newest";
28 const user = await getCurrentUser();
29 const { products, hasMore } = await listPublishedProducts({
30 page,
31 pageSize: PAGE_SIZE,
32 viewerUserId: user?.id ?? null,
33 sort,
34 });
35
36 // Group chronologically by UTC publication date for newest view
37 const groups: Array<{ dateKey: string; label: string; items: ProductListItem[] }> = [];
38 if (sort === "newest") {
39 for (const product of products) {
40 const dateKey = getDateKey(product.published_at);
41 const last = groups[groups.length - 1];
42 if (last && last.dateKey === dateKey) {
43 last.items.push(product);
44 } else {
45 groups.push({
46 dateKey,
47 label: formatDisplayDate(product.published_at),
48 items: [product],
49 });
50 }
51 }
52 }
53
54 let runningIndex = (page - 1) * PAGE_SIZE;
55 const returnTo = buildPageHref(page, sort);
56
57 return (
58 <div>
59 <h1>Products</h1>
60 <p className="subtitle" style={{ marginBottom: 14 }}>
61 A list of products built by hackers.
62 </p>
63
64 <div className="tabs-row" style={{ marginBottom: 24 }}>
65 <Link href="/" style={{ fontWeight: sort === "newest" ? 700 : 400 }}>
66 newest
67 </Link>
68 <span className="nav-sep" aria-hidden="true">
69 /
70 </span>
71 <Link href="/?sort=trending" style={{ fontWeight: sort === "trending" ? 700 : 400 }}>
72 trending
73 </Link>
74 </div>
75
76 {products.length === 0 ? (
77 <p className="muted">
78 No products published yet. <Link href="/submit">Submit the first product</Link>.
79 </p>
80 ) : sort === "trending" ? (
81 <section className="date-group" style={{ marginTop: 12 }}>
82 <h2 className="date-heading">Top upvoted</h2>
83 <ol className="product-list">
84 {products.map((product) => {
85 runningIndex += 1;
86 return (
87 <li key={product.id} className="product-row">
88 <span className="product-index">{runningIndex}.</span>
89 <ProductIcon name={product.name} iconUrl={product.icon_url} />
90 <span className="product-row-body">
91 <Link href={`/${product.slug}`} className="product-name-link">
92 {product.name}
93 </Link>
94 <span className="product-sep"> · </span>
95 <span className="product-desc">{product.description}</span>
96 <UpvoteButton
97 slug={product.slug}
98 initialCount={product.upvote_count}
99 initialUpvoted={product.upvoted_by_me}
100 isSignedIn={Boolean(user)}
101 variant="inline"
102 returnTo={returnTo}
103 />
104 </span>
105 </li>
106 );
107 })}
108 </ol>
109 </section>
110 ) : (
111 groups.map((group) => (
112 <section key={group.dateKey} className="date-group">
113 <h2 className="date-heading">{group.label}</h2>
114 <ol className="product-list">
115 {group.items.map((product) => {
116 runningIndex += 1;
117 return (
118 <li key={product.id} className="product-row">
119 <span className="product-index">{runningIndex}.</span>
120 <ProductIcon name={product.name} iconUrl={product.icon_url} />
121 <span className="product-row-body">
122 <Link href={`/${product.slug}`} className="product-name-link">
123 {product.name}
124 </Link>
125 <span className="product-sep"> · </span>
126 <span className="product-desc">{product.description}</span>
127 <UpvoteButton
128 slug={product.slug}
129 initialCount={product.upvote_count}
130 initialUpvoted={product.upvoted_by_me}
131 isSignedIn={Boolean(user)}
132 variant="inline"
133 returnTo={returnTo}
134 />
135 </span>
136 </li>
137 );
138 })}
139 </ol>
140 </section>
141 ))
142 )}
143
144 {(page > 1 || hasMore) && (
145 <nav className="pagination" aria-label="Pagination">
146 {page > 1 && <Link href={buildPageHref(page - 1, sort)}>← newer</Link>}
147 {hasMore && <Link href={buildPageHref(page + 1, sort)}>older →</Link>}
148 </nav>
149 )}
150 </div>
151 );
152}
153
cg / hackerproducts — hacker products