From e4e263cc9a488bcd38d25b23b4984b4006206396 Mon Sep 17 00:00:00 2001 From: Maciej Dobosz Date: Tue, 28 Nov 2023 19:53:46 +0100 Subject: [PATCH 1/3] Only once executed getCurrentPosition() --- src/components/MapView/MapView.tsx | 8 ++++++++ src/components/MapView/MapView.web.tsx | 12 ++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/components/MapView/MapView.tsx b/src/components/MapView/MapView.tsx index db3e076eacca..17653aa0597a 100644 --- a/src/components/MapView/MapView.tsx +++ b/src/components/MapView/MapView.tsx @@ -28,6 +28,7 @@ const MapView = forwardRef( const [isIdle, setIsIdle] = useState(false); const [currentPosition, setCurrentPosition] = useState(cachedUserLocation); const [userInteractedWithMap, setUserInteractedWithMap] = useState(false); + const hasStartedGettingCurrentPosition = useRef(false); useFocusEffect( useCallback(() => { @@ -35,6 +36,13 @@ const MapView = forwardRef( return; } + if (hasStartedGettingCurrentPosition.current) { + return; + } + + hasStartedGettingCurrentPosition.current = true; + console.log('Called once: hasStartedGettingCurrentPosition.current') + getCurrentPosition( (params) => { const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude}; diff --git a/src/components/MapView/MapView.web.tsx b/src/components/MapView/MapView.web.tsx index 1880049b3542..f438ce9f91f8 100644 --- a/src/components/MapView/MapView.web.tsx +++ b/src/components/MapView/MapView.web.tsx @@ -5,7 +5,7 @@ import {useFocusEffect} from '@react-navigation/native'; import mapboxgl from 'mapbox-gl'; import 'mapbox-gl/dist/mapbox-gl.css'; -import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useState} from 'react'; +import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react'; import Map, {MapRef, Marker} from 'react-map-gl'; import {View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; @@ -47,6 +47,7 @@ const MapView = forwardRef( const [currentPosition, setCurrentPosition] = useState(cachedUserLocation); const [userInteractedWithMap, setUserInteractedWithMap] = useState(false); const [shouldResetBoundaries, setShouldResetBoundaries] = useState(false); + const hasStartedGettingCurrentPosition = useRef(false); const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []); useFocusEffect( @@ -55,6 +56,13 @@ const MapView = forwardRef( return; } + if (hasStartedGettingCurrentPosition.current) { + return; + } + + hasStartedGettingCurrentPosition.current = true; + console.log('Called once: hasStartedGettingCurrentPosition.current') + getCurrentPosition( (params) => { const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude}; @@ -69,7 +77,7 @@ const MapView = forwardRef( setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]}); }, ); - }, [cachedUserLocation, isOffline, initialState.location]), + }, [cachedUserLocation, isOffline, initialState.location]) ); // Determines if map can be panned to user's detected From 8004eb5b0cb7c406305861a73892ecfc91456b97 Mon Sep 17 00:00:00 2001 From: Maciej Dobosz Date: Tue, 28 Nov 2023 19:54:41 +0100 Subject: [PATCH 2/3] Remove console logs --- src/components/MapView/MapView.tsx | 1 - src/components/MapView/MapView.web.tsx | 1 - 2 files changed, 2 deletions(-) diff --git a/src/components/MapView/MapView.tsx b/src/components/MapView/MapView.tsx index 17653aa0597a..7b63836efa66 100644 --- a/src/components/MapView/MapView.tsx +++ b/src/components/MapView/MapView.tsx @@ -41,7 +41,6 @@ const MapView = forwardRef( } hasStartedGettingCurrentPosition.current = true; - console.log('Called once: hasStartedGettingCurrentPosition.current') getCurrentPosition( (params) => { diff --git a/src/components/MapView/MapView.web.tsx b/src/components/MapView/MapView.web.tsx index f438ce9f91f8..47146e1ed8d9 100644 --- a/src/components/MapView/MapView.web.tsx +++ b/src/components/MapView/MapView.web.tsx @@ -61,7 +61,6 @@ const MapView = forwardRef( } hasStartedGettingCurrentPosition.current = true; - console.log('Called once: hasStartedGettingCurrentPosition.current') getCurrentPosition( (params) => { From 367e58e2150a0c8a372506401004920e6103b7ba Mon Sep 17 00:00:00 2001 From: Maciej Dobosz Date: Tue, 28 Nov 2023 20:08:43 +0100 Subject: [PATCH 3/3] Restore missing coma --- src/components/MapView/MapView.web.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/MapView/MapView.web.tsx b/src/components/MapView/MapView.web.tsx index 47146e1ed8d9..d34b8f1ac4d9 100644 --- a/src/components/MapView/MapView.web.tsx +++ b/src/components/MapView/MapView.web.tsx @@ -76,7 +76,7 @@ const MapView = forwardRef( setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]}); }, ); - }, [cachedUserLocation, isOffline, initialState.location]) + }, [cachedUserLocation, isOffline, initialState.location]), ); // Determines if map can be panned to user's detected