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 / components / ProductForm.tsx
src/components/ProductForm.tsx · 358 lines · 10.7 KB← back to folder
| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | import { useRouter } from "next/navigation"; |
| 5 | |
| 6 | interface ProductFormProps { |
| 7 | mode: "create" | "edit"; |
| 8 | initial?: { |
| 9 | slug: string; |
| 10 | name: string; |
| 11 | website_url: string; |
| 12 | description: string; |
| 13 | readme_md: string; |
| 14 | icon_url: string | null; |
| 15 | screenshot_url: string | null; |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | export function ProductForm({ mode, initial }: ProductFormProps) { |
| 20 | const router = useRouter(); |
| 21 | const [name, setName] = useState(initial?.name ?? ""); |
| 22 | const [slug, setSlug] = useState(initial?.slug ?? ""); |
| 23 | const [websiteUrl, setWebsiteUrl] = useState(initial?.website_url ?? ""); |
| 24 | const [description, setDescription] = useState(initial?.description ?? ""); |
| 25 | const [iconUrl, setIconUrl] = useState(initial?.icon_url ?? ""); |
| 26 | const [screenshotUrl, setScreenshotUrl] = useState(initial?.screenshot_url ?? ""); |
| 27 | const [readmeMd, setReadmeMd] = useState( |
| 28 | initial?.readme_md ?? |
| 29 | `## About\n\nDescribe what your product does and who it is for.\n\n## Why I built it\n\nShare the story behind it.\n\n## Features\n\n- Feature one\n- Feature two\n` |
| 30 | ); |
| 31 | |
| 32 | const [tab, setTab] = useState<"write" | "preview">("write"); |
| 33 | const [previewHtml, setPreviewHtml] = useState(""); |
| 34 | const [loadingPreview, setLoadingPreview] = useState(false); |
| 35 | const [uploading, setUploading] = useState<"icon" | "screenshot" | null>(null); |
| 36 | const [submitting, setSubmitting] = useState(false); |
| 37 | const [error, setError] = useState<string | null>(null); |
| 38 | |
| 39 | async function handleUpload(file: File, kind: "icon" | "screenshot") { |
| 40 | setError(null); |
| 41 | setUploading(kind); |
| 42 | try { |
| 43 | const formData = new FormData(); |
| 44 | formData.append("file", file); |
| 45 | formData.append("kind", kind); |
| 46 | |
| 47 | const res = await fetch("/api/upload", { |
| 48 | method: "POST", |
| 49 | body: formData, |
| 50 | }); |
| 51 | const data = (await res.json()) as { url?: string; error?: string }; |
| 52 | if (!res.ok || !data.url) { |
| 53 | setError(data.error || "Upload failed."); |
| 54 | return; |
| 55 | } |
| 56 | if (kind === "icon") { |
| 57 | setIconUrl(data.url); |
| 58 | } else { |
| 59 | setScreenshotUrl(data.url); |
| 60 | } |
| 61 | } catch { |
| 62 | setError("Upload failed. Please try again."); |
| 63 | } finally { |
| 64 | setUploading(null); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | async function openPreview() { |
| 69 | setTab("preview"); |
| 70 | setLoadingPreview(true); |
| 71 | try { |
| 72 | const res = await fetch("/api/preview", { |
| 73 | method: "POST", |
| 74 | headers: { "Content-Type": "application/json" }, |
| 75 | body: JSON.stringify({ markdown: readmeMd }), |
| 76 | }); |
| 77 | const data = (await res.json()) as { html?: string }; |
| 78 | setPreviewHtml(data.html ?? ""); |
| 79 | } catch { |
| 80 | setPreviewHtml("<p>Unable to render preview.</p>"); |
| 81 | } finally { |
| 82 | setLoadingPreview(false); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | async function handleSubmit(e: React.FormEvent) { |
| 87 | e.preventDefault(); |
| 88 | setError(null); |
| 89 | setSubmitting(true); |
| 90 | |
| 91 | try { |
| 92 | const endpoint = |
| 93 | mode === "create" |
| 94 | ? "/api/products" |
| 95 | : `/api/products/${encodeURIComponent(initial!.slug)}`; |
| 96 | const method = mode === "create" ? "POST" : "PATCH"; |
| 97 | |
| 98 | const res = await fetch(endpoint, { |
| 99 | method, |
| 100 | headers: { "Content-Type": "application/json" }, |
| 101 | body: JSON.stringify({ |
| 102 | name, |
| 103 | ...(mode === "edit" ? { slug } : {}), |
| 104 | website_url: websiteUrl, |
| 105 | description, |
| 106 | readme_md: readmeMd, |
| 107 | icon_url: iconUrl || null, |
| 108 | screenshot_url: screenshotUrl || null, |
| 109 | }), |
| 110 | }); |
| 111 | |
| 112 | const data = (await res.json()) as { slug?: string; error?: string }; |
| 113 | if (!res.ok) { |
| 114 | setError(data.error || "Could not save product."); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | const targetSlug = data.slug ?? initial?.slug; |
| 119 | if (mode === "create" && targetSlug) { |
| 120 | window.minilytics?.("goal", "product_submitted", { slug: targetSlug }); |
| 121 | } |
| 122 | router.push(`/${targetSlug}`); |
| 123 | router.refresh(); |
| 124 | } catch { |
| 125 | setError("Network error while saving."); |
| 126 | } finally { |
| 127 | setSubmitting(false); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | async function handleDelete() { |
| 132 | if (!initial) return; |
| 133 | if (!window.confirm("Remove this product from Hacker Products?")) return; |
| 134 | |
| 135 | setSubmitting(true); |
| 136 | setError(null); |
| 137 | try { |
| 138 | const res = await fetch(`/api/products/${encodeURIComponent(initial.slug)}`, { |
| 139 | method: "DELETE", |
| 140 | }); |
| 141 | if (!res.ok) { |
| 142 | const data = (await res.json().catch(() => ({}))) as { error?: string }; |
| 143 | setError(data.error || "Could not remove product."); |
| 144 | return; |
| 145 | } |
| 146 | router.push("/"); |
| 147 | router.refresh(); |
| 148 | } catch { |
| 149 | setError("Network error while removing product."); |
| 150 | } finally { |
| 151 | setSubmitting(false); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return ( |
| 156 | <form className="plain-form" onSubmit={handleSubmit}> |
| 157 | {error && ( |
| 158 | <div className="form-error" role="alert"> |
| 159 | {error} |
| 160 | </div> |
| 161 | )} |
| 162 | |
| 163 | <div className="form-field"> |
| 164 | <label htmlFor="name">Product name</label> |
| 165 | <input |
| 166 | id="name" |
| 167 | type="text" |
| 168 | required |
| 169 | maxLength={80} |
| 170 | value={name} |
| 171 | onChange={(e) => setName(e.target.value)} |
| 172 | placeholder="Vessel" |
| 173 | /> |
| 174 | </div> |
| 175 | |
| 176 | {mode === "edit" && ( |
| 177 | <div className="form-field"> |
| 178 | <label htmlFor="slug">URL slug</label> |
| 179 | <input |
| 180 | id="slug" |
| 181 | type="text" |
| 182 | required |
| 183 | maxLength={70} |
| 184 | value={slug} |
| 185 | onChange={(e) => setSlug(e.target.value)} |
| 186 | placeholder="vessel" |
| 187 | /> |
| 188 | <p className="form-hint">hackerproducts.com/{slug || "slug"}</p> |
| 189 | </div> |
| 190 | )} |
| 191 | |
| 192 | <div className="form-field"> |
| 193 | <label htmlFor="website_url">Website URL</label> |
| 194 | <input |
| 195 | id="website_url" |
| 196 | type="url" |
| 197 | required |
| 198 | value={websiteUrl} |
| 199 | onChange={(e) => setWebsiteUrl(e.target.value)} |
| 200 | placeholder="https://vessel.sh" |
| 201 | /> |
| 202 | </div> |
| 203 | |
| 204 | <div className="form-field"> |
| 205 | <label htmlFor="description">Short description</label> |
| 206 | <input |
| 207 | id="description" |
| 208 | type="text" |
| 209 | required |
| 210 | maxLength={160} |
| 211 | value={description} |
| 212 | onChange={(e) => setDescription(e.target.value)} |
| 213 | placeholder="A fast, scriptable terminal workspace." |
| 214 | /> |
| 215 | <p className="form-hint">{description.length} / 160 characters</p> |
| 216 | </div> |
| 217 | |
| 218 | <div className="form-field"> |
| 219 | <label htmlFor="icon_file">Product icon (square, PNG/JPG/WebP)</label> |
| 220 | <div className="upload-row"> |
| 221 | <input |
| 222 | id="icon_file" |
| 223 | type="file" |
| 224 | accept="image/png,image/jpeg,image/webp,image/gif" |
| 225 | disabled={uploading !== null} |
| 226 | onChange={(e) => { |
| 227 | const file = e.target.files?.[0]; |
| 228 | if (file) void handleUpload(file, "icon"); |
| 229 | }} |
| 230 | /> |
| 231 | {uploading === "icon" && <span className="muted">Uploading...</span>} |
| 232 | {iconUrl && ( |
| 233 | <> |
| 234 | <img src={iconUrl} alt="Icon preview" width={24} height={24} className="product-icon" /> |
| 235 | <button |
| 236 | type="button" |
| 237 | className="link-button" |
| 238 | onClick={() => setIconUrl("")} |
| 239 | > |
| 240 | remove |
| 241 | </button> |
| 242 | </> |
| 243 | )} |
| 244 | </div> |
| 245 | <p className="form-hint">Optional. A small icon is generated automatically if omitted.</p> |
| 246 | </div> |
| 247 | |
| 248 | <div className="form-field"> |
| 249 | <label htmlFor="screenshot_file">Product screenshot</label> |
| 250 | <div className="upload-row"> |
| 251 | <input |
| 252 | id="screenshot_file" |
| 253 | type="file" |
| 254 | accept="image/png,image/jpeg,image/webp,image/gif" |
| 255 | disabled={uploading !== null} |
| 256 | onChange={(e) => { |
| 257 | const file = e.target.files?.[0]; |
| 258 | if (file) void handleUpload(file, "screenshot"); |
| 259 | }} |
| 260 | /> |
| 261 | {uploading === "screenshot" && <span className="muted">Uploading...</span>} |
| 262 | {screenshotUrl && ( |
| 263 | <button |
| 264 | type="button" |
| 265 | className="link-button" |
| 266 | onClick={() => setScreenshotUrl("")} |
| 267 | > |
| 268 | remove screenshot |
| 269 | </button> |
| 270 | )} |
| 271 | </div> |
| 272 | {screenshotUrl && ( |
| 273 | <img |
| 274 | src={screenshotUrl} |
| 275 | alt="Screenshot preview" |
| 276 | className="product-screenshot" |
| 277 | style={{ maxHeight: 260, width: "auto", marginTop: 10, marginBottom: 4 }} |
| 278 | /> |
| 279 | )} |
| 280 | <p className="form-hint">Also used as the social preview image when sharing on X or LinkedIn.</p> |
| 281 | </div> |
| 282 | |
| 283 | <div className="form-field"> |
| 284 | <label htmlFor="readme_md">README (Markdown)</label> |
| 285 | <div className="tabs-row"> |
| 286 | <button |
| 287 | type="button" |
| 288 | className="link-button" |
| 289 | style={{ fontWeight: tab === "write" ? 700 : 400 }} |
| 290 | onClick={() => setTab("write")} |
| 291 | > |
| 292 | write |
| 293 | </button> |
| 294 | <span className="nav-sep">/</span> |
| 295 | <button |
| 296 | type="button" |
| 297 | className="link-button" |
| 298 | style={{ fontWeight: tab === "preview" ? 700 : 400 }} |
| 299 | onClick={() => void openPreview()} |
| 300 | > |
| 301 | preview |
| 302 | </button> |
| 303 | </div> |
| 304 | |
| 305 | {tab === "write" ? ( |
| 306 | <textarea |
| 307 | id="readme_md" |
| 308 | rows={14} |
| 309 | required |
| 310 | value={readmeMd} |
| 311 | onChange={(e) => setReadmeMd(e.target.value)} |
| 312 | /> |
| 313 | ) : ( |
| 314 | <div className="preview-box"> |
| 315 | {loadingPreview ? ( |
| 316 | <p className="muted">Rendering preview...</p> |
| 317 | ) : ( |
| 318 | <div |
| 319 | className="readme" |
| 320 | dangerouslySetInnerHTML={{ __html: previewHtml }} |
| 321 | /> |
| 322 | )} |
| 323 | </div> |
| 324 | )} |
| 325 | <p className="form-hint"> |
| 326 | Supports standard Markdown: headings, paragraphs, lists, links, images, tables, and code blocks. |
| 327 | </p> |
| 328 | </div> |
| 329 | |
| 330 | <div style={{ display: "flex", gap: 12, alignItems: "center", flexWrap: "wrap" }}> |
| 331 | <button |
| 332 | type="submit" |
| 333 | className="btn" |
| 334 | disabled={submitting || uploading !== null} |
| 335 | data-ml-goal={mode === "create" ? "submit_product_click" : undefined} |
| 336 | > |
| 337 | {submitting |
| 338 | ? "Saving..." |
| 339 | : mode === "create" |
| 340 | ? "Publish product" |
| 341 | : "Save changes"} |
| 342 | </button> |
| 343 | |
| 344 | {mode === "edit" && ( |
| 345 | <button |
| 346 | type="button" |
| 347 | className="btn-danger" |
| 348 | disabled={submitting} |
| 349 | onClick={() => void handleDelete()} |
| 350 | > |
| 351 | Remove product |
| 352 | </button> |
| 353 | )} |
| 354 | </div> |
| 355 | </form> |
| 356 | ); |
| 357 | } |
| 358 |