From 565c2a4095d27e737c97275438e103665e906476 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 12 Sep 2024 16:24:15 -0500 Subject: [PATCH] perf: memo --- app/react/components/dom/bag.jsx | 6 +++- app/react/components/dom/hotbar.jsx | 6 +++- app/react/components/ui.jsx | 46 ++++++++++++++++++----------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/react/components/dom/bag.jsx b/app/react/components/dom/bag.jsx index 3019e91..193e05f 100644 --- a/app/react/components/dom/bag.jsx +++ b/app/react/components/dom/bag.jsx @@ -1,3 +1,5 @@ +import {memo} from 'react'; + import styles from './bag.module.css'; import Grid from './grid.jsx'; @@ -5,7 +7,7 @@ import Grid from './grid.jsx'; /** * Inventory bag. 10-40 slots of inventory. */ -export default function Bag({ +function Bag({ isInventoryOpen, onActivate, slots, @@ -25,3 +27,5 @@ export default function Bag({ ); } + +export default memo(Bag); diff --git a/app/react/components/dom/hotbar.jsx b/app/react/components/dom/hotbar.jsx index 7a4eec0..c0a1772 100644 --- a/app/react/components/dom/hotbar.jsx +++ b/app/react/components/dom/hotbar.jsx @@ -1,3 +1,5 @@ +import {memo} from 'react'; + import styles from './hotbar.module.css'; import gridStyles from './grid.module.css'; @@ -6,7 +8,7 @@ import Grid from './grid.jsx'; /** * The hotbar. 10 slots of inventory with an active selection. */ -export default function Hotbar({ +function Hotbar({ active, hotbarIsHidden, onActivate, @@ -33,3 +35,5 @@ export default function Hotbar({ ); } + +export default memo(Hotbar); diff --git a/app/react/components/ui.jsx b/app/react/components/ui.jsx index 23634bf..4cafa94 100644 --- a/app/react/components/ui.jsx +++ b/app/react/components/ui.jsx @@ -26,7 +26,7 @@ const KEY_MAP = { }; function emptySlots() { - return Array(50).fill(undefined); + return Array(10).fill(undefined); } const devEventsChannel = new EventEmitter(); @@ -45,6 +45,7 @@ function Ui({disconnected}) { const [devtoolsIsOpen, setDevtoolsIsOpen] = useState(false); const ratio = (RESOLUTION.x * (devtoolsIsOpen ? 2 : 1)) / RESOLUTION.y; const [camera, setCamera] = useState({x: 0, y: 0}); + const [hotbarSlots, setHotbarSlots] = useState(emptySlots()); const [inventorySlots, setInventorySlots] = useState(emptySlots()); const [activeSlot, setActiveSlot] = useState(0); const [scale, setScale] = useState(2); @@ -265,9 +266,16 @@ function Ui({disconnected}) { if (update.Inventory) { if (mainEntityRef.current === id) { setBufferSlot(entity.Inventory.item(0)); + setHotbarSlots(() => { + const newHotbarSlots = []; + for (let i = 1; i < 11; ++i) { + newHotbarSlots.push(entity.Inventory.item(i)); + } + return newHotbarSlots; + }); const newInventorySlots = emptySlots(); - for (let i = 1; i < 41; ++i) { - newInventorySlots[i - 1] = entity.Inventory.item(i); + for (let i = 11; i < 41; ++i) { + newInventorySlots[i - 11] = entity.Inventory.item(i); } setInventorySlots(newInventorySlots); } @@ -382,6 +390,19 @@ function Ui({disconnected}) { mainEntityRef, scale, ]); + const hotbarOnActivate = useCallback((i) => { + keepHotbarOpen(); + client.send({ + type: 'Action', + payload: {type: 'swapSlots', value: [0, mainEntityRef.current, i + 1]}, + }); + }, [client, keepHotbarOpen, mainEntityRef]); + const bagOnActivate = useCallback((i) => { + client.send({ + type: 'Action', + payload: {type: 'swapSlots', value: [0, mainEntityRef.current, i + 11]}, + }); + }, [client, mainEntityRef]); return (
{ - keepHotbarOpen(); - client.send({ - type: 'Action', - payload: {type: 'swapSlots', value: [0, mainEntityRef.current, i + 1]}, - }); - }} - slots={inventorySlots.slice(0, 10)} + onActivate={hotbarOnActivate} + slots={hotbarSlots} /> { - client.send({ - type: 'Action', - payload: {type: 'swapSlots', value: [0, mainEntityRef.current, i + 11]}, - }); - }} - slots={inventorySlots.slice(10, 20)} + onActivate={bagOnActivate} + slots={inventorySlots} /> {externalInventory && (