fix(inventory): derive active prefix from the fresh list, not a stale closure

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.
This commit is contained in:
simoleo89
2026-06-13 15:58:27 +02:00
parent 9cc9ef86c0
commit 39fbfdd9e2
+18 -14
View File
@@ -78,25 +78,29 @@ const useInventoryPrefixesState = () =>
setPrefixes(prevValue => setPrefixes(prevValue =>
{ {
return prevValue.map(p => ({ const next = prevValue.map(p => ({
...p, ...p,
active: p.id === parser.prefixId active: p.id === parser.prefixId
})); }));
});
if(parser.prefixId === 0) // Derive the active prefix from the freshly-mapped list, not from
{ // the `prefixes` closure (which lags a render and is stale when the
setActivePrefix(null); // prefix was added earlier in the same event batch).
} if(parser.prefixId === 0)
else
{
setActivePrefix(prev =>
{ {
const found = prefixes.find(p => p.id === parser.prefixId); setActivePrefix(null);
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 }; 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) => const activatePrefix = (prefixId: number) =>