import { useState } from 'react'; import { useRest } from '../api/queries'; import { CLASS_LABELS, extractErrorMessage, fmtMod, slotsSummary } from '../lib/format'; import { Modal } from './Modal'; import { NumberInput } from './NumberInput'; import { ErrorBanner } from './Spinner'; import { LifeBadge } from './DeathSaves'; import type { CharacterSummary, RestResult, RestType } from '../api/types'; const REST_LABELS: Record = { SHORT: 'Short rest', LONG: 'Long rest' }; // What a rest did for one character, in a line or two. function ResultRow({ result, type }: { result: RestResult; type: RestType }) { if (result.skipped) { return (
{result.name}
{result.skipped}
); } const { hitDice } = result; const lines: string[] = []; if (hitDice.spent > 0) { const dice = `${hitDice.spent}d${hitDice.size}`; const rolled = hitDice.rolls ? `the site rolled ${hitDice.rolls.join(' + ')}` : `rolled ${hitDice.total}`; const con = hitDice.conBonus === 0 ? '' : ` ${hitDice.conBonus > 0 ? '+' : '−'} ${Math.abs(hitDice.conBonus)} Constitution`; lines.push(`Spent ${dice}: ${rolled}${con}, healing ${hitDice.healed}.`); } if (hitDice.regained > 0) lines.push(`${hitDice.regained} Hit ${hitDice.regained === 1 ? 'Die' : 'Dice'} back.`); lines.push(`Hit Dice left: ${hitDice.left} of ${hitDice.of}.`); if (result.slotsRestored) lines.push('All spell slots back.'); if (type === 'SHORT' && hitDice.spent === 0 && !result.slotsRestored) lines.unshift('Rested, with no Hit Dice spent.'); return (
{result.name} HP {result.hpBefore === result.hpAfter ? result.hpAfter : `${result.hpBefore} → ${result.hpAfter}`}/{result.maxHp} {result.tempHp > 0 && ` +${result.tempHp}`}
{lines.join(' ')}
{result.notes.map((note) => (
✦ {note}
))}
); } // The DM's rest button: pick who rests and for how long. A long rest is // worked out by the site. A short rest says which Hit Dice each character can // roll; the DM types how many they spend and what they rolled (or leaves the // roll to the site), and the healing is added up from that. export function RestModal({ campaignId, characters, onClose }: { campaignId: string; characters: CharacterSummary[]; onClose: () => void }) { const rest = useRest(campaignId); const party = characters.filter((c) => c.buildStage === 'COMPLETE'); const [type, setType] = useState('SHORT'); // The dead are left out to begin with: a rest does nothing for them. const [picked, setPicked] = useState>(() => new Set(party.filter((c) => c.deathSaves.state !== 'DEAD').map((c) => c.id))); const [dice, setDice] = useState>({}); const [rolled, setRolled] = useState>({}); const [error, setError] = useState(null); const [done, setDone] = useState<{ type: RestType; results: RestResult[] } | null>(null); function toggle(id: string) { setPicked((current) => { const next = new Set(current); if (!next.delete(id)) next.add(id); return next; }); } async function handleRest() { setError(null); try { const resting = party.filter((c) => picked.has(c.id)); setDone( await rest.mutateAsync({ type, characters: resting.map((c) => { const spent = type === 'SHORT' ? Number(dice[c.id]) || 0 : 0; return { characterId: c.id, hitDice: spent, rolled: spent > 0 && rolled[c.id] ? Number(rolled[c.id]) : null }; }), }) ); } catch (err) { setError(extractErrorMessage(err)); } } if (done) { return ( {done.results.map((result) => ( ))} {done.type === 'SHORT' && (

Also back on a short rest, for the players to note: a fighter's Second Wind and Action Surge, a monk's ki, a cleric's Channel Divinity, a druid's Wild Shape.

)} {done.type === 'LONG' && (

"Per day" abilities are back too. One long rest per 24 hours.

)}
); } return (
{(['SHORT', 'LONG'] as const).map((option) => ( ))}

{type === 'SHORT' ? 'Each character can spend Hit Dice to heal: they roll each die and add their Constitution modifier. Type how many dice they spend and what the dice added up to, or leave the total empty and the site rolls them. A warlock gets every spell slot back.' : 'Everyone resting goes to full HP, gets every spell slot back, and regains spent Hit Dice up to half their level (at least one). Temp HP is gone, except what the Hardy Boon gives.'}

{error && }

Who rests ({party.filter((c) => picked.has(c.id)).length} of {party.length})

{party.length === 0 &&

No finished characters in this campaign yet.

} {party.map((c) => { const isPicked = picked.has(c.id); const hitDice = c.hitDice; const spent = Math.min(Number(dice[c.id]) || 0, hitDice?.remaining ?? 0); const slots = slotsSummary(c.spellSlots); return (
{type === 'SHORT' && isPicked && hitDice && (
{hitDice.remaining === 0 ? ( No Hit Dice left to spend until a long rest. ) : ( <> {spent > 0 && ( )} )}
)}
); })}
); }