diff options
author | Eric Bailey <git@esb.lol> | 2025-09-04 11:07:12 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-09-04 11:07:12 -0500 |
commit | f8ae0540a062e6346baf9fbf0481f769fb23a120 (patch) | |
tree | fc888e55258169e8e7246a1c099aab78fc7d9c99 /src/state/geolocation/useRequestDeviceLocation.ts | |
parent | 625b4e61dbf11c1d485bf8e8265df4d5af0c9657 (diff) | |
download | voidsky-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.ts | 43 |
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, + }, + } + } + }, []) +} |