+36
-1
src/state/geolocation/useSyncedDeviceGeolocation.ts
+36
-1
src/state/geolocation/useSyncedDeviceGeolocation.ts
···
1
1
import {useEffect, useRef} from 'react'
2
2
import * as Location from 'expo-location'
3
+
import {createPermissionHook} from 'expo-modules-core'
3
4
4
5
import {logger} from '#/state/geolocation/logger'
5
6
import {getDeviceGeolocation} from '#/state/geolocation/util'
6
7
import {device, useStorage} from '#/storage'
7
8
8
9
/**
10
+
* Location.useForegroundPermissions on web just errors if the navigator.permissions API is not available.
11
+
* We need to catch and ignore it, since it's effectively denied.
12
+
* @see https://github.com/expo/expo/blob/72f1562ed9cce5ff6dfe04aa415b71632a3d4b87/packages/expo-location/src/Location.ts#L290-L293
13
+
*/
14
+
const useForegroundPermissions = createPermissionHook({
15
+
getMethod: () =>
16
+
Location.getForegroundPermissionsAsync().catch(error => {
17
+
logger.debug(
18
+
'useForegroundPermission: error getting location permissions',
19
+
{safeMessage: error},
20
+
)
21
+
return {
22
+
status: Location.PermissionStatus.DENIED,
23
+
granted: false,
24
+
canAskAgain: false,
25
+
expires: 0,
26
+
}
27
+
}),
28
+
requestMethod: () =>
29
+
Location.requestForegroundPermissionsAsync().catch(error => {
30
+
logger.debug(
31
+
'useForegroundPermission: error requesting location permissions',
32
+
{safeMessage: error},
33
+
)
34
+
return {
35
+
status: Location.PermissionStatus.DENIED,
36
+
granted: false,
37
+
canAskAgain: false,
38
+
expires: 0,
39
+
}
40
+
}),
41
+
})
42
+
43
+
/**
9
44
* Hook to get and sync the device geolocation from the device GPS and store it
10
45
* using device storage. If permissions are not granted, it will clear any cached
11
46
* storage value.
12
47
*/
13
48
export function useSyncedDeviceGeolocation() {
14
49
const synced = useRef(false)
15
-
const [status] = Location.useForegroundPermissions()
50
+
const [status] = useForegroundPermissions()
16
51
const [deviceGeolocation, setDeviceGeolocation] = useStorage(device, [
17
52
'deviceGeolocation',
18
53
])