Fix clearing map selected cells between pages

Also limits fitBounds zooming to selecting mods or plugins only.
This commit is contained in:
Tyler Hallada 2022-03-18 20:20:12 -04:00
parent 68796f6f9c
commit daa4d3c039

View File

@ -138,7 +138,7 @@ const Map: React.FC = () => {
);
const selectCells = useCallback(
(cells: { x: number; y: number }[]) => {
(cells: { x: number; y: number }[], { fitCells } = { fitCells: false }) => {
if (!map.current) return;
if (map.current && !map.current.getSource("grid-source")) return;
@ -168,6 +168,7 @@ const Map: React.FC = () => {
visited[id] = true;
}
if (fitCells) {
let bounds: mapboxgl.LngLatBounds | null = null;
const fitBounds = () => {
const zoom = map.current.getZoom();
@ -213,6 +214,7 @@ const Map: React.FC = () => {
} else {
fitBounds();
}
}
},
[map]
);
@ -280,14 +282,14 @@ const Map: React.FC = () => {
selectedCell.x !== cell.x ||
selectedCell.y !== cell.y
) {
// clearSelectedCells();
clearSelectedCells();
selectCell(cell);
}
} else if (router.query.mod && typeof router.query.mod === "string") {
if (selectedCells) {
clearSelectedCell();
setSidebarOpen(true);
selectCells(selectedCells);
selectCells(selectedCells, { fitCells: true });
} else {
// TODO: this is so spaghetti
clearSelectedCell();
@ -311,12 +313,21 @@ const Map: React.FC = () => {
cellSet.add(cell.x + cell.y * 1000);
}
}
selectCells(cells);
selectCells(cells, { fitCells: true });
}
}
} else if (plugins && plugins.length > 0 && pluginsPending === 0) {
} else {
clearSelectedCells();
clearSelectedCell();
}
if (
plugins &&
plugins.length > 0 &&
pluginsPending === 0 &&
!router.query.mod &&
!router.query.plugin
) {
const cells = plugins.reduce(
(acc: { x: number; y: number }[], plugin: PluginFile) => {
if (plugin.enabled && plugin.parsed) {
@ -337,9 +348,6 @@ const Map: React.FC = () => {
[]
);
selectCells(cells);
} else {
clearSelectedCells();
clearSelectedCell();
}
}, [
selectedCell,