feat: encounter UX, tracked effects, and character state improvements #10
No files matched your search
+110
-5
@@ -9,7 +9,7 @@ import {
|
|||||||
Play as PlayIcon, Pause as PauseIcon, SkipForward as SkipForwardIcon,
|
Play as PlayIcon, Pause as PauseIcon, SkipForward as SkipForwardIcon,
|
||||||
StopCircle as StopCircleIcon, Users2, Dices, ChevronDown, ScrollText,
|
StopCircle as StopCircleIcon, Users2, Dices, ChevronDown, ScrollText,
|
||||||
Maximize2, Minimize2, PanelTopOpen, PanelTopClose, Moon, Coffee, Clock, ChevronRight, X,
|
Maximize2, Minimize2, PanelTopOpen, PanelTopClose, Moon, Coffee, Clock, ChevronRight, X,
|
||||||
Undo2, Redo2, Crosshair
|
Undo2, Redo2, Crosshair, Pencil
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
|
|
||||||
// ----- UI feedback: toast (transient) + info modal (persistent) -----
|
// ----- UI feedback: toast (transient) + info modal (persistent) -----
|
||||||
@@ -2428,6 +2428,32 @@ function EncounterManager({ campaignId, initialActiveEncounterId, campaignCharac
|
|||||||
});
|
});
|
||||||
const [showCreateModal, setShowCreateModal] = useState(false);
|
const [showCreateModal, setShowCreateModal] = useState(false);
|
||||||
const [encounterFullscreen, setEncounterFullscreen] = useState(false);
|
const [encounterFullscreen, setEncounterFullscreen] = useState(false);
|
||||||
|
const [completedOpen, setCompletedOpen] = useState(false);
|
||||||
|
const [renamingId, setRenamingId] = useState(null);
|
||||||
|
const [renameValue, setRenameValue] = useState('');
|
||||||
|
|
||||||
|
const startRename = (encounter) => {
|
||||||
|
setRenamingId(encounter.id);
|
||||||
|
setRenameValue(encounter.name);
|
||||||
|
};
|
||||||
|
|
||||||
|
const cancelRename = () => {
|
||||||
|
setRenamingId(null);
|
||||||
|
setRenameValue('');
|
||||||
|
};
|
||||||
|
|
||||||
|
const commitRename = async (encounterId) => {
|
||||||
|
const trimmed = renameValue.trim();
|
||||||
|
if (trimmed && db && campaignId) {
|
||||||
|
try {
|
||||||
|
await storage.updateDoc(`${getPath.campaigns()}/${campaignId}/encounters/${encounterId}`, { name: trimmed });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error renaming encounter:', err);
|
||||||
|
showToast('Failed to rename encounter. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cancelRename();
|
||||||
|
};
|
||||||
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
|
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
|
||||||
const [itemToDelete, setItemToDelete] = useState(null);
|
const [itemToDelete, setItemToDelete] = useState(null);
|
||||||
const [draggedEncounterId, setDraggedEncounterId] = useState(null);
|
const [draggedEncounterId, setDraggedEncounterId] = useState(null);
|
||||||
@@ -2643,8 +2669,12 @@ function EncounterManager({ campaignId, initialActiveEncounterId, campaignCharac
|
|||||||
<p className="text-sm text-stone-400">No encounters yet.</p>
|
<p className="text-sm text-stone-400">No encounters yet.</p>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="space-y-3">
|
{/* Encounter groups: In Progress / Ready / Completed. */}
|
||||||
{sortedEncounters.map(encounter => {
|
{(() => {
|
||||||
|
const inProgress = sortedEncounters.filter(e => e.isStarted && !e.endedAt);
|
||||||
|
const completed = sortedEncounters.filter(e => e.endedAt);
|
||||||
|
const ready = sortedEncounters.filter(e => !(e.isStarted && !e.endedAt) && !e.endedAt);
|
||||||
|
const renderCard = (encounter) => {
|
||||||
const isLive = activeDisplayInfo &&
|
const isLive = activeDisplayInfo &&
|
||||||
activeDisplayInfo.activeCampaignId === campaignId &&
|
activeDisplayInfo.activeCampaignId === campaignId &&
|
||||||
activeDisplayInfo.activeEncounterId === encounter.id;
|
activeDisplayInfo.activeEncounterId === encounter.id;
|
||||||
@@ -2661,7 +2691,40 @@ function EncounterManager({ campaignId, initialActiveEncounterId, campaignCharac
|
|||||||
>
|
>
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
<div onClick={() => setSelectedEncounterId(encounter.id)} className="cursor-pointer flex-grow">
|
<div onClick={() => setSelectedEncounterId(encounter.id)} className="cursor-pointer flex-grow">
|
||||||
|
{renamingId === encounter.id ? (
|
||||||
|
<form
|
||||||
|
onSubmit={(e) => { e.preventDefault(); commitRename(encounter.id); }}
|
||||||
|
className="flex items-center gap-1"
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={renameValue}
|
||||||
|
onChange={(e) => setRenameValue(e.target.value)}
|
||||||
|
onKeyDown={(e) => { if (e.key === 'Escape') cancelRename(); }}
|
||||||
|
autoFocus
|
||||||
|
aria-label="Encounter name"
|
||||||
|
className="font-medium text-white bg-stone-950 border border-amber-500 rounded px-1 py-0.5 w-full min-w-0"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="p-1 rounded bg-emerald-700 hover:bg-emerald-600 text-white"
|
||||||
|
title="Save Name"
|
||||||
|
>
|
||||||
|
<Save size={14} />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={cancelRename}
|
||||||
|
className="p-1 rounded bg-stone-700 hover:bg-stone-600 text-white"
|
||||||
|
title="Cancel Rename"
|
||||||
|
>
|
||||||
|
<X size={14} />
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
) : (
|
||||||
<h4 className="font-medium text-white">{encounter.name} <span className={`ml-1 px-1.5 py-0.5 rounded text-xs font-bold tracking-wide ${encounter.ruleset === 'generic' ? 'bg-purple-900/80 text-purple-200 border border-purple-500' : 'bg-amber-900/80 text-amber-200 border border-amber-500'}`}>{encounter.ruleset === 'generic' ? 'GEN' : '5e'}</span>{encounter.isStarted && !encounter.endedAt && <span className="ml-1 px-1.5 py-0.5 rounded text-xs font-bold bg-red-600 text-white animate-pulse">⚔ IN PROGRESS</span>}</h4>
|
<h4 className="font-medium text-white">{encounter.name} <span className={`ml-1 px-1.5 py-0.5 rounded text-xs font-bold tracking-wide ${encounter.ruleset === 'generic' ? 'bg-purple-900/80 text-purple-200 border border-purple-500' : 'bg-amber-900/80 text-amber-200 border border-amber-500'}`}>{encounter.ruleset === 'generic' ? 'GEN' : '5e'}</span>{encounter.isStarted && !encounter.endedAt && <span className="ml-1 px-1.5 py-0.5 rounded text-xs font-bold bg-red-600 text-white animate-pulse">⚔ IN PROGRESS</span>}</h4>
|
||||||
|
)}
|
||||||
<p className="text-xs text-stone-300">
|
<p className="text-xs text-stone-300">
|
||||||
{encounter.createdAt && `${new Date(encounter.createdAt).toLocaleDateString('en-US', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', hour12: false })} · `}Participants: {encounter.participants?.length || 0}
|
{encounter.createdAt && `${new Date(encounter.createdAt).toLocaleDateString('en-US', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', hour12: false })} · `}Participants: {encounter.participants?.length || 0}
|
||||||
</p>
|
</p>
|
||||||
@@ -2689,6 +2752,16 @@ function EncounterManager({ campaignId, initialActiveEncounterId, campaignCharac
|
|||||||
>
|
>
|
||||||
{isLive ? <EyeOff size={18} /> : <Eye size={18} />}
|
{isLive ? <EyeOff size={18} /> : <Eye size={18} />}
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
startRename(encounter);
|
||||||
|
}}
|
||||||
|
className="p-1 rounded transition-colors text-amber-400 hover:text-amber-300 bg-stone-700 hover:bg-stone-600"
|
||||||
|
title="Rename Encounter"
|
||||||
|
>
|
||||||
|
<Pencil size={18} />
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
@@ -2703,8 +2776,40 @@ function EncounterManager({ campaignId, initialActiveEncounterId, campaignCharac
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
};
|
||||||
</div>
|
|
||||||
|
const Group = ({ label, count, items, collapsible, open, onToggle }) => (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={collapsible ? onToggle : undefined}
|
||||||
|
aria-expanded={collapsible ? open : undefined}
|
||||||
|
className="flex items-center gap-2 text-sm font-semibold text-stone-300 font-cinzel tracking-wide w-full text-left"
|
||||||
|
>
|
||||||
|
{collapsible ? (open ? <ChevronDown size={16} /> : <ChevronRight size={16} />) : null}
|
||||||
|
{label}
|
||||||
|
<span className="px-1.5 py-0.5 rounded bg-stone-700 text-stone-300 text-xs font-bold">{count}</span>
|
||||||
|
</button>
|
||||||
|
{(!collapsible || open) && items}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const wrap = (encounter) => (
|
||||||
|
<div key={encounter.id} className="space-y-3">{renderCard(encounter)}</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const groups = [];
|
||||||
|
if (inProgress.length > 0) {
|
||||||
|
groups.push(<Group key="g-prog" label="⚔ In Progress" count={inProgress.length} items={inProgress.map(wrap)} collapsible={false} open />);
|
||||||
|
}
|
||||||
|
if (ready.length > 0) {
|
||||||
|
groups.push(<Group key="g-ready" label="Ready" count={ready.length} items={ready.map(wrap)} collapsible={false} open />);
|
||||||
|
}
|
||||||
|
if (completed.length > 0) {
|
||||||
|
groups.push(<Group key="g-done" label="Completed" count={completed.length} items={completed.map(wrap)} collapsible open={completedOpen} onToggle={() => setCompletedOpen(v => !v)} />);
|
||||||
|
}
|
||||||
|
return <div className="space-y-4">{groups}</div>;
|
||||||
|
})()}
|
||||||
|
|
||||||
{showCreateModal && (
|
{showCreateModal && (
|
||||||
<Modal onClose={() => setShowCreateModal(false)} title="Create New Encounter">
|
<Modal onClose={() => setShowCreateModal(false)} title="Create New Encounter">
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
// Encounter list groups: In Progress / Ready / Completed.
|
||||||
|
// Completed collapsed by default. Group headers show counts.
|
||||||
|
|
||||||
|
import { screen, fireEvent, act } from '@testing-library/react';
|
||||||
|
import '@testing-library/jest-dom';
|
||||||
|
import { MOCK_DB } from '../__mocks__/firebase/_mock-db';
|
||||||
|
import { renderApp, createCampaignViaUI, selectCampaignByName } from './testHelpers';
|
||||||
|
|
||||||
|
const encPath = (cid, eid) => `artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/${cid}/encounters/${eid}`;
|
||||||
|
|
||||||
|
async function seedEncounter(cid, eid, fields) {
|
||||||
|
act(() => {
|
||||||
|
MOCK_DB.set(encPath(cid, eid), {
|
||||||
|
id: eid,
|
||||||
|
name: fields.name,
|
||||||
|
participants: [],
|
||||||
|
round: 0,
|
||||||
|
isStarted: false,
|
||||||
|
isPaused: false,
|
||||||
|
createdAt: fields.createdAt || '2026-07-01T00:00:00Z',
|
||||||
|
order: fields.order,
|
||||||
|
...fields,
|
||||||
|
});
|
||||||
|
// touch collection so useFirestoreCollection snapshot re-emits
|
||||||
|
MOCK_DB.set(`artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/${cid}`, { name: cid, players: [] });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('encounter list grouping', () => {
|
||||||
|
test('groups appear with counts: In Progress, Ready, Completed', async () => {
|
||||||
|
await renderApp();
|
||||||
|
const cid = await createCampaignViaUI('GroupCamp');
|
||||||
|
await selectCampaignByName('GroupCamp');
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
MOCK_DB.set(encPath(cid, 'e-ready'), { id: 'e-ready', name: 'Ready One', participants: [], round: 0, isStarted: false, isPaused: false, createdAt: '2026-07-01T00:00:00Z' });
|
||||||
|
MOCK_DB.set(encPath(cid, 'e-prog'), { id: 'e-prog', name: 'Live One', participants: [], round: 0, isStarted: true, startedAt: 1, isPaused: false, createdAt: '2026-07-01T00:00:00Z' });
|
||||||
|
MOCK_DB.set(encPath(cid, 'e-done'), { id: 'e-done', name: 'Old One', participants: [], round: 0, isStarted: false, startedAt: 1, endedAt: 2, isPaused: false, createdAt: '2026-07-01T00:00:00Z' });
|
||||||
|
MOCK_DB.set(`artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/${cid}`, { name: cid, players: [] });
|
||||||
|
});
|
||||||
|
|
||||||
|
// gate on card text first (groups render), then headers.
|
||||||
|
// 'Old One' lives in collapsed Completed group — assert after expanding.
|
||||||
|
await screen.findByText('Live One');
|
||||||
|
await screen.findByText('Ready One');
|
||||||
|
expect(screen.getByRole('button', { name: /⚔ In Progress/ })).toBeInTheDocument();
|
||||||
|
expect(screen.getByRole('button', { name: /^Ready/ })).toBeInTheDocument();
|
||||||
|
const completedBtn = screen.getByRole('button', { name: /^Completed/ });
|
||||||
|
expect(completedBtn).toBeInTheDocument();
|
||||||
|
fireEvent.click(completedBtn);
|
||||||
|
expect(await screen.findByText('Old One')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('completed encounters hidden until group expanded', async () => {
|
||||||
|
await renderApp();
|
||||||
|
const cid = await createCampaignViaUI('GroupCamp2');
|
||||||
|
await selectCampaignByName('GroupCamp2');
|
||||||
|
|
||||||
|
await seedEncounter(cid, 'e-done2', { name: 'Hidden Corpse', isStarted: false, startedAt: 1, endedAt: 2 });
|
||||||
|
|
||||||
|
expect(await screen.findByRole('button', { name: /^Completed/ })).toBeInTheDocument();
|
||||||
|
expect(screen.queryByText('Hidden Corpse')).not.toBeInTheDocument();
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: /^Completed/ }));
|
||||||
|
expect(await screen.findByText('Hidden Corpse')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('in-progress and ready encounters visible by default', async () => {
|
||||||
|
await renderApp();
|
||||||
|
const cid = await createCampaignViaUI('GroupCamp3');
|
||||||
|
await selectCampaignByName('GroupCamp3');
|
||||||
|
|
||||||
|
await seedEncounter(cid, 'e-ready3', { name: 'Visible Ready', isStarted: false });
|
||||||
|
await seedEncounter(cid, 'e-prog3', { name: 'Visible Live', isStarted: true, startedAt: 1 });
|
||||||
|
|
||||||
|
expect(await screen.findByText('Visible Ready')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Visible Live')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user