import { useState, type ReactNode } from 'react'; import { useCampaignLocations, useDeleteMonster, useDeletePoolEntry, usePatchPoolEntry, useUpdateMonster } from '../../api/queries'; import { Modal } from '../Modal'; import { NumberInput } from '../NumberInput'; import { ErrorBanner } from '../Spinner'; import { EffectsEditor } from '../EffectsEditor'; import { WaresEditor } from '../Wares'; import { extractErrorMessage, fmtMod, spellLevelLabel, spellRequirementLabel } from '../../lib/format'; import { abilityLine, rateLabel, type EntryKind, type EntryRef } from '../../lib/campaignEntries'; import { Field, MonsterForm, ScalingSelect, SpellDamageSelect, SpellLandingSelect, StartLevelSelect } from './parts'; import { landingValue } from '../../lib/campaignEntries'; import type { GearEffect, Item, Monster, Npc, Spell, Weapon, ArmorType } from '../../api/types'; const KIND_NOUNS: Record = { weapon: 'weapon', item: 'item', spell: 'spell', monster: 'monster', npc: 'NPC' }; const ATTACK_ABILITY_LABELS: Record = { STR: 'STR', DEX: 'DEX', FINESSE: 'Finesse (the better of STR and DEX)' }; function Chip({ label, children }: { label: string; children: ReactNode }) { return (
{label} {children}
); } // The foot of every pop-out: taking the thing out of the campaign. function RemoveButton({ name, warning, pending, onRemove }: { name: string; warning: string; pending: boolean; onRemove: () => void }) { return (
); } // A weapon or an item: the two share levels, growth and effects. function GearDetail({ campaignId, pool, entry, added, onClose }: { added: ReactNode; campaignId: string; pool: 'weapons' | 'items'; entry: Weapon | Item; onClose: () => void }) { const patch = usePatchPoolEntry(campaignId, pool); const remove = useDeletePoolEntry(campaignId, pool); const [effects, setEffects] = useState(entry.effects ?? []); const [note, setNote] = useState(entry.effectNote ?? ''); const [effectsSaved, setEffectsSaved] = useState(false); const [error, setError] = useState(null); const onError = (err: unknown) => setError(extractErrorMessage(err)); const weapon = 'damageType' in entry ? entry : null; return ( {added} {error && }
{weapon ? ( <> {weapon.damageDice} {weapon.damageType} {ATTACK_ABILITY_LABELS[weapon.attackAbility]} {weapon.rangeNormal && ( {weapon.rangeNormal} {weapon.rangeLong ? `/${weapon.rangeLong}` : ''} ft )} {weapon.properties.length > 0 && {weapon.properties.join(', ')}} ) : ( entry.damageDice && {entry.damageDice} )}
{!weapon && 'description' in entry && entry.description &&

{entry.description}

}
{(id) => patch.mutate({ id: entry.id, minLevel }, { onError })} />} {entry.damageDice && ( {(id) => patch.mutate({ id: entry.id, damageScaling }, { onError })} />} )}

A character must be at least its level to use it. {entry.damageDice && ' The dice above are what it does at the level it starts at. When you level up a character\'s copy they grow one small step per level (1d4 → 1d6 → 1d8 → 1d10 → 1d12 → 2d6 → …): two steps for Powerful gear, three for Legendary.'}

{!weapon && 'armorType' in entry && (
{(id) => ( )}

Its AC is still set below, as +AC. This says what kind it is, for the class features that depend on what is worn: a barbarian’s and a monk’s Unarmored Defense and a draconic sorcerer’s scales stop while armor is equipped, a monk’s also behind a shield, and a barbarian’s extra speed stops in heavy armor.

)}
While equipped

These count for a character only while it is equipped and they are high enough level to use it. Armor is an item with +AC; a penalty is a negative number.

{ setEffects(nextEffects); setNote(nextNote); setEffectsSaved(false); }} />
{effectsSaved && Saved.}
remove.mutate(entry.id, { onError, onSuccess: onClose })} />
); } function SpellDetail({ campaignId, spell, added, onClose }: { added: ReactNode; campaignId: string; spell: Spell; onClose: () => void }) { const patch = usePatchPoolEntry(campaignId, 'spells'); const remove = useDeletePoolEntry(campaignId, 'spells'); const [error, setError] = useState(null); const onError = (err: unknown) => setError(extractErrorMessage(err)); return ( {added} {error && }
{spell.castingTime && {spell.castingTime}} {spell.range && {spell.range}} {spell.duration && {spell.duration}}

{spell.description || 'No description.'}

{(id) => patch.mutate({ id: spell.id, damageDice }, { onError })} />} {(id) => patch.mutate({ id: spell.id, ...landing }, { onError })} />}
remove.mutate(spell.id, { onError, onSuccess: onClose })} />
); } function MonsterDetail({ campaignId, monster, added, onClose }: { added: ReactNode; campaignId: string; monster: Monster; onClose: () => void }) { const update = useUpdateMonster(campaignId); const remove = useDeleteMonster(campaignId); const [editing, setEditing] = useState(false); const [error, setError] = useState(null); const onError = (err: unknown) => setError(extractErrorMessage(err)); return ( {added} {error && } {editing ? ( setEditing(false)} onSave={async (stats) => { setError(null); try { await update.mutateAsync({ id: monster.id, ...stats }); setEditing(false); } catch (err) { onError(err); throw err; } }} /> ) : ( <>
{monster.challengeRating || '—'} {monster.ac} {monster.maxHp} {monster.speed} {fmtMod(monster.attackBonus)} {monster.damageDice || '—'} {(monster.attacks ?? []).map((a, i) => ( 1 ? ` ×${a.count}` : ''}`}> {fmtMod(a.attackBonus)} · {a.damageDice || '—'} {a.damageType ? ` ${a.damageType}` : ''} ))}

{abilityLine(monster)}

{monster.specialTrait && (

Trait: {monster.specialTrait}

)} {monster.notes &&

{monster.notes}

} remove.mutate(monster.id, { onError, onSuccess: onClose })} /> )}
); } // An NPC: who they are, and what they trade — the rate they change gold and // cash at, and their wares. Players meet both on the encounter page, in a // quest the NPC is part of. function NpcDetail({ campaignId, npc, weapons, items, added, onClose }: { added: ReactNode; campaignId: string; npc: Npc; weapons: Weapon[]; items: Item[]; onClose: () => void }) { const patch = usePatchPoolEntry(campaignId, 'npcs'); const remove = useDeletePoolEntry(campaignId, 'npcs'); const { data: locations } = useCampaignLocations(campaignId); const [gold, setGold] = useState(npc.exchangeGold ? String(npc.exchangeGold) : ''); const [cash, setCash] = useState(npc.exchangeCash ? String(npc.exchangeCash) : ''); const [error, setError] = useState(null); const onError = (err: unknown) => setError(extractErrorMessage(err)); const rate = rateLabel(npc); const hasStats = npc.ac !== null || npc.maxHp !== null || npc.attackBonus !== null || Boolean(npc.damageDice); return ( {npc.role || 'NPC'} {npc.disposition} {rate && 💱 {rate}} } wide onClose={onClose} > {added} {error && } {npc.description ?

{npc.description}

:

No description for the players.

} {npc.dmNotes &&

🔒 {npc.dmNotes}

} {hasStats && (
{npc.ac !== null && {npc.ac}} {npc.maxHp !== null && {npc.maxHp}} {npc.attackBonus !== null && {fmtMod(npc.attackBonus)}} {npc.damageDice && {npc.damageDice}}
)}
Found at

Where the party meets {npc.name} in a free-roam scene: whoever goes to that place can talk and trade with them there. (In a quest, they are met when you add them to it.)

Changes money

Set a rate and the party can change gold into cash, or cash into gold, with {npc.name}. Silver and copper are not taken. Leave it empty for someone who does not change money.

setGold(e.target.value)} style={{ width: 90 }} /> gold = $ setCash(e.target.value)} style={{ width: 90 }} /> {rate && ( )}
For sale
remove.mutate(npc.id, { onError, onSuccess: onClose })} />
); } // The pop-out for whichever weapon, item, spell, monster or NPC is open. // Nothing shows if it is no longer in the campaign. export function EntryDetail({ campaignId, entry, weapons, items, spells, monsters, npcs, onAddAnother, onClose, }: { campaignId: string; entry: EntryRef; weapons: Weapon[]; items: Item[]; spells: Spell[]; monsters: Monster[]; npcs: Npc[]; // Given for something added by hand a moment ago: opens the form it came from again. onAddAnother?: () => void; onClose: () => void; }) { const added = onAddAnother && (
✓ Added to the campaign.
); const find = (list: T[]) => list.find((e) => e.id === entry.id); switch (entry.kind) { case 'weapon': { const weapon = find(weapons); return weapon ? : null; } case 'item': { const item = find(items); return item ? : null; } case 'spell': { const spell = find(spells); return spell ? : null; } case 'monster': { const monster = find(monsters); return monster ? : null; } case 'npc': { const npc = find(npcs); return npc ? : null; } } }