From 4bfe49f454d595e3a8028f03aee11777bd73d7d1 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 10 Jun 2021 20:08:11 +1000 Subject: [PATCH] Optimisation to tile rendering --- src/components/map/MapTiles.js | 6 +++--- src/components/token/TokenTiles.js | 8 +++----- src/contexts/MapDataContext.js | 11 +++++++++++ src/contexts/TokenDataContext.js | 13 +++++++++---- src/modals/SelectMapModal.js | 6 ++++-- src/modals/SelectTokensModal.js | 5 +++-- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/components/map/MapTiles.js b/src/components/map/MapTiles.js index 294661d..3adeba6 100644 --- a/src/components/map/MapTiles.js +++ b/src/components/map/MapTiles.js @@ -9,7 +9,7 @@ import { getGroupItems } from "../../helpers/group"; import { useGroup } from "../../contexts/GroupContext"; -function MapTiles({ maps, onMapEdit, onMapSelect, subgroup }) { +function MapTiles({ mapsById, onMapEdit, onMapSelect, subgroup }) { const { selectedGroupIds, selectMode, @@ -19,7 +19,7 @@ function MapTiles({ maps, onMapEdit, onMapSelect, subgroup }) { function renderTile(group) { if (group.type === "item") { - const map = maps.find((map) => map.id === group.id); + const map = mapsById[group.id]; const isSelected = selectedGroupIds.includes(group.id); const canEdit = isSelected && selectMode === "single" && selectedGroupIds.length === 1; @@ -44,7 +44,7 @@ function MapTiles({ maps, onMapEdit, onMapSelect, subgroup }) { maps.find((map) => map.id === item.id))} + maps={items.map((item) => mapsById[item.id])} isSelected={isSelected} onSelect={onGroupSelect} onDoubleClick={() => canOpen && onGroupOpen(group.id)} diff --git a/src/components/token/TokenTiles.js b/src/components/token/TokenTiles.js index 363eeb9..02d5059 100644 --- a/src/components/token/TokenTiles.js +++ b/src/components/token/TokenTiles.js @@ -10,7 +10,7 @@ import { getGroupItems } from "../../helpers/group"; import { useGroup } from "../../contexts/GroupContext"; -function TokenTiles({ tokens, onTokenEdit, subgroup }) { +function TokenTiles({ tokensById, onTokenEdit, subgroup }) { const { selectedGroupIds, selectMode, @@ -20,7 +20,7 @@ function TokenTiles({ tokens, onTokenEdit, subgroup }) { function renderTile(group) { if (group.type === "item") { - const token = tokens.find((token) => token.id === group.id); + const token = tokensById[group.id]; const isSelected = selectedGroupIds.includes(group.id); const canEdit = isSelected && selectMode === "single" && selectedGroupIds.length === 1; @@ -48,9 +48,7 @@ function TokenTiles({ tokens, onTokenEdit, subgroup }) { - tokens.find((token) => token.id === item.id) - )} + tokens={items.map((item) => tokensById[item.id])} isSelected={isSelected} onSelect={onGroupSelect} onDoubleClick={() => canOpen && onGroupOpen(group.id)} diff --git a/src/contexts/MapDataContext.js b/src/contexts/MapDataContext.js index b110b18..f29c713 100644 --- a/src/contexts/MapDataContext.js +++ b/src/contexts/MapDataContext.js @@ -221,6 +221,16 @@ export function MapDataProvider({ children }) { }; }, [database, databaseStatus]); + const [mapsById, setMapsById] = useState({}); + useEffect(() => { + setMapsById( + maps.reduce((obj, map) => { + obj[map.id] = map; + return obj; + }, {}) + ); + }, [maps]); + const value = { maps, mapStates, @@ -234,6 +244,7 @@ export function MapDataProvider({ children }) { mapsLoading, getMapState, updateMapGroups, + mapsById, }; return ( {children} diff --git a/src/contexts/TokenDataContext.js b/src/contexts/TokenDataContext.js index 9771c31..a72722a 100644 --- a/src/contexts/TokenDataContext.js +++ b/src/contexts/TokenDataContext.js @@ -169,10 +169,15 @@ export function TokenDataProvider({ children }) { }; }, [database, databaseStatus]); - const tokensById = tokens.reduce((obj, token) => { - obj[token.id] = token; - return obj; - }, {}); + const [tokensById, setTokensById] = useState({}); + useEffect(() => { + setTokensById( + tokens.reduce((obj, token) => { + obj[token.id] = token; + return obj; + }, {}) + ); + }, [tokens]); const value = { tokens, diff --git a/src/modals/SelectMapModal.js b/src/modals/SelectMapModal.js index 36b5b16..be7dfb0 100644 --- a/src/modals/SelectMapModal.js +++ b/src/modals/SelectMapModal.js @@ -52,6 +52,7 @@ function SelectMapModal({ updateMapGroups, updateMap, updateMapState, + mapsById, } = useMapData(); const { addAssets } = useAssets(); @@ -214,6 +215,7 @@ function SelectMapModal({ handleWidth handleHeight onResize={handleModalResize} + refreshMode="debounce" > @@ -254,7 +256,7 @@ function SelectMapModal({ > @@ -256,7 +257,7 @@ function SelectTokensModal({ isOpen, onRequestClose, onMapTokensStateCreate }) { >