import { useState } from 'react'; import { useCharacterState, useResources } from '../api/queries'; import { CONDITIONS, MAX_EXHAUSTION, conditionRule, exhaustionRule } from '../lib/conditions'; import { NumberInput } from './NumberInput'; import type { ClassResource } from '../api/types'; // A counter with more uses than this is a pool (Lay on Hands, a high-level // monk's Ki): it gets a number to spend in place of a row of pips. const MOST_PIPS = 10; // One condition, with its rule a hover away. `onRemove` adds the ✕. export function ConditionChip({ name, note, onRemove }: { name: string; note?: string; onRemove?: () => void }) { return ( {name} {note && {note}} {onRemove && ( )} ); } // The "+ Condition" menu: every condition not already in force. export function ConditionPicker({ who, current, onAdd }: { who: string; current: string[]; onAdd: (condition: string) => void }) { return ( ); } // Inspiration and exhaustion as badges, for places that only show them. export function StateBadges({ inspiration, exhaustion }: { inspiration: boolean; exhaustion: number }) { return ( <> {inspiration && ( ✨ Inspired )} {exhaustion > 0 && ( Exhaustion {exhaustion} )} ); } // The class counters (Rage, Ki, Channel Divinity...): pips that are filled // while a use is left, and "Use" to spend one. `canUse` is the character's own // player or the DM. Getting a use back is the DM's alone: for them each pip // sets how many are left, and "All back" refills the lot. export function ResourceTracker({ characterId, name, resources, canUse, isDM, onError, }: { characterId: string; name: string; resources: ClassResource[]; canUse: boolean; isDM: boolean; onError?: (err: unknown) => void; }) { const mutation = useResources(characterId); const send = (action: Parameters[0]) => mutation.mutate(action, { onError }); // What is typed into a pool's box, by counter. const [amounts, setAmounts] = useState>({}); if (resources.length === 0) return null; return (
{isDM && resources.some((r) => r.used > 0) && (
)} {resources.map((r) => { const rest = r.restoresOn === 'SHORT' ? 'short or long rest' : 'long rest'; const isPool = r.max > MOST_PIPS; const amount = Math.max(1, Math.floor(Number(amounts[r.key])) || 1); return (
{r.name} {r.restoresOn === 'SHORT' ? 'short rest' : 'long rest'} {isPool ? ( ) : ( {Array.from({ length: r.max }, (_, i) => i + 1).map((n) => { const className = `slot-pip${n <= r.remaining ? ' is-filled' : ''}`; const left = n === r.remaining ? n - 1 : n; return isDM ? ( )} {isDM && isPool && ( )}
); })} {canUse && !isDM &&

Once used, a rest or the DM gives it back.

}
); } // How a character is doing beyond hit points: inspiration, exhaustion, // conditions and class counters. The DM gives and takes away all of it. The // character's own player spends inspiration and counters. Everyone else reads. // With `conditions` left out that row is not drawn (an encounter card has its // own). export function CharacterState({ characterId, name, inspiration, exhaustion, conditions, resources, canUse, isDM, onError, }: { characterId: string; name: string; inspiration: boolean; exhaustion: number; conditions?: string[]; resources: ClassResource[]; canUse: boolean; isDM: boolean; onError?: (err: unknown) => void; }) { const state = useCharacterState(characterId); const set = (data: Parameters[0]) => state.mutate(data, { onError }); return (
{isDM ? ( ) : inspiration ? ( <> {canUse && ( )} ) : ( No inspiration )} {isDM ? ( ) : ( )}
{exhaustion > 0 &&

{exhaustionRule(exhaustion)}

} {conditions && (conditions.length > 0 || isDM) && (
{conditions.map((c) => ( set({ conditions: conditions.filter((x) => x !== c) }) : undefined} /> ))} {isDM && set({ conditions: [...conditions, c] })} />}
)}
); }