From 39fbfdd9e2db880acd60f99e290205dd3286567e Mon Sep 17 00:00:00 2001 From: simoleo89 Date: Sat, 13 Jun 2026 15:58:27 +0200 Subject: [PATCH] fix(inventory): derive active prefix from the fresh list, not a stale closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ActivePrefixUpdatedEvent handler set the active prefix via `setActivePrefix(prev => { const found = prefixes.find(...) })`, reading the `prefixes` state from the closure — which lags by a render and is stale/empty when the prefix was added earlier in the same event batch, so `found` was undefined and the active prefix fell back to a partial item missing icon/color/displayName. Move the derivation inside the `setPrefixes` updater so it reads the freshly-mapped list. --- src/hooks/inventory/useInventoryPrefixes.ts | 32 ++++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/hooks/inventory/useInventoryPrefixes.ts b/src/hooks/inventory/useInventoryPrefixes.ts index 81702d3..d42126d 100644 --- a/src/hooks/inventory/useInventoryPrefixes.ts +++ b/src/hooks/inventory/useInventoryPrefixes.ts @@ -78,25 +78,29 @@ const useInventoryPrefixesState = () => setPrefixes(prevValue => { - return prevValue.map(p => ({ + const next = prevValue.map(p => ({ ...p, active: p.id === parser.prefixId })); - }); - if(parser.prefixId === 0) - { - setActivePrefix(null); - } - else - { - setActivePrefix(prev => + // Derive the active prefix from the freshly-mapped list, not from + // the `prefixes` closure (which lags a render and is stale when the + // prefix was added earlier in the same event batch). + if(parser.prefixId === 0) { - const found = prefixes.find(p => p.id === parser.prefixId); - if(found) return { ...found, active: true, font: parser.font || found.font || '' }; - return { id: parser.prefixId, text: parser.text, color: parser.color, icon: parser.icon || '', effect: parser.effect || '', font: parser.font || '', active: true }; - }); - } + setActivePrefix(null); + } + else + { + const found = next.find(p => p.id === parser.prefixId); + + setActivePrefix(found + ? { ...found, active: true, font: parser.font || found.font || '' } + : { id: parser.prefixId, text: parser.text, color: parser.color, icon: parser.icon || '', effect: parser.effect || '', font: parser.font || '', active: true }); + } + + return next; + }); }); const activatePrefix = (prefixId: number) =>