@@ -17,10 +17,11 @@ import {
1717 View
1818} from 'react-native' ;
1919
20+ import { NATIVE } from './../wrapper' ;
2021import { sentryLogo } from './branding' ;
2122import { defaultConfiguration } from './defaults' ;
2223import defaultStyles from './FeedbackForm.styles' ;
23- import type { FeedbackFormProps , FeedbackFormState , FeedbackFormStyles , FeedbackGeneralConfiguration , FeedbackTextConfiguration } from './FeedbackForm.types' ;
24+ import type { FeedbackFormProps , FeedbackFormState , FeedbackFormStyles , FeedbackGeneralConfiguration , FeedbackTextConfiguration , ImagePickerConfiguration } from './FeedbackForm.types' ;
2425import { isValidEmail } from './utils' ;
2526
2627/**
@@ -100,12 +101,50 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
100101 }
101102 } ;
102103
103- public onScreenshotButtonPress : ( ) => void = ( ) => {
104+ public onScreenshotButtonPress : ( ) => void = async ( ) => {
104105 if ( ! this . state . filename && ! this . state . attachment ) {
105- const { onAddScreenshot } = { ...defaultConfiguration , ...this . props } ;
106- onAddScreenshot ( ( filename : string , attachement : Uint8Array ) => {
107- this . setState ( { filename, attachment : attachement } ) ;
108- } ) ;
106+ const imagePickerConfiguration : ImagePickerConfiguration = this . props ;
107+ if ( imagePickerConfiguration . imagePicker ) {
108+ const launchImageLibrary = imagePickerConfiguration . imagePicker . launchImageLibraryAsync
109+ // expo-image-picker library is available
110+ ? ( ) => imagePickerConfiguration . imagePicker . launchImageLibraryAsync ( { mediaTypes : [ 'images' ] } )
111+ // react-native-image-picker library is available
112+ : imagePickerConfiguration . imagePicker . launchImageLibrary
113+ ? ( ) => imagePickerConfiguration . imagePicker . launchImageLibrary ( { mediaType : 'photo' } )
114+ : null ;
115+ if ( ! launchImageLibrary ) {
116+ logger . warn ( 'No compatible image picker library found. Please provide a valid image picker library.' ) ;
117+ if ( __DEV__ ) {
118+ Alert . alert (
119+ 'Development note' ,
120+ 'No compatible image picker library found. Please provide a compatible version of `expo-image-picker` or `react-native-image-picker`.' ,
121+ ) ;
122+ }
123+ return ;
124+ }
125+
126+ const result = await launchImageLibrary ( ) ;
127+ if ( result . assets && result . assets . length > 0 ) {
128+ const filename = result . assets [ 0 ] . fileName ;
129+ const imageUri = result . assets [ 0 ] . uri ;
130+ NATIVE . getDataFromUri ( imageUri ) . then ( ( data ) => {
131+ if ( data != null ) {
132+ this . setState ( { filename, attachment : data } ) ;
133+ } else {
134+ logger . error ( 'Failed to read image data from uri:' , imageUri ) ;
135+ }
136+ } )
137+ . catch ( ( error ) => {
138+ logger . error ( 'Failed to read image data from uri:' , imageUri , 'error: ' , error ) ;
139+ } ) ;
140+ }
141+ } else {
142+ // Defaulting to the onAddScreenshot callback
143+ const { onAddScreenshot } = { ...defaultConfiguration , ...this . props } ;
144+ onAddScreenshot ( ( filename : string , attachement : Uint8Array ) => {
145+ this . setState ( { filename, attachment : attachement } ) ;
146+ } ) ;
147+ }
109148 } else {
110149 this . setState ( { filename : undefined , attachment : undefined } ) ;
111150 }
@@ -118,6 +157,7 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
118157 const { name, email, description } = this . state ;
119158 const { onFormClose } = this . props ;
120159 const config : FeedbackGeneralConfiguration = this . props ;
160+ const imagePickerConfiguration : ImagePickerConfiguration = this . props ;
121161 const text : FeedbackTextConfiguration = this . props ;
122162 const styles : FeedbackFormStyles = { ...defaultStyles , ...this . props . styles } ;
123163 const onCancel = ( ) : void => {
@@ -191,7 +231,7 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
191231 onChangeText = { ( value ) => this . setState ( { description : value } ) }
192232 multiline
193233 />
194- { config . enableScreenshot && (
234+ { ( config . enableScreenshot || imagePickerConfiguration . imagePicker ) && (
195235 < TouchableOpacity style = { styles . screenshotButton } onPress = { this . onScreenshotButtonPress } >
196236 < Text style = { styles . screenshotText } >
197237 { ! this . state . filename && ! this . state . attachment
0 commit comments