import { useSpellSlots } from '../api/queries'; import { slotLevelLabel } from '../lib/format'; import { InfoTip } from './InfoTip'; import type { SpellSlots as SpellSlotsInfo } from '../api/types'; // The slot tracker: a row of pips for each level of slot the character has, // filled while the slot is still there to spend. `canUse` is the character's // own player or the DM: "Use" spends one. Getting a slot back is the DM's // alone: for them each pip is a button that sets how many are left (clicking // the last filled one takes it off), and "All back" refills the lot. export function SpellSlotTracker({ characterId, name, slots, canUse, isDM, onError, }: { characterId: string; name: string; slots: SpellSlotsInfo; canUse: boolean; isDM: boolean; onError?: (err: unknown) => void; }) { const spellSlots = useSpellSlots(characterId); const send = (action: Parameters[0]) => spellSlots.mutate(action, { onError }); const anySpent = slots.levels.some((slot) => slot.used > 0); return (
Spell slots {isDM && anySpent && ( )}
{slots.levels.map((slot) => { const label = slotLevelLabel(slot.level); return (
{label} {Array.from({ length: slot.max }, (_, i) => i + 1).map((n) => { const className = `slot-pip${n <= slot.remaining ? ' is-filled' : ''}`; const left = n === slot.remaining ? n - 1 : n; return isDM ? ( )}
); })}

{slots.kind === 'PACT' ? 'Every spell cast with one of these is cast at that level. They all come back on a short or long rest.' : 'A higher slot can cast a lower spell. Cantrips need no slot. They all come back on a long rest.'} {canUse && !isDM && ' Once used, only the DM can give one back.'}

); }