import { useState } from 'react'; import Modal from '../ui/Modal'; import { weaponTemplates } from '../../data/weapon-templates'; import type { WeaponTemplate } from '../../data/weapon-templates'; interface Props { open: boolean; onClose: () => void; onCreate: (weapon: Record) => void; } interface FormState { name: string; type: string; damage: string; attackMod: string; range: string; ammoMax: string; notes: string; } function defaults(): FormState { return { name: '', type: '', damage: '', attackMod: '', range: '', ammoMax: '', notes: '' }; } function fromTemplate(t: WeaponTemplate): FormState { return { name: t.name, type: t.type, damage: t.damage, attackMod: '', range: t.range, ammoMax: '', notes: t.notes ?? '', }; } export default function AddWeaponModal({ open, onClose, onCreate }: Props) { const [templateIdx, setTemplateIdx] = useState(''); const [form, setForm] = useState(defaults()); const set = (field: keyof FormState) => (e: React.ChangeEvent) => { setForm((f) => ({ ...f, [field]: e.target.value })); }; const handleTemplateChange = (e: React.ChangeEvent) => { const val = e.target.value; setTemplateIdx(val); if (val === '') { setForm(defaults()); } else { setForm(fromTemplate(weaponTemplates[parseInt(val)])); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!form.name.trim()) return; const aMax = parseInt(form.ammoMax) || null; onCreate({ name: form.name.trim(), type: form.type || null, damage: form.damage.trim() || null, attack_mod: parseInt(form.attackMod) || null, range: form.range.trim() || null, ammo_max: aMax, ammo_current: aMax, status: 'ok', notes: form.notes.trim() || null, }); setForm(defaults()); setTemplateIdx(''); onClose(); }; // Group templates by type for the dropdown const mundane = weaponTemplates.map((t, i) => ({ t, i })).filter(({ t }) => t.type === 'mundane'); const spellcannons = weaponTemplates.map((t, i) => ({ t, i })).filter(({ t }) => t.type === 'spellcannon'); return (
); }