All files / apps/chatbot/src/utils permissions.ts

100% Statements 23/23
100% Branches 14/14
100% Functions 5/5
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69          1x 10x     1x 2x                   1x             1x 10x 10x 10x   9x 4x     5x 2x       2x     3x   3x 1x     2x 1x     1x         3x     1x       1x    
import { check, openSettings, PERMISSIONS, request, RESULTS } from "react-native-permissions";
import { Alert, Platform } from "react-native";
 
import sentryService from "@repo/services/sentryService";
 
const getCameraPermission = () => {
  return Platform.OS === "ios" ? PERMISSIONS.IOS.CAMERA : PERMISSIONS.ANDROID.CAMERA;
};
 
const showBlockedAlert = (title: string, message: string) => {
  Alert.alert(
    title,
    message,
    [
      {
        text: "Cancel",
        style: "cancel",
      },
      {
        text: "Open Settings",
        onPress: () => openSettings(),
      },
    ],
    { cancelable: true },
  );
};
 
export const requestCameraPermission = async () => {
  const permission = getCameraPermission();
  try {
    const initialStatus = await check(permission);
 
    if (initialStatus === RESULTS.GRANTED || initialStatus === RESULTS.LIMITED) {
      return true;
    }
 
    if (initialStatus === RESULTS.BLOCKED) {
      showBlockedAlert(
        "Camera Permission",
        "Camera access is required to take photos. Please enable it in the app settings.",
      );
      return false;
    }
 
    const status = await request(permission);
 
    if (status === RESULTS.GRANTED || status === RESULTS.LIMITED) {
      return true;
    }
 
    if (status === RESULTS.UNAVAILABLE) {
      Alert.alert("Camera Unavailable", "Camera is not available on this device.");
    }
  } catch (error) {
    sentryService.captureException(error, {
      tags: { component: "chatbot", action: "camera_permission" },
    });
  }
 
  return false;
};
 
export const requestPhotoLibraryPermission = async () => {
  // With modern React Native and react-native-image-picker, we rely on the out-of-process
  // system pickers (PHPickerViewController on iOS 14+, Photo Picker/ACTION_GET_CONTENT on Android).
  // These do not require the app to explicitly declare or request library permissions.
  return true;
};