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