Attempt to fix #196 - sticky selector

This commit is contained in:
Jeremie Pardou-Piquemal 2020-11-14 14:10:08 +01:00 committed by Jérémie Pardou-Piquemal
parent 54eba32bb4
commit 4a6045a3e4
4 changed files with 12 additions and 3 deletions

View file

@ -42,7 +42,9 @@ const useItems = () => {
(items) => { (items) => {
setItemMap( setItemMap(
items.reduce((acc, item) => { items.reduce((acc, item) => {
acc[item.id] = item; if (item && item.id) {
acc[item.id] = item;
}
return acc; return acc;
}, {}) }, {})
); );

View file

@ -34,6 +34,11 @@ const findSelected = (itemMap, rect) => {
.filter((elem) => { .filter((elem) => {
const { clientHeight, clientWidth, id } = elem; const { clientHeight, clientWidth, id } = elem;
const item = itemMap[id]; const item = itemMap[id];
if (!item) {
// Avoid to find item that are not yet removed from DOM
console.error(`Missing item ${id}`);
return false;
}
return ( return (
!item.locked && !item.locked &&
isPointInsideRect({ x: item.x, y: item.y }, rect) && isPointInsideRect({ x: item.x, y: item.y }, rect) &&

View file

@ -23,7 +23,9 @@ export const AllItemsSelector = selector({
key: "AllItemsSelector", key: "AllItemsSelector",
get: ({ get }) => { get: ({ get }) => {
const itemMap = get(ItemMapAtom); const itemMap = get(ItemMapAtom);
return get(ItemListAtom).map((id) => itemMap[id]); return get(ItemListAtom)
.map((id) => itemMap[id])
.filter((item) => item); // This filter clean the selection of missing items
}, },
}); });

View file

@ -37,7 +37,7 @@ export const GameProvider = ({ gameId, game, children }) => {
newGame.availableItems.map((item) => ({ ...item, id: nanoid() })) newGame.availableItems.map((item) => ({ ...item, id: nanoid() }))
); );
} }
setItemList(newGame.items); setItemList(newGame.items.filter((item) => item)); // The filter avoid the empty item bug on reload
setBoardConfig(newGame.board, false); setBoardConfig(newGame.board, false);
setGameLoaded(true); setGameLoaded(true);
}, },