diff --git a/src/components/frontend/personnel/NpcConversation.tsx b/src/components/frontend/personnel/NpcConversation.tsx index 831d64da..bb0e2a13 100644 --- a/src/components/frontend/personnel/NpcConversation.tsx +++ b/src/components/frontend/personnel/NpcConversation.tsx @@ -85,8 +85,16 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) { const [takenChoices, setTakenChoices] = useState>(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>({}); + /** 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({}); @@ -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") && (