@@ -17,10 +17,11 @@ import {
17
17
View
18
18
} from 'react-native' ;
19
19
20
+ import { NATIVE } from './../wrapper' ;
20
21
import { sentryLogo } from './branding' ;
21
22
import { defaultConfiguration } from './defaults' ;
22
23
import 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' ;
24
25
import { isValidEmail } from './utils' ;
25
26
26
27
/**
@@ -100,12 +101,50 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
100
101
}
101
102
} ;
102
103
103
- public onScreenshotButtonPress : ( ) => void = ( ) => {
104
+ public onScreenshotButtonPress : ( ) => void = async ( ) => {
104
105
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
+ }
109
148
} else {
110
149
this . setState ( { filename : undefined , attachment : undefined } ) ;
111
150
}
@@ -118,6 +157,7 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
118
157
const { name, email, description } = this . state ;
119
158
const { onFormClose } = this . props ;
120
159
const config : FeedbackGeneralConfiguration = this . props ;
160
+ const imagePickerConfiguration : ImagePickerConfiguration = this . props ;
121
161
const text : FeedbackTextConfiguration = this . props ;
122
162
const styles : FeedbackFormStyles = { ...defaultStyles , ...this . props . styles } ;
123
163
const onCancel = ( ) : void => {
@@ -191,7 +231,7 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
191
231
onChangeText = { ( value ) => this . setState ( { description : value } ) }
192
232
multiline
193
233
/>
194
- { config . enableScreenshot && (
234
+ { ( config . enableScreenshot || imagePickerConfiguration . imagePicker ) && (
195
235
< TouchableOpacity style = { styles . screenshotButton } onPress = { this . onScreenshotButtonPress } >
196
236
< Text style = { styles . screenshotText } >
197
237
{ ! this . state . filename && ! this . state . attachment
0 commit comments