diff --git a/apps/web/src/features/encounters/EncounterPresenceIndicator.tsx b/apps/web/src/features/encounters/EncounterPresenceIndicator.tsx new file mode 100644 index 000000000..b08fb9016 --- /dev/null +++ b/apps/web/src/features/encounters/EncounterPresenceIndicator.tsx @@ -0,0 +1,80 @@ +/** + * Compact Co-DM presence indicator for the encounter runner (issue #2212, #816 slice 2). + * + * Renders the OTHER human collaborators currently on the same running encounter — a chip + * per person showing their name and coarse activity (`viewing` / `editing`) — resolved + * from the `encounter.presence` SSE roster maintained by {@link useEncounterPresence}. + * + * The current user is never listed (AC #2212.5): the roster this receives may include the + * caller (they legitimately declared), so they are filtered out here. + * + * Presentational only. Secrecy is server-enforced — a non-DM on a hidden encounter never + * receives the frames in the first place (AC #2212.7) — and name resolution comes from the + * membership roster every member already reads (`names`), so no new secret crosses the + * wire. Returns `null` when nobody else is present, so a solo DM sees nothing. + */ +import { useTranslation } from 'react-i18next'; +import type { EncounterPresenceEntry } from '@campfire/schema'; + +export interface EncounterPresenceIndicatorProps { + /** Live roster (may include the caller). */ + members: readonly EncounterPresenceEntry[]; + /** The current user's id (`me.user.id`), stringified for comparison — never rendered. */ + selfUserId: string | number | null | undefined; + /** `String(member.userId)` → display name, from the membership roster. */ + names: ReadonlyMap; +} + +export function EncounterPresenceIndicator({ + members, + selfUserId, + names, +}: EncounterPresenceIndicatorProps): JSX.Element | null { + const { t } = useTranslation(); + // When selfUserId is null/undefined (e.g. while `me` is loading), filter by a + // sentinel that matches nothing rather than an empty string that could match + // a real userId, so the caller never briefly sees themselves (AC #2212.5). + const self = selfUserId == null ? '\u0000SELF_NOT_LOADED' : String(selfUserId); + const others = members.filter((member) => member.userId !== self); + if (others.length === 0) return null; + + return ( + + {others.map((member) => { + const name = names.get(member.userId) ?? member.userId; + const activity = t(`encounters.presence.${member.activity}`); + return ( + + + {t('encounters.presence.personLabel', { name, activity })} + + ); + })} + + ); +} diff --git a/apps/web/src/features/encounters/RunSessionPage.tsx b/apps/web/src/features/encounters/RunSessionPage.tsx index 11c279493..e47f54cb4 100644 --- a/apps/web/src/features/encounters/RunSessionPage.tsx +++ b/apps/web/src/features/encounters/RunSessionPage.tsx @@ -26,7 +26,7 @@ import { DetailPageWayfinding } from '../../components/DetailPageWayfinding'; import { PrintControl } from '../../components/PrintControl'; import { PrintOnly } from '../../components/PrintOnly'; import { useKeyboardCommandHint, useKeyboardGuardedAction } from '../../components/KeyboardCommandProvider'; -import type { ActionSpec, ActionUndoToken, AoeTemplate, CampaignMember, CastSessionCreated, Character, Combatant, CombatantRemoveResult, DiceRoll, DifficultyBand, EncounterDifficulty, EncounterEvent, EncounterWithCombatants, TurnWorkspace as TurnWorkspaceData, FogState, GenerateMapParams, GeneratedMapResult, HpResyncDirection, HpSyncConflict, MapObjectCreate, MapObjectUpdate, MapPing, RulePack, TokenSize, UsableAction } from '@campfire/schema'; +import type { ActionSpec, ActionUndoToken, AoeTemplate, CampaignMember, CastSessionCreated, Character, Combatant, CombatantRemoveResult, DiceRoll, DifficultyBand, EncounterDifficulty, EncounterEvent, EncounterPresenceEntry, EncounterWithCombatants, TurnWorkspace as TurnWorkspaceData, FogState, GenerateMapParams, GeneratedMapResult, HpResyncDirection, HpSyncConflict, MapObjectCreate, MapObjectUpdate, MapPing, RulePack, TokenSize, UsableAction } from '@campfire/schema'; import { actionEconomyForAdapter, ARCHMAGE_ADAPTER_ID, STARFINDER_ADAPTER_ID, buildDifficultyExplanation, fogStatesEqual, hasCriticalHitsForAdapter, hasInitiativeRollForAdapter, LAIR_INITIATIVE_COUNT, LEGENDARY_ACTION_SLOT, ruleSystemAdapter } from '@campfire/schema'; import { entityTargetProps, entityHref } from '../../lib/entityLinks'; import { rulesetCapabilitiesForSelection } from '../../lib/rules'; @@ -90,6 +90,8 @@ import { useAiDmLiveActivity } from '../ai-dm/useAiDmLiveActivity'; import { EncounterAiDriverPanel } from '../ai-dm/EncounterAiDriverPanel'; import { AiDmPresenceTag, AiDmToolActivityRow } from '../ai-dm/AiDmActivityChip'; import { resolveToolActivity, toolResource } from '../ai-dm/toolActivity'; +import { useEncounterPresence } from './useEncounterPresence'; +import { EncounterPresenceIndicator } from './EncounterPresenceIndicator'; import { GameIcon } from '../../components/GameIcon'; import { TermHelp } from '../../components/TermHelp'; import { useWakeLock } from '../screen/useWakeLock'; @@ -1925,6 +1927,14 @@ export default function RunSessionPage() { setSyncRevision(null); }, [eid]); + // Issue #2212 (#816 slice 2): Co-DM presence. The roster lives in `useEncounterPresence` + // (called below, once `headerBusy` is in scope for the activity hint), but the SSE + // handlers wired in `useCampaignEvents` below need to reach it. A ref bridge closes the + // order gap the same way `useCampaignEvents` itself mirrors its own `handlersRef`: the + // ref is assigned during render (before any SSE frame can arrive), so the handlers read + // the live functions without entering the `useCallback` deps. + const presenceApplyRef = useRef<((members: EncounterPresenceEntry[]) => void) | null>(null); + const presenceRedeclareRef = useRef<(() => void) | null>(null); // Live updates over SSE (issue #4) — players waiting for the DM to hit "Start" (or // take a turn, adjust HP, …) see it pushed instantly. Rather than a manual reload, an // event just invalidates the encounter's reads and Query refetches. On a remote delete, @@ -1978,6 +1988,15 @@ export default function RunSessionPage() { invalidateCampaignCheckRequests(queryClient, cid); return; } + // Issue #2212 (#816 slice 2): a Co-DM joined/left/heartbeat-expired on this + // encounter. The frame is a FULL snapshot (see EncounterPresenceSnapshot), so the + // roster is replaced rather than patched — a late or reconnecting client reconciles + // by swapping its local set. Handle it before the encounter-type filter below (which + // only admits the encounter.* change signals), and ignore frames for other encounters. + if (event.type === 'encounter.presence') { + if (event.encounterId === eid) presenceApplyRef.current?.(event.members); + return; + } if (event.type !== 'encounter.updated' && event.type !== 'encounter.deleted' && event.type !== 'encounter.ping' && event.type !== 'encounter.turn_changed') return; if (event.encounterId !== eid) return; if (event.type === 'encounter.deleted') { @@ -2123,6 +2142,10 @@ export default function RunSessionPage() { // frame outright — re-arm the REST turn-beat baseline resync (see // `awaitingTurnBeatResyncRef`'s own comment) so the catch-up encounter read above // re-derives it. + // Issue #2212: an outage that crossed the 45s presence TTL let the server reap this + // DM's entry — re-declare so reconnect restores presence rather than waiting up to a + // heartbeat. No-op when presence is not enabled. + presenceRedeclareRef.current?.(); }, [queryClient, eid, cid, invalidateCampaignCharactersForOwnership, refreshCampaigns]), // Parser recovery (connection stayed up) — same catch-up refetch. onStreamRecovery: useCallback(() => { @@ -2134,6 +2157,8 @@ export default function RunSessionPage() { void queryClient.invalidateQueries({ queryKey: queryKeys.encounterTurn(eid) }); invalidateTableSafety(queryClient, cid); void refreshCampaigns(); + // Issue #2212: same presence-restore rationale as `onReconnect` above. + presenceRedeclareRef.current?.(); }, [queryClient, eid, cid, invalidateCampaignCharactersForOwnership, refreshCampaigns]), onStatusChange: useCallback((status: CampaignEventsStatus) => setEventStatus(status), []), }); @@ -3961,6 +3986,17 @@ export default function RunSessionPage() { // is unavailable, which is the "reconcile before another action is allowed" rule. const headerBusy = runControl.isPending || nextTurnMut.isPending || hpMutationCount > 0 || bulkHpApplyPending || undoTurnMut.isPending || deleteEncounterMut.isPending || escalationControl.isPending || reconcileBlocks; + // Issue #2212 (#816 slice 2): Co-DM presence. Only a DM on a running encounter declares + // (AC #2212.2); the coarse activity is `editing` while a cockpit write is in flight and + // `viewing` otherwise. The hook owns the roster; the ref bridge hands its reconciler / + // re-declarer to the SSE handlers wired further up. + const presence = useEncounterPresence({ + encounterId: eid, + enabled: isDm && encounter?.status === 'running' && Number.isFinite(eid), + activity: headerBusy ? 'editing' : 'viewing', + }); + presenceApplyRef.current = presence.applySnapshot; + presenceRedeclareRef.current = presence.redeclare; const nextTurnShortcut = useKeyboardCommandHint('encounterNextTurn'); useKeyboardGuardedAction( @@ -4663,6 +4699,16 @@ export default function RunSessionPage() { {/* AI-DM presence chip (#344) — the Driver's live session chat is available on this running encounter. */} {liveActivity.mode === 'driver' && } + {/* Co-DM presence (#2212, #816 slice 2) — the other human collaborators + currently on this encounter, from the `encounter.presence` SSE roster. The + current user is filtered out inside the indicator. Hidden when nobody else + is here. Secrecy is server-enforced (a non-DM on a hidden encounter never + receives the frames), so this renders purely from the roster it is given. */} +