From 6d1973b3628680cd0c0dd26de62d20f8ad24f62d Mon Sep 17 00:00:00 2001 From: Fedi Rajhi Date: Fri, 21 Jun 2024 13:14:38 +0100 Subject: [PATCH] chore: Avoid nagivating to same location --- src/components/MapView/MapView.website.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/MapView/MapView.website.tsx b/src/components/MapView/MapView.website.tsx index bfa923fa2af4..51bf49f5b009 100644 --- a/src/components/MapView/MapView.website.tsx +++ b/src/components/MapView/MapView.website.tsx @@ -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'; @@ -56,6 +57,7 @@ const MapView = forwardRef( const [mapRef, setMapRef] = useState(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(false); const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []); @@ -110,11 +112,16 @@ const MapView = forwardRef( 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) {