"use client" import * as React from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Textarea } from "@/components/ui/textarea" import { Badge } from "@/components/ui/badge" import { useMutation } from "convex/react" import { api } from "@/convex/_generated/api" type SectionKey = | "profile" | "features" | "competitors" | "keywords" | "problems" | "personas" | "useCases" | "dorkQueries" function summarizeItem(item: any) { if (typeof item === "string") return item if (!item || typeof item !== "object") return String(item) if (item.name && item.role) return `${item.name} ยท ${item.role}` if (item.name) return item.name if (item.term) return item.term if (item.problem) return item.problem if (item.scenario) return item.scenario if (item.query) return item.query return JSON.stringify(item) } export function SectionEditor({ analysisId, sectionKey, title, items, }: { analysisId: string sectionKey: SectionKey title: string items: any[] }) { const addItem = useMutation(api.analysisSections.addItem) const removeItem = useMutation(api.analysisSections.removeItem) const [isRepromptOpen, setIsRepromptOpen] = React.useState(false) const [repromptText, setRepromptText] = React.useState("") const [isAddOpen, setIsAddOpen] = React.useState(false) const [addText, setAddText] = React.useState("") const [isBusy, setIsBusy] = React.useState(false) const handleAdd = async () => { setIsBusy(true) try { let parsed: any = addText if (addText.trim().startsWith("{") || addText.trim().startsWith("[")) { parsed = JSON.parse(addText) } await addItem({ analysisId: analysisId as any, sectionKey, item: parsed }) setAddText("") setIsAddOpen(false) } finally { setIsBusy(false) } } const handleReprompt = async () => { setIsBusy(true) try { const response = await fetch("/api/analysis/reprompt", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ analysisId, sectionKey, prompt: repromptText.trim() || undefined, }), }) if (!response.ok) { const data = await response.json() throw new Error(data.error || "Reprompt failed") } setRepromptText("") setIsRepromptOpen(false) } finally { setIsBusy(false) } } return ( {title}
{items.length === 0 ? (
No items yet.
) : ( items.map((item, index) => (
{summarizeItem(item)}
#{index + 1}
)) )}
Add to {title} Paste JSON for an item, or plain text for a string entry.