|
| 1 | +import type { SendFeedbackParams } from '@sentry/core'; |
| 2 | +import { captureFeedback, getCurrentScope, lastEventId } from '@sentry/core'; |
| 3 | +import * as React from 'react'; |
| 4 | +import type { KeyboardTypeOptions } from 'react-native'; |
| 5 | +import { |
| 6 | + Alert, |
| 7 | + Image, |
| 8 | + Keyboard, |
| 9 | + KeyboardAvoidingView, |
| 10 | + SafeAreaView, |
| 11 | + ScrollView, |
| 12 | + Text, |
| 13 | + TextInput, |
| 14 | + TouchableOpacity, |
| 15 | + TouchableWithoutFeedback, |
| 16 | + View |
| 17 | +} from 'react-native'; |
| 18 | + |
| 19 | +import { sentryLogo } from './branding'; |
| 20 | +import { defaultConfiguration } from './defaults'; |
| 21 | +import defaultStyles from './FeedbackForm.styles'; |
| 22 | +import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types'; |
| 23 | + |
| 24 | +/** |
| 25 | + * @beta |
| 26 | + * Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback. |
| 27 | + */ |
| 28 | +export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFormState> { |
| 29 | + public static defaultProps: Partial<FeedbackFormProps> = { |
| 30 | + ...defaultConfiguration |
| 31 | + } |
| 32 | + |
| 33 | + public constructor(props: FeedbackFormProps) { |
| 34 | + super(props); |
| 35 | + |
| 36 | + const currentUser = { |
| 37 | + useSentryUser: { |
| 38 | + email: this.props?.useSentryUser?.email || getCurrentScope()?.getUser()?.email || '', |
| 39 | + name: this.props?.useSentryUser?.name || getCurrentScope()?.getUser()?.name || '', |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + this.state = { |
| 44 | + isVisible: true, |
| 45 | + name: currentUser.useSentryUser.name, |
| 46 | + email: currentUser.useSentryUser.email, |
| 47 | + description: '', |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + public handleFeedbackSubmit: () => void = () => { |
| 52 | + const { name, email, description } = this.state; |
| 53 | + const { onFormClose } = this.props; |
| 54 | + const text: FeedbackTextConfiguration = this.props; |
| 55 | + |
| 56 | + const trimmedName = name?.trim(); |
| 57 | + const trimmedEmail = email?.trim(); |
| 58 | + const trimmedDescription = description?.trim(); |
| 59 | + |
| 60 | + if ((this.props.isNameRequired && !trimmedName) || (this.props.isEmailRequired && !trimmedEmail) || !trimmedDescription) { |
| 61 | + Alert.alert(text.errorTitle, text.formError); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (this.props.shouldValidateEmail && (this.props.isEmailRequired || trimmedEmail.length > 0) && !this._isValidEmail(trimmedEmail)) { |
| 66 | + Alert.alert(text.errorTitle, text.emailError); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const eventId = lastEventId(); |
| 71 | + const userFeedback: SendFeedbackParams = { |
| 72 | + message: trimmedDescription, |
| 73 | + name: trimmedName, |
| 74 | + email: trimmedEmail, |
| 75 | + associatedEventId: eventId, |
| 76 | + }; |
| 77 | + |
| 78 | + onFormClose(); |
| 79 | + this.setState({ isVisible: false }); |
| 80 | + |
| 81 | + captureFeedback(userFeedback); |
| 82 | + Alert.alert(text.successMessageText); |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * Renders the feedback form screen. |
| 87 | + */ |
| 88 | + public render(): React.ReactNode { |
| 89 | + const { name, email, description } = this.state; |
| 90 | + const { onFormClose } = this.props; |
| 91 | + const config: FeedbackGeneralConfiguration = this.props; |
| 92 | + const text: FeedbackTextConfiguration = this.props; |
| 93 | + const styles: FeedbackFormStyles = { ...defaultStyles, ...this.props.styles }; |
| 94 | + const onCancel = (): void => { |
| 95 | + onFormClose(); |
| 96 | + this.setState({ isVisible: false }); |
| 97 | + } |
| 98 | + |
| 99 | + if (!this.state.isVisible) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <SafeAreaView style={[styles.container, { padding: 0 }]}> |
| 105 | + <KeyboardAvoidingView behavior={'padding'} style={[styles.container, { padding: 0 }]}> |
| 106 | + <ScrollView> |
| 107 | + <TouchableWithoutFeedback onPress={Keyboard.dismiss}> |
| 108 | + <View style={styles.container}> |
| 109 | + <View style={styles.titleContainer}> |
| 110 | + <Text style={styles.title}>{text.formTitle}</Text> |
| 111 | + {config.showBranding && ( |
| 112 | + <Image |
| 113 | + source={{ uri: sentryLogo }} |
| 114 | + style={styles.sentryLogo} |
| 115 | + testID='sentry-logo' |
| 116 | + /> |
| 117 | + )} |
| 118 | + </View> |
| 119 | + |
| 120 | + {config.showName && ( |
| 121 | + <> |
| 122 | + <Text style={styles.label}> |
| 123 | + {text.nameLabel} |
| 124 | + {config.isNameRequired && ` ${text.isRequiredLabel}`} |
| 125 | + </Text> |
| 126 | + <TextInput |
| 127 | + style={styles.input} |
| 128 | + placeholder={text.namePlaceholder} |
| 129 | + value={name} |
| 130 | + onChangeText={(value) => this.setState({ name: value })} |
| 131 | + /> |
| 132 | + </> |
| 133 | + )} |
| 134 | + |
| 135 | + {config.showEmail && ( |
| 136 | + <> |
| 137 | + <Text style={styles.label}> |
| 138 | + {text.emailLabel} |
| 139 | + {config.isEmailRequired && ` ${text.isRequiredLabel}`} |
| 140 | + </Text> |
| 141 | + <TextInput |
| 142 | + style={styles.input} |
| 143 | + placeholder={text.emailPlaceholder} |
| 144 | + keyboardType={'email-address' as KeyboardTypeOptions} |
| 145 | + value={email} |
| 146 | + onChangeText={(value) => this.setState({ email: value })} |
| 147 | + /> |
| 148 | + </> |
| 149 | + )} |
| 150 | + |
| 151 | + <Text style={styles.label}> |
| 152 | + {text.messageLabel} |
| 153 | + {` ${text.isRequiredLabel}`} |
| 154 | + </Text> |
| 155 | + <TextInput |
| 156 | + style={[styles.input, styles.textArea]} |
| 157 | + placeholder={text.messagePlaceholder} |
| 158 | + value={description} |
| 159 | + onChangeText={(value) => this.setState({ description: value })} |
| 160 | + multiline |
| 161 | + /> |
| 162 | + |
| 163 | + <TouchableOpacity style={styles.submitButton} onPress={this.handleFeedbackSubmit}> |
| 164 | + <Text style={styles.submitText}>{text.submitButtonLabel}</Text> |
| 165 | + </TouchableOpacity> |
| 166 | + |
| 167 | + <TouchableOpacity style={styles.cancelButton} onPress={onCancel}> |
| 168 | + <Text style={styles.cancelText}>{text.cancelButtonLabel}</Text> |
| 169 | + </TouchableOpacity> |
| 170 | + </View> |
| 171 | + </TouchableWithoutFeedback> |
| 172 | + </ScrollView> |
| 173 | + </KeyboardAvoidingView> |
| 174 | + </SafeAreaView> |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + private _isValidEmail = (email: string): boolean => { |
| 179 | + const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ |
| 180 | + return emailRegex.test(email); |
| 181 | + }; |
| 182 | +} |
0 commit comments