import { Link, useMatch } from 'react-router-dom'; import { useCampaign, useCampaignCharacters, usePatchCharacterWeapon, usePatchInventoryItem, useQuickStats } from '../api/queries'; import { useAuth } from '../auth/useAuth'; import { useLastCampaignId } from '../lib/lastCampaign'; import { closeCharacterPanel, openCharacterPanel, useCharacterPanelId } from '../lib/characterPanel'; import { CLASS_LABELS, SPECIES_LABELS, fmtMod, formatPurse, spellLevelLabel } from '../lib/format'; import { effectLabel, effectsSummary } from '../lib/gear'; import { speciesIcon } from '../lib/speciesIcons'; import { Spinner, ErrorBanner } from './Spinner'; import { HpBar } from './HpBar'; import { DeathSaves } from './DeathSaves'; import { useState } from 'react'; import { extractErrorMessage } from '../lib/format'; import type { ActiveGear } from '../api/types'; // What equipped gear is doing, one line per thing: "Chain Mail: +6 AC · −10 Speed". export function GearInForce({ gear }: { gear: ActiveGear }) { const sources = [...new Set([...gear.effects.map((e) => e.source), ...gear.notes.map((n) => n.source)])]; if (sources.length === 0) return null; return ( <> {sources.map((source) => (
{source} {[...gear.effects.filter((e) => e.source === source).map(effectLabel), ...gear.notes.filter((n) => n.source === source).map((n) => n.note)].join(' · ')}
))} ); } function Flags({ equipped, usable, level }: { equipped: boolean; usable: boolean; level: number }) { return ( <> {equipped && equipped} {!usable && needs level {level}} ); } // One character at a glance: the numbers for a fight, and everything they // hold. The pack, spells and purse are there for the character's own player // and the DM, who can also put gear on and take it off from here. function PanelBody({ characterId }: { characterId: string }) { const { data, isLoading, error } = useQuickStats(characterId, true); const patchWeapon = usePatchCharacterWeapon(characterId); const patchItem = usePatchInventoryItem(characterId); // Why something could not be equipped: too much on already. const [equipError, setEquipError] = useState(null); const equip = { onError: (err: unknown) => setEquipError(extractErrorMessage(err)), onSuccess: () => setEquipError(null) }; if (isLoading) return ; if (error || !data) return ; const canEquip = !data.restricted; const stats: [string, string | number][] = [ ['AC', data.armorClass], ['Initiative', fmtMod(data.initiative)], ['Speed', data.speed], ['Prof.', fmtMod(data.proficiencyBonus)], ['Passive Perc.', data.passivePerception], ]; return (

{data.name}

{data.species && SPECIES_LABELS[data.species]} {data.characterClass && CLASS_LABELS[data.characterClass]} · Level {data.level}
Full sheet
HP {data.currentHp}/{data.maxHp} {data.tempHp > 0 && ` +${data.tempHp}`} {stats.map(([label, value]) => ( {label} {value} ))}
{/* Read-only here; the saves are rolled on the sheet or in the encounter. */}
{(Object.keys(data.abilities) as (keyof typeof data.abilities)[]).map((a) => ( {a} {data.abilities[a].final} ({fmtMod(data.abilities[a].modifier)}) ))}
{(data.gear.effects.length > 0 || data.gear.notes.length > 0) && (
From equipped gear

Already counted in the numbers above.

)} {data.spellcasting && (
Spellcasting

Save DC {data.spellcasting.saveDC} · Attack {fmtMod(data.spellcasting.attackBonus)}

)} {canEquip && (

Tick what is equipped. Only equipped weapons can be used in an encounter, and only equipped gear gives its effects.

)} {equipError && }
Weapons · {data.weapons.length}
{data.weapons.length === 0 &&

No weapons.

} {data.weapons.map((w) => (
{w.name} {w.quantity > 1 && ×{w.quantity}}
Lv {w.level} · attack {fmtMod(w.attackBonus)} · {w.damageDice} {fmtMod(w.damageBonus)} {w.damageType}
{effectsSummary(w.effects, w.effectNote) &&
✦ {effectsSummary(w.effects, w.effectNote)}
}
{canEquip && ( patchWeapon.mutate({ weaponId: w.weaponId, equipped: e.target.checked }, equip)} /> )}
))}
{data.restricted ? (

Their pack, spells and purse are their own business.

) : ( <>
Inventory · {data.inventory.length}
{data.inventory.length === 0 &&

Nothing carried.

} {data.inventory.map((item) => (
{item.name} ×{item.quantity}
Lv {item.level} {item.damageDice && ` · ${item.damageDice}`}
{effectsSummary(item.effects, item.effectNote) &&
✦ {effectsSummary(item.effects, item.effectNote)}
} {item.description &&
{item.description}
}
patchItem.mutate({ itemId: item.itemId, equipped: e.target.checked }, equip)} />
))}
Money {data.purse && {formatPurse(data.purse)}}
{data.spells.length > 0 && (
Spells · {data.spells.length}
{data.spells.map((s) => (
{s.name} {' '} {spellLevelLabel(s.level)} {s.damageDice && ` · ${s.damageDice}`} {s.prepared && prepared} {!s.usable && needs level {s.level}} {s.description &&
{s.description}
}
))}
)} )} {data.specialAbilities && (
Special Abilities

{data.specialAbilities}

)}
); } // The floating character button, on every page, and the panel it opens. A // player gets their own characters in the campaign they are in; the DM gets // everyone's. Other pages can open the panel for a particular character with // openCharacterPanel (lib/characterPanel.ts). export function CharacterPanel() { const { user } = useAuth(); const campaignId = useLastCampaignId() ?? undefined; const { data: campaignData } = useCampaign(campaignId); const { data: characters } = useCampaignCharacters(campaignId); const onSheet = useMatch('/characters/:characterId'); const openId = useCharacterPanelId(); const isDM = campaignData?.campaign.role === 'DM'; const choices = (characters ?? []).filter((c) => c.buildStage === 'COMPLETE' && (isDM || c.userId === user?.id)); if (choices.length === 0 && !openId) return null; // On a character's own sheet the button opens that character; otherwise the first of yours. const fallback = choices.find((c) => c.id === onSheet?.params.characterId) ?? choices.find((c) => c.userId === user?.id) ?? choices[0]; const shown = openId ?? undefined; return ( <> {fallback && ( )} {shown && ( <>
{choices.length > 1 && choices.some((c) => c.id === shown) ? ( ) : (

Character

)}
)} ); }