about summary refs log tree commit diff
path: root/src/state/geolocation/useRequestDeviceLocation.ts
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2025-09-04 11:07:12 -0500
committerGitHub <noreply@github.com>2025-09-04 11:07:12 -0500
commitf8ae0540a062e6346baf9fbf0481f769fb23a120 (patch)
treefc888e55258169e8e7246a1c099aab78fc7d9c99 /src/state/geolocation/useRequestDeviceLocation.ts
parent625b4e61dbf11c1d485bf8e8265df4d5af0c9657 (diff)
downloadvoidsky-f8ae0540a062e6346baf9fbf0481f769fb23a120.tar.zst
Provide geo-gated users optional GPS fallback for precise location data (#8973)
Diffstat (limited to 'src/state/geolocation/useRequestDeviceLocation.ts')
-rw-r--r--src/state/geolocation/useRequestDeviceLocation.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/state/geolocation/useRequestDeviceLocation.ts b/src/state/geolocation/useRequestDeviceLocation.ts
new file mode 100644
index 000000000..64e05b056
--- /dev/null
+++ b/src/state/geolocation/useRequestDeviceLocation.ts
@@ -0,0 +1,43 @@
+import {useCallback} from 'react'
+import * as Location from 'expo-location'
+
+import {type DeviceLocation} from '#/state/geolocation/types'
+import {getDeviceGeolocation} from '#/state/geolocation/util'
+
+export {PermissionStatus} from 'expo-location'
+
+export function useRequestDeviceLocation(): () => Promise<
+  | {
+      granted: true
+      location: DeviceLocation | undefined
+    }
+  | {
+      granted: false
+      status: {
+        canAskAgain: boolean
+        /**
+         * Enum, use `PermissionStatus` export for comparisons
+         */
+        permissionStatus: Location.PermissionStatus
+      }
+    }
+> {
+  return useCallback(async () => {
+    const status = await Location.requestForegroundPermissionsAsync()
+
+    if (status.granted) {
+      return {
+        granted: true,
+        location: await getDeviceGeolocation(),
+      }
+    } else {
+      return {
+        granted: false,
+        status: {
+          canAskAgain: status.canAskAgain,
+          permissionStatus: status.status,
+        },
+      }
+    }
+  }, [])
+}