feat: add encounter roster visibility and kill actions

- persist independent Hide Disabled and Hide Dead encounter controls
- keep dead PCs exempt from Hide Dead while allowing Hide Disabled to hide any inactive participant
- add confirmed Kill action that sets zero HP, dead, and inactive without deleting
- add undoable Heal Full action above Revive
- fix random initiative reroll test flake
This commit is contained in:
david raistrick committed 2026-11-18 11:11:03 -05:00
1 parent 0e7bc7c099
commit 15347bfdc8
5 files changed
+300 -27

No files matched your search

+92 -10
View File
@@ -127,6 +127,7 @@ const {
deathSave: combatDeathSave,
stabilizeParticipant,
reviveParticipant,
healFullParticipant,
markDead,
toggleCondition: combatToggleCondition,
reorderParticipants,
@@ -1423,6 +1424,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
const [draggedItemId, setDraggedItemId] = useState(null);
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
const [itemToDelete, setItemToDelete] = useState(null);
const [killTarget, setKillTarget] = useState(null);
const [lastRollDetails, setLastRollDetails] = useState(null);
const [openConditionsId, setOpenConditionsId] = useState(null);
const [customConditionInput, setCustomConditionInput] = useState('');
@@ -1439,6 +1441,16 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
];
const participants = encounter.participants || [];
const isDeadParticipant = p => p.status === 'dead' || ((encounter.ruleset || '5e') === '5e' && p.type === 'monster' && p.currentHp === 0);
// Independent filters: Hide Disabled applies to every inactive participant,
// including dead PCs. Hide Dead applies only to dead monsters/NPCs.
const deadParticipants = participants.filter(p => isDeadParticipant(p) && p.type !== 'character');
const disabledParticipants = participants.filter(p => p.isActive === false);
const visibleParticipants = participants.filter(p => {
if (encounter.hideDisabledParticipants && p.isActive === false) return false;
if (encounter.hideDeadParticipants && isDeadParticipant(p) && p.type !== 'character') return false;
return true;
});
const [editingField, setEditingField] = useState(null);
const [editingId, setEditingId] = useState(null);
const blurTimeoutRef = useRef(null);
@@ -1739,6 +1751,15 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
}
};
const handleHealFull = async (participantId) => {
if (!db) return;
try {
await healFullParticipant(encounter, participantId, buildCtx(encounterPath));
} catch (err) {
showToast(err.message);
}
};
const handleMarkDead = async (participantId) => {
if (!db) return;
try {
@@ -1748,6 +1769,20 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
}
};
const confirmKill = async () => {
if (!killTarget) return;
await handleMarkDead(killTarget.id);
setKillTarget(null);
};
const toggleParticipantVisibility = async (field) => {
try {
await storage.updateDoc(encounterPath, { [field]: !encounter[field] });
} catch (err) {
showToast('Failed to update participant visibility.');
}
};
const handleCritDamage = async (participantId) => {
if (!db) return;
try {
@@ -1890,8 +1925,9 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
setDraggedItemId(null);
};
// 1-list model: participants[] IS the display order. No re-sort.
const sortedParticipants = participants;
// 1-list model: participants[] IS display order. Visibility filters only render;
// encounter data/order remain untouched.
const sortedParticipants = visibleParticipants;
const initiativeGroups = participants.reduce((acc, p) => {
acc[p.initiative] = (acc[p.initiative] || 0) + 1;
@@ -2138,7 +2174,28 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
</div>
)}
<div className="mb-3 flex flex-wrap items-center gap-2 rounded-md border border-stone-700 bg-stone-950/60 p-2">
<span className="text-xs font-semibold uppercase tracking-wide text-stone-400">Roster visibility</span>
<button
type="button"
onClick={() => toggleParticipantVisibility('hideDisabledParticipants')}
aria-pressed={!!encounter.hideDisabledParticipants}
className={`px-2 py-1 rounded text-xs border ${encounter.hideDisabledParticipants ? 'bg-amber-800 border-amber-500 text-white' : 'bg-stone-800 border-stone-600 text-stone-300'}`}
>
{encounter.hideDisabledParticipants ? 'Show' : 'Hide'} disabled ({disabledParticipants.length})
</button>
<button
type="button"
onClick={() => toggleParticipantVisibility('hideDeadParticipants')}
aria-pressed={!!encounter.hideDeadParticipants}
className={`px-2 py-1 rounded text-xs border ${encounter.hideDeadParticipants ? 'bg-amber-800 border-amber-500 text-white' : 'bg-stone-800 border-stone-600 text-stone-300'}`}
>
{encounter.hideDeadParticipants ? 'Show' : 'Hide'} dead ({deadParticipants.length})
</button>
</div>
{participants.length === 0 && <p className="text-sm text-stone-400">No participants added yet.</p>}
{participants.length > 0 && visibleParticipants.length === 0 && <p className="text-sm text-stone-400 mb-2">All participants hidden.</p>}
<ul className="space-y-2">
@@ -2272,15 +2329,21 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
</span>
<span className="ml-auto inline-flex items-center gap-1">
{participantStatus === 'dead' && (
<button onClick={() => handleRevive(p.id)} className="inline-flex items-center gap-1 px-2 py-0.5 text-xs rounded bg-emerald-800 hover:bg-emerald-700 text-white" title="Revive (clear dead status)">
<HeartPulse size={12} /> Revive
</button>
)}
{isGeneric && !isDead && (
<button onClick={() => handleMarkDead(p.id)} className="inline-flex items-center gap-1 px-2 py-0.5 text-xs rounded bg-red-900 hover:bg-red-800 text-white" title="Mark dead">
<HeartCrack size={12} /> Mark Dead
</button>
<span className="inline-flex flex-col items-stretch gap-1">
<button
onClick={() => handleHealFull(p.id)}
className="inline-flex items-center justify-center gap-1 px-2 py-0.5 text-xs rounded bg-green-700 hover:bg-green-600 text-white"
title="Restore Max HP, consciousness, and active status"
aria-label={`Heal ${p.name} to full`}
>
<HeartPulse size={12} /> Heal Full
</button>
<button onClick={() => handleRevive(p.id)} className="inline-flex items-center justify-center gap-1 px-2 py-0.5 text-xs rounded bg-emerald-800 hover:bg-emerald-700 text-white" title="Revive at 0 HP">
<HeartPulse size={12} /> Revive
</button>
</span>
)}
</span>
</div>
@@ -2496,6 +2559,17 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
</button>
</div>
)}
{!isDead && (
<button
type="button"
onClick={() => setKillTarget({ id: p.id, name: p.name })}
className="p-1 rounded transition-colors text-red-200 bg-red-950 hover:bg-red-900 border border-red-700"
title={`Kill ${p.name}: set 0 HP, dead, and inactive`}
aria-label={`Kill ${p.name}`}
>
<HeartCrack size={18} />
</button>
)}
<button
onClick={() => toggleParticipantActive(p.id)}
className={`p-1 rounded transition-all ring-2 ${p.isActive ? 'bg-emerald-700 hover:bg-emerald-600 text-white ring-emerald-300' : 'bg-red-900 hover:bg-red-800 text-red-100 ring-red-500'}`}
@@ -2566,6 +2640,14 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
/>
)}
<ConfirmationModal
isOpen={!!killTarget}
onClose={() => setKillTarget(null)}
onConfirm={confirmKill}
title={`Kill ${killTarget?.name || 'participant'}?`}
message="Sets HP and Temp HP to 0, marks dead, and disables participant. Participant remains in encounter and can be shown or revived later."
/>
<ConfirmationModal
isOpen={showDeleteConfirm}
onClose={() => setShowDeleteConfirm(false)}
@@ -0,0 +1,107 @@
import { screen, fireEvent, waitFor, within } from '@testing-library/react';
import '@testing-library/jest-dom';
import {
setupReady, addMonsterViaUI, startCombatViaUI, MOCK_DB,
} from './testHelpers';
function encounterRow() {
const row = [...MOCK_DB._state.docs.entries()].find(([path]) => path.includes('/encounters/'));
return row ? { path: row[0], doc: MOCK_DB.get(row[0]) } : null;
}
function participant(name) {
return encounterRow().doc.participants.find(p => p.name === name);
}
function card(name) {
return screen.getByTestId(`admin-participant-card-${participant(name).id}`);
}
describe('encounter participant visibility and kill', () => {
test('Kill confirms, sets 0 HP/dead/inactive, and does not delete participant', async () => {
await setupReady('Kill Camp', 'Kill Encounter');
await addMonsterViaUI('Ogre', 59, 1);
fireEvent.click(within(card('Ogre')).getByRole('button', { name: 'Kill Ogre' }));
expect(await screen.findByText('Kill Ogre?')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /^Cancel$/ }));
expect(participant('Ogre')).toMatchObject({ currentHp: 59, isActive: true });
fireEvent.click(within(card('Ogre')).getByRole('button', { name: 'Kill Ogre' }));
fireEvent.click(await screen.findByRole('button', { name: /^Confirm$/ }));
await waitFor(() => expect(participant('Ogre')).toMatchObject({ currentHp: 0, tempHp: 0, status: 'dead', isActive: false }));
expect(encounterRow().doc.participants).toHaveLength(1);
const deadCard = card('Ogre');
const healFull = within(deadCard).getByRole('button', { name: 'Heal Ogre to full' });
const revive = within(deadCard).getByRole('button', { name: /Revive/i });
expect(healFull.compareDocumentPosition(revive) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
fireEvent.click(healFull);
await waitFor(() => expect(participant('Ogre')).toMatchObject({ currentHp: 59, status: 'conscious', isActive: true }));
});
test('Hide dead never hides player characters', async () => {
await setupReady('PC Visibility Camp', 'PC Visibility Encounter');
await addMonsterViaUI('Dead Monster', 10, 1);
fireEvent.change(screen.getByLabelText('Name', { selector: '#characterName' }), { target: { value: 'Dead Hero' } });
fireEvent.click(screen.getByRole('button', { name: 'Add Character' }));
await screen.findByText('Dead Hero');
fireEvent.click(screen.getByRole('button', { name: /Add All \(Roll Init\)/i }));
await waitFor(() => expect(participant('Dead Hero')).toBeDefined());
for (const name of ['Dead Monster', 'Dead Hero']) {
fireEvent.click(within(card(name)).getByRole('button', { name: `Kill ${name}` }));
fireEvent.click(await screen.findByRole('button', { name: /^Confirm$/ }));
await waitFor(() => expect(participant(name).status).toBe('dead'));
await waitFor(() => expect(screen.queryByText(`Kill ${name}?`)).not.toBeInTheDocument());
}
expect(screen.getByRole('button', { name: /Hide dead \(1\)/i })).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Hide dead \(1\)/i }));
await waitFor(() => expect(screen.queryByText('Dead Monster')).not.toBeInTheDocument());
expect(screen.getByTestId(`admin-participant-card-${participant('Dead Hero').id}`)).toBeInTheDocument();
});
test('Hide disabled and Hide dead are independent, persisted, and unchanged by pause', async () => {
await setupReady('Visibility Camp', 'Visibility Encounter');
await addMonsterViaUI('Alive', 10, 3);
await addMonsterViaUI('Disabled', 10, 2);
await addMonsterViaUI('Dead', 10, 1);
fireEvent.click(within(card('Disabled')).getByRole('button', { name: 'Disable participant' }));
await waitFor(() => expect(participant('Disabled').isActive).toBe(false));
fireEvent.click(within(card('Dead')).getByRole('button', { name: 'Kill Dead' }));
fireEvent.click(await screen.findByRole('button', { name: /^Confirm$/ }));
await waitFor(() => expect(participant('Dead').status).toBe('dead'));
// Disabled count includes dead+inactive. Filters overlap independently.
fireEvent.click(screen.getByRole('button', { name: /Hide disabled \(2\)/i }));
await waitFor(() => expect(encounterRow().doc.hideDisabledParticipants).toBe(true));
expect(screen.queryByText('Disabled')).not.toBeInTheDocument();
expect(screen.queryByText('Dead')).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Show disabled \(2\)/i }));
await waitFor(() => expect(screen.getByText('Disabled')).toBeInTheDocument());
expect(screen.getByText('Dead')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Hide dead \(1\)/i }));
await waitFor(() => expect(encounterRow().doc.hideDeadParticipants).toBe(true));
expect(screen.queryByText('Dead')).not.toBeInTheDocument();
expect(screen.getByText('Disabled')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Hide disabled \(2\)/i }));
await waitFor(() => expect(encounterRow().doc.hideDisabledParticipants).toBe(true));
await startCombatViaUI();
expect(screen.queryByText('Disabled')).not.toBeInTheDocument();
expect(screen.queryByText('Dead')).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Pause Combat/i }));
expect(screen.queryByText('Disabled')).not.toBeInTheDocument();
expect(screen.queryByText('Dead')).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Show disabled \(2\)/i }));
await waitFor(() => expect(screen.getByText('Disabled')).toBeInTheDocument());
expect(screen.queryByText('Dead')).not.toBeInTheDocument();
expect(encounterRow().doc.hideDeadParticipants).toBe(true);
});
});