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 { landingFrom, matchesSearch, scoreField, type MonsterInput, type SpellLanding } from '../../lib/campaignEntries'; import type { Ability, Monster, MonsterAttack } 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)}
); } const LANDING_ABILITIES: Ability[] = ['STR', 'DEX', 'CON', 'INT', 'WIS', 'CHA']; const ABILITY_NAMES: Record = { STR: 'Strength', DEX: 'Dexterity', CON: 'Constitution', INT: 'Intelligence', WIS: 'Wisdom', CHA: 'Charisma' }; // How a spell lands in a battle, as one choice (lib/campaignEntries.ts, SpellLanding). export function SpellLandingSelect({ id, name, value, onChange }: { id?: string; name?: string; value?: string; onChange?: (landing: SpellLanding) => void }) { return ( ); } // 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; }) { // Named attacks, as typed: kept as text until saved. const [attacks, setAttacks] = useState<{ name: string; attackBonus: string; damageDice: string; damageType: string; count: string }[]>(() => (monster?.attacks ?? []).map((a) => ({ name: a.name, attackBonus: String(a.attackBonus), damageDice: a.damageDice ?? '', damageType: a.damageType ?? '', count: String(a.count ?? 1), })) ); const setAttack = (index: number, change: Partial<(typeof attacks)[number]>) => setAttacks((rows) => rows.map((row, i) => (i === index ? { ...row, ...change } : row))); 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'), attacks: attacks .filter((a) => a.name.trim()) .map( (a): MonsterAttack => ({ name: a.name.trim(), attackBonus: Math.trunc(Number(a.attackBonus)) || 0, damageDice: a.damageDice.trim() || null, damageType: a.damageType.trim() || null, count: Math.min(10, Math.max(1, Math.trunc(Number(a.count)) || 1)), }) ), }); } 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))}

For a creature with more than the one attack above: each with its own bonus and damage, and how many times it is made each turn (a multiattack). With any listed here, these are what it attacks with.

{attacks.map((a, i) => (
setAttack(i, { name: e.target.value })} style={{ flex: '2 1 110px' }} /> setAttack(i, { attackBonus: e.target.value })} style={{ width: 70 }} /> setAttack(i, { damageDice: e.target.value })} style={{ flex: '1 1 80px' }} /> setAttack(i, { damageType: e.target.value })} style={{ flex: '1 1 80px' }} /> setAttack(i, { count: e.target.value })} style={{ width: 60 }} />
))} {attacks.length < 8 && ( )}
{field( 'specialTrait', 'Special trait', , { marginTop: '0.5rem' } )} {field( 'notes', 'Notes',