From 5e5812bcaac03bdd976a7a2c44b1c5540fb28476 Mon Sep 17 00:00:00 2001 From: Lukas Date: Tue, 24 Mar 2026 23:18:49 +0100 Subject: [PATCH] Fix stat block panel showing wrong creature on first open useAutoStatBlock was overriding the user's creature selection when the panel transitioned from closed to open. Now only auto-updates when the active turn index changes (advance/retreat), not when the panel mode changes. Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/web/src/hooks/use-auto-stat-block.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/web/src/hooks/use-auto-stat-block.ts b/apps/web/src/hooks/use-auto-stat-block.ts index c5ab110..d1b4abe 100644 --- a/apps/web/src/hooks/use-auto-stat-block.ts +++ b/apps/web/src/hooks/use-auto-stat-block.ts @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useEffect, useRef } from "react"; import { useEncounterContext } from "../contexts/encounter-context.js"; import { useSidePanelContext } from "../contexts/side-panel-context.js"; @@ -8,10 +8,20 @@ export function useAutoStatBlock(): void { const activeCreatureId = encounter.combatants[encounter.activeIndex]?.creatureId; + const prevActiveIndexRef = useRef(encounter.activeIndex); useEffect(() => { - if (activeCreatureId && panelView.mode === "creature") { + const prevIndex = prevActiveIndexRef.current; + prevActiveIndexRef.current = encounter.activeIndex; + + // Only auto-update when the active turn changes (advance/retreat), + // not when the panel mode changes (user clicking a different creature). + if ( + encounter.activeIndex !== prevIndex && + activeCreatureId && + panelView.mode === "creature" + ) { updateCreature(activeCreatureId); } - }, [activeCreatureId, panelView.mode, updateCreature]); + }, [encounter.activeIndex, activeCreatureId, panelView.mode, updateCreature]); }