"use client"; import { useCallback, useMemo, useState } from "react"; import { Sparkles, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { ItemImageCapture, ProcessedImage } from "./ItemImageCapture"; interface ItemAiAssistantProps { locale: string; categories: string[]; serverOptions: string[]; onGenerated: (result: { json: unknown; prefill: ItemAiPrefill }) => void; } export interface ItemAiPrefill { name?: string; description?: string; category?: string; rarity?: string | null; servers?: string[] | string; tags?: string[] | string; metadata?: Record; } export function ItemAiAssistant({ locale, categories, serverOptions, onGenerated, }: ItemAiAssistantProps) { const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [scope, setScope] = useState<"global" | "server">("global"); const [server, setServer] = useState(serverOptions[0] ?? ""); const [images, setImages] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [rawJson, setRawJson] = useState(null); const disabled = useMemo(() => !name.trim() || !description.trim(), [name, description]); const handleGenerate = useCallback(async () => { if (disabled || loading) return; setLoading(true); setError(null); try { const response = await fetch("/api/admin/items/analyze", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ locale, name, description, scope, server: scope === "server" ? server : null, categories, servers: serverOptions, images: images.map((image) => image.dataUrl), }), }); if (!response.ok) { const data = await response.json().catch(() => null); throw new Error(data?.error || "Falha na análise com IA."); } const data = await response.json(); const structure = data?.structure ?? data; const columns = structure?.columns; setRawJson(JSON.stringify(structure, null, 2)); const prefill: ItemAiPrefill = { name: typeof columns?.name === "string" ? columns.name : undefined, description: typeof columns?.description === "string" ? columns.description : undefined, metadata: typeof columns === "object" && columns !== null ? columns : undefined, }; if (columns) { if (typeof columns.category === "string") { prefill.category = columns.category; } if (Array.isArray(columns.servers)) { prefill.servers = columns.servers as string[]; } if (typeof columns.servers === "string") { prefill.servers = columns.servers; } if (Array.isArray(columns.tags)) { prefill.tags = columns.tags as string[]; } if (typeof columns.tags === "string") { prefill.tags = columns.tags; } if (columns.rarity && typeof columns.rarity === "string") { prefill.rarity = columns.rarity; } } onGenerated({ json: structure, prefill }); } catch (err) { console.error(err); setError(err instanceof Error ? err.message : "Erro ao processar análise de IA."); } finally { setLoading(false); } }, [ categories, description, disabled, images, locale, loading, name, onGenerated, scope, server, serverOptions, ]); return (

Assistente IA para registro de item

Cole prints e descreva o item. A IA vai sugerir a tabela, campos e metadados. Você pode ajustar os dados antes de salvar.

Até 2 imagens · Compressão automática · Suporte a Ctrl+V
setName(event.target.value)} placeholder="Ex.: Final Fate" />
{scope === "server" && (
)}