diff --git a/src/components/inventory/views/badge/InventoryBadgeView.tsx b/src/components/inventory/views/badge/InventoryBadgeView.tsx index eb4e2fbe..ab2bbbb3 100644 --- a/src/components/inventory/views/badge/InventoryBadgeView.tsx +++ b/src/components/inventory/views/badge/InventoryBadgeView.tsx @@ -39,7 +39,7 @@ export const InventoryBadgeView: FC<{}> = props => { badgeCodes && (badgeCodes.length > 0) && badgeCodes.map((badgeCode, index) => { - if(activeBadgeCodes.indexOf(badgeCode) >= 0) return null; + if(isWearingBadge(badgeCode)) return null; return }) } diff --git a/src/hooks/inventory/useInventoryBadges.ts b/src/hooks/inventory/useInventoryBadges.ts index cf5f3ec5..000e4e3b 100644 --- a/src/hooks/inventory/useInventoryBadges.ts +++ b/src/hooks/inventory/useInventoryBadges.ts @@ -10,7 +10,7 @@ const useInventoryBadgesState = () => { const [ needsUpdate, setNeedsUpdate ] = useState(true); const [ badgeCodes, setBadgeCodes ] = useState([]); - const [ badgeIds, setBadgeIds ] = useState([]); + const [ badgeIds, setBadgeIds ] = useState>(new Map()); const [ activeBadgeCodes, setActiveBadgeCodes ] = useState([]); const [ selectedBadgeCode, setSelectedBadgeCode ] = useState(null); const { isVisible = false, activate = null, deactivate = null } = useSharedVisibility(); @@ -41,7 +41,7 @@ const useInventoryBadgesState = () => const composer = new SetActivatedBadgesComposer(); - for(let i = 0; i < maxBadgeCount; i++) composer.addActivatedBadge(newValue[i] || null); + for(let i = 0; i < maxBadgeCount; i++) composer.addActivatedBadge(newValue[i] ?? ''); SendMessageComposer(composer); @@ -55,7 +55,7 @@ const useInventoryBadgesState = () => if(index === -1) return 0; - return (badgeIds[index] || 0); + return (badgeIds.get(badgeCode) ?? 0); } useMessageEvent(BadgesEvent, event => @@ -65,23 +65,25 @@ const useInventoryBadgesState = () => setBadgeIds(prevValue => { - const newValue = [ ...prevValue ]; + const newValue = new Map(prevValue); parser.getAllBadgeCodes().forEach(code => { + const exists = badgeCodes.indexOf(code) >= 0; const badgeId = parser.getBadgeId(code); - if(newValue.indexOf(badgeId) >= 0) return; - - newValue.push(badgeId); + newValue.set(code, badgeId); + + if(exists) return; + badgesToAdd.push(code); }); - + return newValue; }); - setBadgeCodes(prevValue => [ ...prevValue, ...badgesToAdd ]); setActiveBadgeCodes(parser.getActiveBadgeCodes()); + setBadgeCodes(prev => [ ...prev, ...badgesToAdd ]); }); useMessageEvent(BadgeReceivedEvent, event => @@ -101,10 +103,9 @@ const useInventoryBadgesState = () => setBadgeIds(prevValue => { - const newValue = [ ...prevValue ]; + const newValue = new Map(prevValue); - if(unseen) newValue.unshift(parser.badgeId) - else newValue.push(parser.badgeId); + newValue.set(parser.badgeCode, parser.badgeId); return newValue; });