Skip to content

Commit

Permalink
Merge pull request #33157 from software-mansion-labs/fix/double-conse…
Browse files Browse the repository at this point in the history
…nt-ask
  • Loading branch information
thienlnam authored Dec 20, 2023
2 parents ad72213 + a459d90 commit fdf5d68
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
const [isIdle, setIsIdle] = useState(false);
const [currentPosition, setCurrentPosition] = useState(cachedUserLocation);
const [userInteractedWithMap, setUserInteractedWithMap] = useState(false);
const hasAskedForLocationPermission = useRef(false);

useFocusEffect(
useCallback(() => {
if (isOffline) {
return;
}

if (hasAskedForLocationPermission.current) {
return;
}

hasAskedForLocationPermission.current = true;
getCurrentPosition(
(params) => {
const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude};
Expand Down
13 changes: 10 additions & 3 deletions src/components/MapView/MapView.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -52,28 +52,35 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
const [userInteractedWithMap, setUserInteractedWithMap] = useState(false);
const [shouldResetBoundaries, setShouldResetBoundaries] = useState<boolean>(false);
const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []);
const hasAskedForLocationPermission = useRef(false);

useFocusEffect(
useCallback(() => {
if (isOffline) {
return;
}

if (hasAskedForLocationPermission.current) {
return;
}

hasAskedForLocationPermission.current = true;
getCurrentPosition(
(params) => {
const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude};
setCurrentPosition(currentCoords);
setUserLocation(currentCoords);
},
() => {
if (cachedUserLocation) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (cachedUserLocation || !initialState) {
return;
}

setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]});
},
);
}, [cachedUserLocation, isOffline, initialState.location]),
}, [cachedUserLocation, initialState, isOffline]),
);

// Determines if map can be panned to user's detected
Expand Down

0 comments on commit fdf5d68

Please sign in to comment.