Jacksuyu commited on
Commit
946dcc2
·
1 Parent(s): af2571a

feat: promote search UI and tag social submissions

Browse files
Files changed (2) hide show
  1. app/api/ideas/route.ts +40 -2
  2. components/HomeList.tsx +27 -27
app/api/ideas/route.ts CHANGED
@@ -3,7 +3,7 @@ import { z } from "zod";
3
  import { prisma } from "@/lib/prisma";
4
  import { generateSpec } from "@/lib/llm";
5
  import { getAnonId, getClientIp } from "@/lib/identity";
6
- import { mapIdea, mapTask } from "@/lib/db-mappers";
7
  import { scoreHot } from "@/lib/hot-score";
8
  import { auth } from "@/auth";
9
  import { findBlockedContent } from "@/lib/content-moderation";
@@ -16,11 +16,34 @@ const createIdeaSchema = z.object({
16
  target_users: z.string().max(300).optional(),
17
  platform: z.enum(["Web", "Mobile", "Desktop", "Any"]).optional(),
18
  constraints: z.string().max(500).optional(),
 
19
  show_name: z.boolean().optional().default(false),
20
  });
21
 
22
  const RATE_LIMIT_PER_HOUR = 20;
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  async function checkRateLimit(rateKey: string, ipAddress: string): Promise<boolean> {
25
  const now = new Date();
26
  const windowStart = `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()}-${now.getUTCHours()}`;
@@ -210,6 +233,8 @@ export async function POST(request: NextRequest) {
210
  return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
211
  }
212
 
 
 
213
  const moderation = findBlockedContent(parsed.data.raw_input_text, parsed.data.target_users, parsed.data.constraints);
214
  if (moderation.blocked) {
215
  return NextResponse.json(
@@ -248,6 +273,17 @@ export async function POST(request: NextRequest) {
248
  const existing = await prisma.idea.findUnique({ where: { id: mergeTarget.targetIdeaId } });
249
 
250
  if (existing) {
 
 
 
 
 
 
 
 
 
 
 
251
  await prisma.ideaMerge.create({
252
  data: {
253
  targetIdeaId: existing.id,
@@ -299,6 +335,8 @@ export async function POST(request: NextRequest) {
299
  profile?.displayName || session?.user?.name || session?.user?.email || "Member";
300
  }
301
 
 
 
302
  const idea = await prisma.idea.create({
303
  data: {
304
  createdByAnonId: anonId || null,
@@ -311,7 +349,7 @@ export async function POST(request: NextRequest) {
311
  constraints: parsed.data.constraints,
312
  title: displayTitle,
313
  problemStatement: spec.problem_statement,
314
- tags: JSON.stringify(spec.tags),
315
  features: JSON.stringify(spec.features),
316
  openQuestions: JSON.stringify(spec.open_questions),
317
  tasks: {
 
3
  import { prisma } from "@/lib/prisma";
4
  import { generateSpec } from "@/lib/llm";
5
  import { getAnonId, getClientIp } from "@/lib/identity";
6
+ import { mapIdea, mapTask, parseJsonArray } from "@/lib/db-mappers";
7
  import { scoreHot } from "@/lib/hot-score";
8
  import { auth } from "@/auth";
9
  import { findBlockedContent } from "@/lib/content-moderation";
 
16
  target_users: z.string().max(300).optional(),
17
  platform: z.enum(["Web", "Mobile", "Desktop", "Any"]).optional(),
18
  constraints: z.string().max(500).optional(),
19
+ source_tag: z.string().max(40).optional(),
20
  show_name: z.boolean().optional().default(false),
21
  });
22
 
23
  const RATE_LIMIT_PER_HOUR = 20;
24
 
25
+ function normalizeSourceTag(sourceTag?: string): string | null {
26
+ const rawTag = sourceTag?.trim();
27
+ if (!rawTag) return null;
28
+ return rawTag.replace(/\s+/g, "_").slice(0, 40);
29
+ }
30
+
31
+ function mergeIdeaTags(primaryTags: string[], extraTags: string[]): string[] {
32
+ const seen = new Set<string>();
33
+ const merged: string[] = [];
34
+
35
+ for (const rawTag of [...primaryTags, ...extraTags]) {
36
+ const tag = rawTag.trim();
37
+ if (!tag) continue;
38
+ const key = tag.toLowerCase();
39
+ if (seen.has(key)) continue;
40
+ seen.add(key);
41
+ merged.push(tag);
42
+ }
43
+
44
+ return merged;
45
+ }
46
+
47
  async function checkRateLimit(rateKey: string, ipAddress: string): Promise<boolean> {
48
  const now = new Date();
49
  const windowStart = `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()}-${now.getUTCHours()}`;
 
233
  return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
234
  }
235
 
236
+ const sourceTag = normalizeSourceTag(parsed.data.source_tag);
237
+
238
  const moderation = findBlockedContent(parsed.data.raw_input_text, parsed.data.target_users, parsed.data.constraints);
239
  if (moderation.blocked) {
240
  return NextResponse.json(
 
273
  const existing = await prisma.idea.findUnique({ where: { id: mergeTarget.targetIdeaId } });
274
 
275
  if (existing) {
276
+ if (sourceTag) {
277
+ const existingTags = parseJsonArray<string>(existing.tags);
278
+ const mergedTags = mergeIdeaTags(existingTags, [sourceTag]);
279
+ if (mergedTags.length !== existingTags.length) {
280
+ await prisma.idea.update({
281
+ where: { id: existing.id },
282
+ data: { tags: JSON.stringify(mergedTags) },
283
+ });
284
+ }
285
+ }
286
+
287
  await prisma.ideaMerge.create({
288
  data: {
289
  targetIdeaId: existing.id,
 
335
  profile?.displayName || session?.user?.name || session?.user?.email || "Member";
336
  }
337
 
338
+ const ideaTags = mergeIdeaTags(Array.isArray(spec.tags) ? spec.tags : [], sourceTag ? [sourceTag] : []);
339
+
340
  const idea = await prisma.idea.create({
341
  data: {
342
  createdByAnonId: anonId || null,
 
349
  constraints: parsed.data.constraints,
350
  title: displayTitle,
351
  problemStatement: spec.problem_statement,
352
+ tags: JSON.stringify(ideaTags),
353
  features: JSON.stringify(spec.features),
354
  openQuestions: JSON.stringify(spec.open_questions),
355
  tasks: {
components/HomeList.tsx CHANGED
@@ -73,6 +73,29 @@ export function HomeList() {
73
 
74
  return (
75
  <div className="space-y-4">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  <div className="flex flex-wrap items-center justify-between gap-2">
77
  <div className="flex items-center gap-2 rounded-full border border-white/70 bg-white/60 p-1">
78
  <button onClick={() => setSort("hot")} className={`btn rounded-full px-4 ${sort === "hot" ? "btn-primary" : ""}`}>
@@ -82,35 +105,12 @@ export function HomeList() {
82
  New
83
  </button>
84
  </div>
85
-
86
- <div className="flex flex-col items-start gap-1">
87
- <label className="subtle text-xs font-medium uppercase tracking-[0.12em]" htmlFor="idea-search-input">
88
- Search (Fuzzy)
89
- </label>
90
- <div className="flex items-center gap-2">
91
- <input
92
- id="idea-search-input"
93
- type="search"
94
- className="min-w-[240px] rounded-full border border-zinc-300 bg-white/90 px-4 py-2 text-sm outline-none focus:border-zinc-500"
95
- placeholder="Try approximate terms or typos"
96
- value={queryInput}
97
- onChange={(event) => setQueryInput(event.target.value)}
98
- aria-label="Search ideas"
99
- />
100
- {queryInput ? (
101
- <button type="button" className="btn rounded-full px-3" onClick={() => setQueryInput("")}>
102
- Clear
103
- </button>
104
- ) : null}
105
- </div>
106
- </div>
107
  </div>
108
 
109
- <p className="subtle text-sm">
110
- {items.length} ideas
111
- {query ? ` matching "${query}"` : ""}
112
- </p>
113
-
114
  {loading ? <p className="subtle text-sm">Loading ideas...</p> : null}
115
  {error ? <p className="text-sm text-red-700">{error}</p> : null}
116
  {empty ? (
 
73
 
74
  return (
75
  <div className="space-y-4">
76
+ <div className="card p-4 md:p-5">
77
+ <label className="text-sm font-semibold uppercase tracking-[0.16em] text-zinc-600" htmlFor="idea-search-input">
78
+ Search Ideas
79
+ </label>
80
+ <p className="subtle mt-1 text-sm">Fuzzy search supports approximate terms and minor typos.</p>
81
+ <div className="mt-3 flex flex-col gap-2 md:flex-row md:items-center">
82
+ <input
83
+ id="idea-search-input"
84
+ type="search"
85
+ className="input w-full rounded-full px-5 py-3 text-base"
86
+ placeholder="Search by problem, keywords, or rough phrasing"
87
+ value={queryInput}
88
+ onChange={(event) => setQueryInput(event.target.value)}
89
+ aria-label="Search ideas"
90
+ />
91
+ {queryInput ? (
92
+ <button type="button" className="btn rounded-full px-4" onClick={() => setQueryInput("")}>
93
+ Clear
94
+ </button>
95
+ ) : null}
96
+ </div>
97
+ </div>
98
+
99
  <div className="flex flex-wrap items-center justify-between gap-2">
100
  <div className="flex items-center gap-2 rounded-full border border-white/70 bg-white/60 p-1">
101
  <button onClick={() => setSort("hot")} className={`btn rounded-full px-4 ${sort === "hot" ? "btn-primary" : ""}`}>
 
105
  New
106
  </button>
107
  </div>
108
+ <p className="subtle text-sm">
109
+ {items.length} ideas
110
+ {query ? ` matching "${query}"` : ""}
111
+ </p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  </div>
113
 
 
 
 
 
 
114
  {loading ? <p className="subtle text-sm">Loading ideas...</p> : null}
115
  {error ? <p className="text-sm text-red-700">{error}</p> : null}
116
  {empty ? (