import { useState } from "react"; import type { CampaignId, Npc } from "@/lib/types"; import { pb } from "@/lib/pocketbase"; /** * Renders a form to add a new npc. Calls onCreate with the new npc document. */ export const NpcForm = ({ campaign, onCreate, }: { campaign: CampaignId; onCreate: (npc: Npc) => Promise; }) => { const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [adding, setAdding] = useState(false); const [error, setError] = useState(null); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!name.trim()) return; setAdding(true); setError(null); try { const npcDoc: Npc = await pb.collection("documents").create({ campaign, data: { npc: { name, description, }, }, }); setName(""); setDescription(""); await onCreate(npcDoc); } catch (e: any) { setError(e?.message || "Failed to add npc."); } finally { setAdding(false); } } return (

Create new npc

setName(e.target.value)} disabled={adding} aria-label="Name" />