cg / hackerproducts

63 files · 5,740 lines · 159.1 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 / github / page.tsx

src/app/github/page.tsx · 284 lines · 9.2 KB← back to folder
1import type { Metadata } from "next";
2import Link from "next/link";
3import snapshot from "@/generated/source-snapshot.json";
4import { CopyCodeButton } from "@/components/CopyCodeButton";
5
6export const metadata: Metadata = {
7 title: "cg / hackerproducts",
8 description: "Browse the complete source code of Hacker Products right in your browser.",
9};
10
11interface SnapshotFile {
12 path: string;
13 lines: number;
14 bytes: number;
15 content: string;
16}
17
18const FEATURED_FILES = [
19 "src/app/page.tsx",
20 "src/app/[username]/page.tsx",
21 "src/app/globals.css",
22 "src/db/schema.sql",
23 "package.json",
24];
25
26function formatBytes(bytes: number): string {
27 if (bytes < 1024) return `${bytes} B`;
28 return `${(bytes / 1024).toFixed(1)} KB`;
29}
30
31export default async function GithubPage({
32 searchParams,
33}: {
34 searchParams: Promise<{ file?: string; dir?: string }>;
35}) {
36 const params = await searchParams;
37 const files = snapshot.files as SnapshotFile[];
38 const selectedPath = params.file || "";
39 const selectedFile = selectedPath
40 ? files.find((f) => f.path === selectedPath) ?? null
41 : null;
42
43 const currentDir = params.dir ? params.dir.replace(/^\/+|\/+$/g, "") : "";
44 const prefix = currentDir ? `${currentDir}/` : "";
45
46 // Build immediate subdirectories and files for currentDir
47 const subdirs = new Map<string, { count: number; lines: number }>();
48 const directFiles: SnapshotFile[] = [];
49
50 for (const f of files) {
51 if (!f.path.startsWith(prefix)) continue;
52 const rest = f.path.slice(prefix.length);
53 const slash = rest.indexOf("/");
54 if (slash === -1) {
55 directFiles.push(f);
56 } else {
57 const folder = rest.slice(0, slash);
58 const prev = subdirs.get(folder) ?? { count: 0, lines: 0 };
59 prev.count += 1;
60 prev.lines += f.lines;
61 subdirs.set(folder, prev);
62 }
63 }
64
65 const sortedDirs = Array.from(subdirs.entries()).sort((a, b) => a[0].localeCompare(b[0]));
66
67 // Breadcrumb segments
68 const activePath = selectedFile ? selectedFile.path : currentDir;
69 const crumbs = activePath ? activePath.split("/") : [];
70
71 return (
72 <div>
73 <h1>
74 <Link href="/cg" style={{ color: "#0000ee" }}>
75 cg
76 </Link>{" "}
77 /{" "}
78 <Link href="/github" style={{ color: "#000000", textDecoration: "none" }}>
79 hackerproducts
80 </Link>
81 </h1>
82
83 <p className="subtitle" style={{ marginBottom: 12 }}>
84 {snapshot.fileCount} files · {snapshot.totalLines.toLocaleString("en-US")} lines ·{" "}
85 {formatBytes(snapshot.totalBytes)} — Next.js 15, React 19, TypeScript, PostgreSQL,
86 Object Storage, plain CSS.
87 </p>
88
89 <p style={{ margin: "0 0 22px", fontSize: 14 }}>
90 <span className="muted">Quick jump: </span>
91 {FEATURED_FILES.map((fp, i) => (
92 <span key={fp}>
93 {i > 0 && <span className="nav-sep"> / </span>}
94 <Link
95 href={`/github?file=${encodeURIComponent(fp)}`}
96 style={{ fontWeight: selectedFile?.path === fp ? 700 : 400 }}
97 >
98 {fp.split("/").pop()}
99 </Link>
100 </span>
101 ))}
102 </p>
103
104 {crumbs.length > 0 && (
105 <p style={{ margin: "0 0 14px", fontSize: 14, fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace" }}>
106 <Link href="/github">root</Link>
107 {crumbs.map((part, idx) => {
108 const upTo = crumbs.slice(0, idx + 1).join("/");
109 const isLast = idx === crumbs.length - 1;
110 const isFile = isLast && Boolean(selectedFile);
111 return (
112 <span key={upTo}>
113 {" / "}
114 {isFile ? (
115 <strong>{part}</strong>
116 ) : (
117 <Link href={`/github?dir=${encodeURIComponent(upTo)}`}>{part}</Link>
118 )}
119 </span>
120 );
121 })}
122 </p>
123 )}
124
125 {selectedFile ? (
126 <div>
127 <div
128 style={{
129 display: "flex",
130 justifyContent: "space-between",
131 alignItems: "baseline",
132 gap: 12,
133 flexWrap: "wrap",
134 padding: "6px 10px",
135 border: "1px solid #dddddd",
136 borderBottom: "none",
137 background: "#f6f6f6",
138 fontSize: 13,
139 fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
140 }}
141 >
142 <span>
143 <strong>{selectedFile.path}</strong> · {selectedFile.lines} lines ·{" "}
144 {formatBytes(selectedFile.bytes)}
145 </span>
146 <span style={{ display: "flex", gap: 12 }}>
147 <CopyCodeButton content={selectedFile.content} />
148 <Link
149 href={
150 selectedFile.path.includes("/")
151 ? `/github?dir=${encodeURIComponent(
152 selectedFile.path.slice(0, selectedFile.path.lastIndexOf("/"))
153 )}`
154 : "/github"
155 }
156 >
157 ← back to folder
158 </Link>
159 </span>
160 </div>
161
162 <div
163 style={{
164 border: "1px solid #dddddd",
165 overflowX: "auto",
166 background: "#ffffff",
167 }}
168 >
169 <table
170 style={{
171 width: "100%",
172 borderCollapse: "collapse",
173 fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
174 fontSize: 13,
175 lineHeight: 1.5,
176 }}
177 >
178 <tbody>
179 {selectedFile.content.split("\n").map((line, i) => (
180 <tr key={i}>
181 <td
182 style={{
183 width: 48,
184 textAlign: "right",
185 padding: "0 10px",
186 color: "#888888",
187 userSelect: "none",
188 verticalAlign: "top",
189 borderRight: "1px solid #eeeeee",
190 }}
191 >
192 {i + 1}
193 </td>
194 <td
195 style={{
196 padding: "0 12px",
197 whiteSpace: "pre",
198 color: "#111111",
199 }}
200 >
201 {line || " "}
202 </td>
203 </tr>
204 ))}
205 </tbody>
206 </table>
207 </div>
208 </div>
209 ) : (
210 <table className="plain-table">
211 <thead>
212 <tr>
213 <th>Name</th>
214 <th style={{ textAlign: "right" }}>Lines</th>
215 <th style={{ textAlign: "right" }}>Size</th>
216 </tr>
217 </thead>
218 <tbody>
219 {currentDir && (
220 <tr>
221 <td colSpan={3}>
222 <Link
223 href={
224 currentDir.includes("/")
225 ? `/github?dir=${encodeURIComponent(
226 currentDir.slice(0, currentDir.lastIndexOf("/"))
227 )}`
228 : "/github"
229 }
230 >
231 ..
232 </Link>
233 </td>
234 </tr>
235 )}
236 {sortedDirs.map(([folder, stats]) => {
237 const nextDir = prefix ? `${prefix}${folder}` : folder;
238 return (
239 <tr key={nextDir}>
240 <td style={{ fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace" }}>
241 <Link href={`/github?dir=${encodeURIComponent(nextDir)}`}>{folder}/</Link>
242 </td>
243 <td style={{ textAlign: "right" }} className="muted">
244 {stats.lines.toLocaleString("en-US")}
245 </td>
246 <td style={{ textAlign: "right" }} className="muted">
247 {stats.count} {stats.count === 1 ? "file" : "files"}
248 </td>
249 </tr>
250 );
251 })}
252 {directFiles.map((file) => (
253 <tr key={file.path}>
254 <td style={{ fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace" }}>
255 <Link href={`/github?file=${encodeURIComponent(file.path)}`}>
256 {file.path.slice(prefix.length)}
257 </Link>
258 </td>
259 <td style={{ textAlign: "right" }} className="muted">
260 {file.lines.toLocaleString("en-US")}
261 </td>
262 <td style={{ textAlign: "right" }} className="muted">
263 {formatBytes(file.bytes)}
264 </td>
265 </tr>
266 ))}
267 </tbody>
268 </table>
269 )}
270
271 {!selectedFile && !currentDir && (
272 <div style={{ marginTop: 32 }}>
273 <h2>About this repo</h2>
274 <p>
275 Hacker Products is built as a tiny, server-rendered Next.js App Router site with zero
276 UI component libraries, zero gradients, and zero client bloat. Every file in this
277 browser is snapshotted at build time from the live repo.
278 </p>
279 </div>
280 )}
281 </div>
282 );
283}
284
cg / hackerproducts — hacker products