mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-19 15:06:20 +00:00
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:
@@ -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) =>
|
||||
|
||||
Reference in New Issue
Block a user