cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / app / [username] / page.tsx
src/app/[username]/page.tsx · 268 lines · 8.1 KB← back to folder
| 1 | import type { Metadata } from "next"; |
| 2 | import Link from "next/link"; |
| 3 | import { notFound } from "next/navigation"; |
| 4 | import { getCurrentUser, getSiteUrl } from "@/lib/auth"; |
| 5 | import { formatDisplayDate, getFallbackIconColor } from "@/lib/format"; |
| 6 | import { renderMarkdown } from "@/lib/markdown"; |
| 7 | import { |
| 8 | getMakerByUsername, |
| 9 | getProductBySlug, |
| 10 | listProductsByMaker, |
| 11 | } from "@/lib/products"; |
| 12 | import { listCommentsForProduct } from "@/lib/comments"; |
| 13 | import { ProductIcon } from "@/components/ProductIcon"; |
| 14 | import { UpvoteButton } from "@/components/UpvoteButton"; |
| 15 | import { ShareButton } from "@/components/ShareButton"; |
| 16 | import { CommentsSection } from "@/components/CommentsSection"; |
| 17 | |
| 18 | export const dynamic = "force-dynamic"; |
| 19 | |
| 20 | export async function generateMetadata({ |
| 21 | params, |
| 22 | }: { |
| 23 | params: Promise<{ username: string }>; |
| 24 | }): Promise<Metadata> { |
| 25 | const { username: slug } = await params; |
| 26 | const siteUrl = getSiteUrl(); |
| 27 | |
| 28 | const product = await getProductBySlug(slug); |
| 29 | if (product) { |
| 30 | const canonicalUrl = `${siteUrl}/${product.slug}`; |
| 31 | return { |
| 32 | title: product.name, |
| 33 | description: product.description, |
| 34 | alternates: { canonical: canonicalUrl }, |
| 35 | openGraph: { |
| 36 | title: `${product.name} — hacker products`, |
| 37 | description: product.description, |
| 38 | url: canonicalUrl, |
| 39 | siteName: "hacker products", |
| 40 | type: "article", |
| 41 | publishedTime: new Date(product.published_at).toISOString(), |
| 42 | authors: [product.owner_username], |
| 43 | images: product.screenshot_url |
| 44 | ? [{ url: product.screenshot_url, alt: `${product.name} screenshot` }] |
| 45 | : product.icon_url |
| 46 | ? [{ url: product.icon_url, alt: `${product.name} icon` }] |
| 47 | : undefined, |
| 48 | }, |
| 49 | twitter: { |
| 50 | card: product.screenshot_url ? "summary_large_image" : "summary", |
| 51 | title: `${product.name} — hacker products`, |
| 52 | description: product.description, |
| 53 | images: product.screenshot_url |
| 54 | ? [product.screenshot_url] |
| 55 | : product.icon_url |
| 56 | ? [product.icon_url] |
| 57 | : undefined, |
| 58 | }, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | const maker = await getMakerByUsername(slug); |
| 63 | if (maker) { |
| 64 | const description = |
| 65 | maker.bio || `${maker.username}'s products on Hacker Products.`; |
| 66 | return { |
| 67 | title: maker.username, |
| 68 | description, |
| 69 | alternates: { canonical: `${siteUrl}/${maker.username}` }, |
| 70 | openGraph: { |
| 71 | title: `${maker.username} — hacker products`, |
| 72 | description, |
| 73 | url: `${siteUrl}/${maker.username}`, |
| 74 | type: "profile", |
| 75 | }, |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | return { title: "Not found" }; |
| 80 | } |
| 81 | |
| 82 | export default async function SlugOrMakerPage({ |
| 83 | params, |
| 84 | }: { |
| 85 | params: Promise<{ username: string }>; |
| 86 | }) { |
| 87 | const { username: slug } = await params; |
| 88 | const viewer = await getCurrentUser(); |
| 89 | |
| 90 | // 1. Check if slug matches a product |
| 91 | const product = await getProductBySlug(slug, viewer?.id ?? null, true); |
| 92 | if (product) { |
| 93 | const isOwnerOrAdmin = Boolean( |
| 94 | viewer && (viewer.id === product.owner_id || viewer.is_admin) |
| 95 | ); |
| 96 | if (product.status !== "published" && !isOwnerOrAdmin) { |
| 97 | notFound(); |
| 98 | } |
| 99 | |
| 100 | const readmeHtml = await renderMarkdown(product.readme_md); |
| 101 | const comments = await listCommentsForProduct(product.id); |
| 102 | const shareUrl = `${getSiteUrl()}/${product.slug}`; |
| 103 | |
| 104 | return ( |
| 105 | <article> |
| 106 | {product.status !== "published" && ( |
| 107 | <p className="form-status"> |
| 108 | Status: <strong>{product.status}</strong> (only visible to you and moderators) |
| 109 | </p> |
| 110 | )} |
| 111 | |
| 112 | <h1>{product.name}</h1> |
| 113 | |
| 114 | <p className="product-meta"> |
| 115 | by <Link href={`/${product.owner_username}`}>{product.owner_username}</Link> /{" "} |
| 116 | {formatDisplayDate(product.published_at)} |
| 117 | {isOwnerOrAdmin && ( |
| 118 | <> |
| 119 | {" "} |
| 120 | / <Link href={`/${product.slug}/edit`}>edit</Link> |
| 121 | </> |
| 122 | )} |
| 123 | </p> |
| 124 | |
| 125 | <p className="product-website"> |
| 126 | <a href={product.website_url} target="_blank" rel="noopener noreferrer"> |
| 127 | {product.website_url} |
| 128 | </a> |
| 129 | </p> |
| 130 | |
| 131 | <p className="product-summary">{product.description}</p> |
| 132 | |
| 133 | <div className="product-actions"> |
| 134 | <UpvoteButton |
| 135 | slug={product.slug} |
| 136 | initialCount={product.upvote_count} |
| 137 | initialUpvoted={product.upvoted_by_me} |
| 138 | isSignedIn={Boolean(viewer)} |
| 139 | variant="button" |
| 140 | /> |
| 141 | <ShareButton url={shareUrl} /> |
| 142 | {isOwnerOrAdmin && ( |
| 143 | <> |
| 144 | <span className="nav-sep" aria-hidden="true"> |
| 145 | / |
| 146 | </span> |
| 147 | <Link href={`/${product.slug}/edit`}>edit product</Link> |
| 148 | </> |
| 149 | )} |
| 150 | </div> |
| 151 | |
| 152 | {product.screenshot_url && ( |
| 153 | <img |
| 154 | src={product.screenshot_url} |
| 155 | alt={`${product.name} screenshot`} |
| 156 | className="product-screenshot" |
| 157 | /> |
| 158 | )} |
| 159 | |
| 160 | <div className="readme" dangerouslySetInnerHTML={{ __html: readmeHtml }} /> |
| 161 | |
| 162 | <CommentsSection |
| 163 | slug={product.slug} |
| 164 | productOwnerId={product.owner_id} |
| 165 | initialComments={comments} |
| 166 | viewer={ |
| 167 | viewer |
| 168 | ? { id: viewer.id, username: viewer.username, is_admin: viewer.is_admin } |
| 169 | : null |
| 170 | } |
| 171 | /> |
| 172 | </article> |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | // 2. Otherwise check if slug matches a maker profile |
| 177 | const maker = await getMakerByUsername(slug); |
| 178 | if (!maker) { |
| 179 | notFound(); |
| 180 | } |
| 181 | |
| 182 | const isSelf = Boolean(viewer && viewer.id === maker.id); |
| 183 | const products = await listProductsByMaker( |
| 184 | maker.id, |
| 185 | viewer?.id ?? null, |
| 186 | isSelf || Boolean(viewer?.is_admin) |
| 187 | ); |
| 188 | |
| 189 | return ( |
| 190 | <div> |
| 191 | <div className="maker-header"> |
| 192 | {maker.avatar_url ? ( |
| 193 | <img |
| 194 | src={maker.avatar_url} |
| 195 | alt={maker.username} |
| 196 | width={48} |
| 197 | height={48} |
| 198 | className="maker-avatar" |
| 199 | /> |
| 200 | ) : ( |
| 201 | <span |
| 202 | className="maker-avatar" |
| 203 | style={{ backgroundColor: getFallbackIconColor(maker.username) }} |
| 204 | aria-hidden="true" |
| 205 | > |
| 206 | {maker.username[0].toUpperCase()} |
| 207 | </span> |
| 208 | )} |
| 209 | <div> |
| 210 | <h1 style={{ marginBottom: 2 }}>{maker.username}</h1> |
| 211 | {isSelf && ( |
| 212 | <p className="muted" style={{ margin: 0, fontSize: 14 }}> |
| 213 | <Link href="/settings">edit profile</Link> |
| 214 | </p> |
| 215 | )} |
| 216 | </div> |
| 217 | </div> |
| 218 | |
| 219 | {maker.bio && <p className="product-summary">{maker.bio}</p>} |
| 220 | |
| 221 | {maker.website_url && ( |
| 222 | <p className="product-website"> |
| 223 | <a href={maker.website_url} target="_blank" rel="noopener noreferrer"> |
| 224 | {maker.website_url} |
| 225 | </a> |
| 226 | </p> |
| 227 | )} |
| 228 | |
| 229 | <h2>Products</h2> |
| 230 | {products.length === 0 ? ( |
| 231 | <p className="muted">No products published yet.</p> |
| 232 | ) : ( |
| 233 | <ol className="product-list"> |
| 234 | {products.map((item, index) => ( |
| 235 | <li key={item.id} className="product-row"> |
| 236 | <span className="product-index">{index + 1}.</span> |
| 237 | <ProductIcon name={item.name} iconUrl={item.icon_url} /> |
| 238 | <span className="product-row-body"> |
| 239 | <Link href={`/${item.slug}`} className="product-name-link"> |
| 240 | {item.name} |
| 241 | </Link> |
| 242 | {item.status !== "published" && ( |
| 243 | <span className="muted"> [{item.status}]</span> |
| 244 | )} |
| 245 | <span className="product-sep"> · </span> |
| 246 | <span className="product-desc">{item.description}</span> |
| 247 | <UpvoteButton |
| 248 | slug={item.slug} |
| 249 | initialCount={item.upvote_count} |
| 250 | initialUpvoted={item.upvoted_by_me} |
| 251 | isSignedIn={Boolean(viewer)} |
| 252 | variant="inline" |
| 253 | returnTo={`/${maker.username}`} |
| 254 | /> |
| 255 | {(isSelf || viewer?.is_admin) && ( |
| 256 | <span className="star-inline"> |
| 257 | · <Link href={`/${item.slug}/edit`}>edit</Link> |
| 258 | </span> |
| 259 | )} |
| 260 | </span> |
| 261 | </li> |
| 262 | ))} |
| 263 | </ol> |
| 264 | )} |
| 265 | </div> |
| 266 | ); |
| 267 | } |
| 268 |