forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {Linking} from 'react-native'
2import {useCameraPermissions as useExpoCameraPermissions} from 'expo-camera'
3import * as MediaLibrary from 'expo-media-library'
4
5import {Alert} from '#/view/com/util/Alert'
6import {IS_WEB} from '#/env'
7
8const openPermissionAlert = (perm: string) => {
9 Alert.alert(
10 'Permission needed',
11 `Bluesky does not have permission to access your ${perm}.`,
12 [
13 {
14 text: 'Cancel',
15 style: 'cancel',
16 },
17 {text: 'Open Settings', onPress: () => Linking.openSettings()},
18 ],
19 )
20}
21
22export function usePhotoLibraryPermission() {
23 const [res, requestPermission] = MediaLibrary.usePermissions({
24 granularPermissions: ['photo'],
25 })
26 const requestPhotoAccessIfNeeded = async () => {
27 // On the, we use <input type="file"> to produce a filepicker
28 // This does not need any permission granting.
29 if (IS_WEB) {
30 return true
31 }
32
33 if (res?.granted) {
34 return true
35 } else if (!res || res.status === 'undetermined' || res?.canAskAgain) {
36 const {canAskAgain, granted, status} = await requestPermission()
37
38 if (!canAskAgain && status === 'undetermined') {
39 openPermissionAlert('photo library')
40 }
41
42 return granted
43 } else {
44 openPermissionAlert('photo library')
45 return false
46 }
47 }
48 return {requestPhotoAccessIfNeeded}
49}
50
51export function useVideoLibraryPermission() {
52 const [res, requestPermission] = MediaLibrary.usePermissions({
53 granularPermissions: ['video'],
54 })
55 const requestVideoAccessIfNeeded = async () => {
56 // On the, we use <input type="file"> to produce a filepicker
57 // This does not need any permission granting.
58 if (IS_WEB) {
59 return true
60 }
61
62 if (res?.granted) {
63 return true
64 } else if (!res || res.status === 'undetermined' || res?.canAskAgain) {
65 const {canAskAgain, granted, status} = await requestPermission()
66
67 if (!canAskAgain && status === 'undetermined') {
68 openPermissionAlert('video library')
69 }
70
71 return granted
72 } else {
73 openPermissionAlert('video library')
74 return false
75 }
76 }
77 return {requestVideoAccessIfNeeded}
78}
79
80export function useCameraPermission() {
81 const [res, requestPermission] = useExpoCameraPermissions()
82
83 const requestCameraAccessIfNeeded = async () => {
84 if (res?.granted) {
85 return true
86 } else if (!res || res?.status === 'undetermined' || res?.canAskAgain) {
87 const updatedRes = await requestPermission()
88 return updatedRes?.granted
89 } else {
90 openPermissionAlert('camera')
91 return false
92 }
93 }
94
95 return {requestCameraAccessIfNeeded}
96}