import { useState } from 'react'; import { useApproveLevelUps } from '../api/queries'; import { classLabel, extractErrorMessage } from '../lib/format'; import { Modal } from './Modal'; import { ErrorBanner } from './Spinner'; import type { CharacterSummary, LevelUpApproval } from '../api/types'; const MAX_LEVEL = 20; // The DM's level-up button: approve a level for any of the party at once. // Each approval is a checklist waiting for its character: their player is // told and makes the picks themselves, so nobody queues for the DM. A // character without a player is the DM's to finish, from its sheet. // `trackXp`: with XP kept, those who have the XP for their next level start // ticked; without it (milestones) the DM ticks who levels. export function LevelUpBulkModal({ campaignId, characters, trackXp, onClose, }: { campaignId: string; characters: CharacterSummary[]; trackXp: boolean; onClose: () => void; }) { const approve = useApproveLevelUps(campaignId); const party = characters.filter((c) => c.buildStage === 'COMPLETE'); const canLevel = (c: CharacterSummary) => c.level + c.levelUpsPending < MAX_LEVEL; const [picked, setPicked] = useState>(() => new Set(party.filter((c) => trackXp && c.xpProgress.levelUpDue && canLevel(c)).map((c) => c.id))); const [error, setError] = useState(null); const [done, setDone] = useState(null); function toggle(id: string) { setPicked((current) => { const next = new Set(current); if (!next.delete(id)) next.add(id); return next; }); } async function send(characterIds: string[], revoke = false) { setError(null); try { const { results } = await approve.mutateAsync({ characterIds, revoke }); if (!revoke) setDone(results); } catch (err) { setError(extractErrorMessage(err)); } } if (done) { return ( {done.map((result) => (
{result.name} {result.skipped ? (
{result.skipped}
) : (
Level {result.level} → {result.level + 1} is theirs to take.{' '} {result.userId ? 'Their player has been told and chooses what they gain.' : 'No player: finish it from the sheet.'}
)}
))}
); } return ( {error && }
{trackXp && ( )}
{party.length === 0 &&

No finished characters yet.

} {party.map((c) => ( ))}
); }