import { useId, useState, type CSSProperties, type ReactNode } from 'react'; import { Link } from 'react-router-dom'; import { NumberInput } from '../NumberInput'; import { Pagination } from '../Paginated'; import { usePagination } from '../../hooks/usePagination'; import { SPELL_DAMAGE_GROUPS, SPELL_DAMAGE_SPECIALS } from '../../lib/format'; import { ABILITY_ORDER, DAMAGE_SCALING_LABELS, MAX_GEAR_LEVEL } from '../../lib/previewCalc'; import { matchesSearch, scoreField, type MonsterInput } from '../../lib/campaignEntries'; import type { Monster } from '../../api/types'; // A labelled field in a pop-out form. The child is handed the id its label points at. export function Field({ label, style, children }: { label: string; style?: CSSProperties; children: (id: string) => ReactNode }) { const id = useId(); return (
{children(id)}
); } // How fast a weapon's or item's dice grow as the DM levels a held copy up. export function ScalingSelect({ id, name, value, onChange }: { id?: string; name?: string; value?: number; onChange?: (scaling: number) => void }) { return ( ); } const GEAR_LEVELS = Array.from({ length: MAX_GEAR_LEVEL }, (_, i) => i + 1); // The level a weapon or item starts at. A held copy is never below it, and a // character must be at least this level to use it. export function StartLevelSelect({ id, name, value, onChange }: { id?: string; name?: string; value?: number; onChange?: (minLevel: number) => void }) { return ( ); } // A spell's damage is picked from a list rather than typed. A value that is // not in the list (set through the API, say) is still shown and kept. export function SpellDamageSelect({ id, name, spellName, value, onChange, }: { id?: string; name?: string; spellName?: string; value?: string | null; onChange?: (damageDice: string | null) => void; }) { const known = [...SPELL_DAMAGE_GROUPS.flatMap((g) => g.options), ...SPELL_DAMAGE_SPECIALS]; const others = value && !known.includes(value) ? [...SPELL_DAMAGE_SPECIALS, value] : SPELL_DAMAGE_SPECIALS; return ( ); } // The whole stat block, field for field as a printed one gives it. Adds a // monster, or edits one when `monster` is passed (its values fill the form). export function MonsterForm({ monster, saving, onSave, onCancel, }: { monster?: Monster; saving: boolean; // Rejects if the save failed, so the form keeps what was typed. onSave: (data: MonsterInput) => Promise; onCancel?: () => void; }) { const field = (name: string, label: string, input: ReactNode, style?: CSSProperties) => (
{input}
); const number = (name: keyof MonsterInput, label: string, min: number, max: number, fallback?: number) => field( name, label, ); return (
{ e.preventDefault(); const fd = new FormData(e.target as HTMLFormElement); const text = (key: string) => String(fd.get(key) || '').trim() || null; const num = (key: string) => Number(fd.get(key)); try { await onSave({ name: String(fd.get('name')).trim(), challengeRating: text('challengeRating'), ac: num('ac'), maxHp: num('maxHp'), speed: num('speed'), attackBonus: num('attackBonus'), damageDice: text('damageDice'), strScore: num('strScore'), dexScore: num('dexScore'), conScore: num('conScore'), intScore: num('intScore'), wisScore: num('wisScore'), chaScore: num('chaScore'), specialTrait: text('specialTrait'), notes: text('notes'), }); } catch { // Whoever opened the form shows the error; what was typed stays. } }} >
{field('name', 'Name', )} {field( 'challengeRating', 'CR (Challenge Rating)', )} {number('ac', 'AC (Armor Class)', 0, 50)} {number('maxHp', 'HP (Hit Points)', 1, 100000)} {number('speed', 'Speed (feet)', 0, 1000, 30)} {number('attackBonus', 'Attack bonus', -20, 50, 0)} {field( 'damageDice', 'Damage', )}
{ABILITY_ORDER.map((a) => number(scoreField(a), a, 1, 30, 10))}
{field( 'specialTrait', 'Special trait', , { marginTop: '0.5rem' } )} {field( 'notes', 'Notes',