Skip to content

Commit

Permalink
Fixed some fly to bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Emil Balitzki <[email protected]>
  • Loading branch information
Corgam committed Jul 11, 2024
1 parent e190728 commit c40ce24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
31 changes: 22 additions & 9 deletions frontend/src/components/DataView/DataRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Fragment } from "react/jsx-runtime";
import { Fragment, useState, useEffect } from "react";
import { DatasetItem } from "../../types/LocationDataTypes";
import { useContext, useState } from "react";
import { useContext } from "react";
import {
Box,
Collapse,
Expand Down Expand Up @@ -28,10 +28,22 @@ interface RowProps {

const DataRow: React.FC<RowProps> = ({ row }) => {
const [open, setOpen] = useState(false);
const [shouldFlyTo, setShouldFlyTo] = useState<LatLng | null>(null);
const { currentAlertCache, setCurrentAlertCache } = useContext(AlertContext);
const { changeToOrOpenNewTab } = useContext(TabsContext);
const { currentMapCache } = useContext(MapContext);

/**
* Triggers fly to on the next map change
*/
useEffect(() => {
if (shouldFlyTo && currentMapCache.mapInstance) {
currentMapCache.mapInstance.flyTo(shouldFlyTo);
setShouldFlyTo(null);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentMapCache.mapInstance]);

const openDatasetFromMapIcon = async (
mapId: string | null,
coordinates: number[] | null
Expand Down Expand Up @@ -61,14 +73,15 @@ const DataRow: React.FC<RowProps> = ({ row }) => {
// Open the map
const ifSwitched = changeToOrOpenNewTab(datasetToOpenTransformed);
// If provided fly to the coordinates
if (
ifSwitched &&
coordinates &&
coordinates.length === 2 &&
currentMapCache.mapInstance
) {
if (coordinates && coordinates.length === 2) {
const latLng = new LatLng(coordinates[0], coordinates[1]);
currentMapCache.mapInstance.flyTo(latLng);
if (ifSwitched) {
setShouldFlyTo(latLng);
} else {
if (currentMapCache.mapInstance) {
currentMapCache.mapInstance.flyTo(latLng);
}
}
}
} else {
// Display alert
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/contexts/TabsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,28 @@ export const TabsContextProvider: React.FC<TabsContextProviderProps> = ({
/**
* Opens a new tab if necessary and/or switched to already existing one.
* @param dataset dataset to change to or open
* @return if switched
*/
const changeToOrOpenNewTab = (dataset: Dataset) => {
// Open the tab if it does not exist
if (
!currentTabsCache.openedTabs.some((tab) => tab.dataset.id === dataset.id)
) {
openNewTab(dataset);
return true;
} else {
// Switch to that tab
const tabID = currentTabsCache.openedTabs.find((tab) => {
tab.dataset.id === dataset.id;
return tab.dataset.id === dataset.id;
});
if (tabID) {
setCurrentTabsCache({
...currentTabsCache,
currentTabID: tabID.id,
});
}
return false;
}
return true;
};

const value = {
Expand Down

0 comments on commit c40ce24

Please sign in to comment.