1
0
Fork 0

fix(npc-dialogue): keep choice footer hidden on pick and refresh gates after events

Two runtime bugs in NpcConversation surfaced while conversing with D8-24.

1. After selecting a choice, the old choices briefly reappeared before hiding
   again. The footer renders when neither typing indicator is up and the log
   rests on a choice node, but between the player's line landing and the next
   node starting to type there was a render gap (condition evaluation,
   tokenized text resolution, or a delay node setting an empty speaker) where
   both flags were clear. Added an awaitingReply flag set on pick and cleared
   only once the cascade comes to rest on the next node, so the footer stays
   hidden through the whole transition.

2. A flag written by an event node mid-conversation did not update a gated
   choice's locked/unlocked state until a page reload. choiceGates are cached
   per node id and the fetch effect skips cached nodes, so the cache went
   stale. Now the gate cache is dropped whenever an event writes a flag, and
   the fetch effect skips while awaitingReply is true so it never re-queries
   the node being left.

Verified live against D8-24: choices stay hidden across a pick, and a gated
choice flips to disabled immediately after an event sets its flag, no reload.
This commit is contained in:
Jason Fraley 2026-10-14 13:27:02 -04:00
parent 35a88ccd8f
commit 82607daf6e

View file

@ -85,8 +85,16 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
const [takenChoices, setTakenChoices] = useState<Set<string>>(new Set());
/** Per-node visibility gates for condition-gated choices, fetched from
* the server when the conversation lands on a choice node. Cleared on
* every `begin()` so flags gained mid-conversation are re-checked. */
* every `begin()` AND whenever an event writes a flag mid-conversation, so
* a gated choice's locked/unlocked state always reflects live flags rather
* than a gate cached before the flag changed (which used to need a reload). */
const [choiceGates, setChoiceGates] = useState<Record<string, ChoiceGate[]>>({});
/** True from the instant a choice is picked until the conversation lands on
* the next node. Keeps the whole choice footer hidden through the pick →
* reply transition, so the just-picked choices never flash back during the
* gap where neither typing indicator is up but the log still rests on the
* old choice node (while the next line's text resolves server-side). */
const [awaitingReply, setAwaitingReply] = useState(false);
/** Per-browser read progress powering the NEW badges. Loaded after mount so
* server and client markup agree (localStorage is invisible to SSR). */
const [seenMap, setSeenMap] = useState<SeenMap>({});
@ -121,9 +129,11 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
// Fetch the server-side visibility gates when landing on a choice node
// that has gated choices. Gated choices stay hidden until confirmed, so a
// forbidden option never flashes on screen.
// forbidden option never flashes on screen. Skipped mid-transition
// (`awaitingReply`): gates are only read once we come to rest on a node, and
// fetching then would re-query the choice node we are already leaving.
useEffect(() => {
if (!open || !flow || !activeChoiceNodeId) return;
if (!open || !flow || !activeChoiceNodeId || awaitingReply) return;
if (choiceGates[activeChoiceNodeId]) return;
const node = flow.nodes.find((n) => n.id === activeChoiceNodeId);
const hasGated = (node?.data.choices ?? []).some((c) => c.condition?.key?.trim());
@ -138,7 +148,7 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
return () => {
cancelled = true;
};
}, [open, flow, activeChoiceNodeId, choiceGates, npc.id]);
}, [open, flow, activeChoiceNodeId, choiceGates, awaitingReply, npc.id]);
// NEW badge inputs: current flowchart signature + the nodes this browser
// has not read yet (a GM edit changes the hash and re-flags everything).
@ -195,6 +205,7 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
setPlayerTyping(false);
setTakenChoices(new Set());
setChoiceGates({});
setAwaitingReply(false);
setOpen(true);
if (!flow.startId) return;
void (async () => {
@ -241,6 +252,10 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
// that follow (interpolated text and later condition nodes).
await triggerNpcDialogueEvent(Number(npc.id), current.id).catch(() => undefined);
if (sessionRef.current !== token) return;
// The effect may have written a flag a gated choice depends on. Drop
// cached gates so the next choice node re-checks them against live
// flags instead of rendering a stale locked/unlocked state.
setChoiceGates({});
}
const target: string | undefined = current.data.next?.target;
current = target ? (flow.nodes.find((n) => n.id === target) ?? null) : null;
@ -265,6 +280,10 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
const pick = (hop: DialogueNext, choiceIndex: number, displayText?: string) => {
const token = sessionRef.current;
const text = displayText || hop.label || "…";
// Hide the footer immediately (before the async typing starts) and keep it
// hidden until the next node lands, so the picked choices never reappear
// mid-transition.
setAwaitingReply(true);
if (hop.once && lastNode && lastNode.kind === "node") {
const key = `${lastNode.node.id}:${choiceIndex}`;
setTakenChoices((prev) => (prev.has(key) ? prev : new Set(prev).add(key)));
@ -276,7 +295,14 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
setPlayerTyping(false);
setLog((prev) => [...prev, { kind: "player", id: nextId(), text }]);
const target = flow.nodes.find((n) => n.id === hop.target);
if (target) await cascadeFrom(target, token);
try {
if (target) await cascadeFrom(target, token);
} finally {
// Reveal the footer only once we've come to rest on the next node. If
// the session was invalidated (reopened/closed), `begin()` owns the
// reset, so leave it alone here.
if (sessionRef.current === token) setAwaitingReply(false);
}
})();
};
@ -393,6 +419,7 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
{!typing &&
!playerTyping &&
!awaitingReply &&
lastNode &&
lastNode.kind === "node" &&
(lastNode.node.type === "choice" || lastNode.node.type === "end") && (