Skip to content

Commit

Permalink
chore: Avoid nagivating to same location
Browse files Browse the repository at this point in the history
  • Loading branch information
fedirjh committed Jun 21, 2024
1 parent f31abe9 commit 6d1973b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/components/MapView/MapView.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {GeolocationErrorCallback} from '@libs/getCurrentPosition/getCurrent
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types';
import * as UserLocation from '@userActions/UserLocation';
import CONST from '@src/CONST';
import usePrevious from '@hooks/usePrevious';
import useLocalize from '@src/hooks/useLocalize';
import useNetwork from '@src/hooks/useNetwork';
import getCurrentPosition from '@src/libs/getCurrentPosition';
Expand Down Expand Up @@ -56,6 +57,7 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
const [mapRef, setMapRef] = useState<MapRef | null>(null);
const initialLocation = useMemo(() => ({longitude: initialState.location[0], latitude: initialState.location[1]}), [initialState]);
const currentPosition = userLocation ?? initialLocation;
const prevUserPosition = usePrevious(currentPosition);
const [userInteractedWithMap, setUserInteractedWithMap] = useState(false);
const [shouldResetBoundaries, setShouldResetBoundaries] = useState<boolean>(false);
const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []);
Expand Down Expand Up @@ -110,11 +112,16 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
return;
}

// Avoid navigationg to the same location
if (prevUserPosition.longitude === currentPosition.longitude && prevUserPosition.latitude === currentPosition.latitude) {
return;
}

mapRef.flyTo({
center: [currentPosition.longitude, currentPosition.latitude],
zoom: CONST.MAPBOX.DEFAULT_ZOOM,
});
}, [currentPosition, userInteractedWithMap, mapRef, shouldPanMapToCurrentPosition]);
}, [currentPosition, mapRef, prevUserPosition, shouldPanMapToCurrentPosition]);

const resetBoundaries = useCallback(() => {
if (!waypoints || waypoints.length === 0) {
Expand Down

0 comments on commit 6d1973b

Please sign in to comment.