Files
the-sunken-reach/frontend/src/components/LevelUpBulkModal.tsx
T
joshuaandClaude Opus 5.5 c3f2b0dfa7 V1.25.0: the DM approves a level, each player takes their own
- "Level up..." beside Rest approves a level for any of the party at once;
  with XP tracked, those who have the XP start ticked
- Each player is told with a notice and picks for themselves in the checklist:
  class (multiclass), subclass, Boon, spells within the level's allowance,
  the weapon upgrade; a waiting level shows on the sheet and campaign page
- The DM sees who still has picks to make, and can finish or take back any
- Hit points on a level up can be rolled (campaign setting): by the site, or
  at the table and typed in with physical dice
- Toasts: notices that slide in over any page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 08:30:31 +02:00

136 lines
5.4 KiB
TypeScript

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<Set<string>>(() => new Set(party.filter((c) => trackXp && c.xpProgress.levelUpDue && canLevel(c)).map((c) => c.id)));
const [error, setError] = useState<string | null>(null);
const [done, setDone] = useState<LevelUpApproval[] | null>(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 (
<Modal title="Level ups approved" subtitle="Each player makes their own picks" onClose={onClose}>
{done.map((result) => (
<div key={result.characterId} className="reward-row">
<strong>{result.name}</strong>
{result.skipped ? (
<div className="combatant-sub">{result.skipped}</div>
) : (
<div className="combatant-sub">
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.'}
</div>
)}
</div>
))}
<div className="modal-foot">
<button className="btn btn-primary" onClick={onClose}>
Done
</button>
</div>
</Modal>
);
}
return (
<Modal title="Level up" subtitle="Approve a level for the party; each player then picks what they gain" wide onClose={onClose}>
{error && <ErrorBanner message={error} />}
<div className="flex-row flex-wrap gap-sm">
<button className="btn" onClick={() => setPicked(new Set(party.filter(canLevel).map((c) => c.id)))}>
Everyone
</button>
{trackXp && (
<button className="btn" onClick={() => setPicked(new Set(party.filter((c) => c.xpProgress.levelUpDue && canLevel(c)).map((c) => c.id)))}>
Those with the XP
</button>
)}
<button className="btn btn-ghost" onClick={() => setPicked(new Set())}>
Nobody
</button>
</div>
<div className="mt-md">
{party.length === 0 && <p className="combatant-sub">No finished characters yet.</p>}
{party.map((c) => (
<label key={c.id} className="checkbox-row level-choice" style={canLevel(c) ? undefined : { opacity: 0.55 }}>
<input type="checkbox" checked={picked.has(c.id)} disabled={!canLevel(c)} onChange={() => toggle(c.id)} />
<span style={{ flex: 1 }}>
<strong>{c.name}</strong>{' '}
<span className="combatant-sub">
{classLabel(c.characterClass, c.subclassName, c.classes)} · Level {c.level}
</span>
{trackXp && c.xpProgress.levelUpDue && <span className="badge badge-live" style={{ marginLeft: 6 }}>has the XP</span>}
{c.levelUpsPending > 0 && (
<span className="badge" style={{ marginLeft: 6 }}>
{c.levelUpsPending} waiting on their picks
</span>
)}
{!c.userId && <span className="badge" style={{ marginLeft: 6 }}>no player: yours to finish</span>}
</span>
{c.levelUpsPending > 0 && (
<button
type="button"
className="btn btn-ghost btn-sm"
title="Takes back one approved level that has not been taken yet"
disabled={approve.isPending}
onClick={(e) => {
e.preventDefault();
send([c.id], true);
}}
>
Take one back
</button>
)}
</label>
))}
</div>
<div className="modal-foot">
<button className="btn btn-primary" disabled={picked.size === 0 || approve.isPending} onClick={() => send([...picked])}>
{approve.isPending ? 'Approving…' : `Approve a level for ${picked.size} character${picked.size === 1 ? '' : 's'}`}
</button>
</div>
</Modal>
);
}