/
SupportContact.tsx
270 lines (257 loc) · 8.72 KB
/
SupportContact.tsx
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { anonymizedPhone } from '@celo/base/lib/phoneNumbers'
import { NativeStackScreenProps } from '@react-navigation/native-stack'
import React, { useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ActivityIndicator, Platform, ScrollView, StyleSheet, Text, View } from 'react-native'
import DeviceInfo from 'react-native-device-info'
import { e164NumberSelector, nameSelector } from 'src/account/selectors'
import { sendSupportRequest } from 'src/account/zendesk'
import { showMessage } from 'src/alert/actions'
import {
multichainBetaStatusSelector,
phoneNumberVerifiedSelector,
sessionIdSelector,
} from 'src/app/selectors'
import { APP_NAME } from 'src/brandingConfig'
import Button, { BtnTypes } from 'src/components/Button'
import KeyboardSpacer from 'src/components/KeyboardSpacer'
import Switch from 'src/components/Switch'
import TextInput from 'src/components/TextInput'
import { DEFAULT_TESTNET } from 'src/config'
import { navigateBack } from 'src/navigator/NavigationService'
import { Screens } from 'src/navigator/Screens'
import { StackParamList } from 'src/navigator/types'
import { userLocationDataSelector } from 'src/networkInfo/selectors'
import { hooksPreviewApiUrlSelector } from 'src/positions/selectors'
import { useDispatch, useSelector } from 'src/redux/hooks'
import colors from 'src/styles/colors'
import fontStyles from 'src/styles/fonts'
import Logger from 'src/utils/Logger'
import { currentAccountSelector } from 'src/web3/selectors'
type Props = NativeStackScreenProps<StackParamList, Screens.SupportContact>
// Language agnostic loose regex for email validation
const tester =
/^[-!#$%&'*+/0-9=?A-Z^_a-z`{|}~\u0E00-\u0E7F](\.?[-!#$%&'*+/0-9=?A-Z^_a-z`{|}~\u0E00-\u0E7F])*@[A-Za-z0-9\u0E00-\u0E7F](-*\.?[A-Za-z0-9\u0E00-\u0E7F])*\.[A-Za-z\u0E00-\u0E7F](-?[A-Za-z0-9\u0E00-\u0E7F])+$/
// Validate email function - https://github.com/manishsaraan/email-validator
export function validateEmail(email: string) {
if (!email) return false
if (email.length > 360) return false
const emailParts = email.split('@')
if (emailParts.length !== 2) return false
const account = emailParts[0]
const address = emailParts[1]
if (account.length > 64) return false
else if (address.length > 255) return false
const domainParts = address.split('.')
if (!domainParts[1] || domainParts[1].length < 2) return false
if (
domainParts.some((part) => {
return part.length > 63
})
) {
return false
}
return tester.test(email)
}
function SupportContact({ route }: Props) {
const { t } = useTranslation()
const cachedName = useSelector(nameSelector)
const [message, setMessage] = useState('')
const [email, setEmail] = useState('')
const [name, setName] = useState(cachedName ?? '')
const [attachLogs, setAttachLogs] = useState(true)
const [inProgress, setInProgress] = useState(false)
const e164PhoneNumber = useSelector(e164NumberSelector)
const currentAccount = useSelector(currentAccountSelector)
const sessionId = useSelector(sessionIdSelector)
const numberVerifiedCentralized = useSelector(phoneNumberVerifiedSelector)
const multichainBetaStatus = useSelector(multichainBetaStatusSelector)
const { countryCodeAlpha2: country, region } = useSelector(userLocationDataSelector)
const hooksPreviewApiUrl = useSelector(hooksPreviewApiUrlSelector)
const dispatch = useDispatch()
const prefilledText = route.params?.prefilledText
useEffect(() => {
if (prefilledText) {
setMessage(prefilledText)
}
}, [prefilledText])
const navigateBackAndToast = () => {
navigateBack()
dispatch(showMessage(t('contactSuccess')))
}
const onPressSendEmail = useCallback(async () => {
setInProgress(true)
const deviceInfo = {
version: DeviceInfo.getVersion(),
systemVersion: DeviceInfo.getSystemVersion(),
buildNumber: DeviceInfo.getBuildNumber(),
apiLevel: DeviceInfo.getApiLevelSync(),
os: Platform.OS,
country,
region,
deviceId: DeviceInfo.getDeviceId(),
deviceBrand: DeviceInfo.getBrand(),
deviceModel: DeviceInfo.getModel(),
address: currentAccount,
sessionId,
numberVerifiedCentralized,
multichainBetaStatus,
hooksPreviewEnabled: !!hooksPreviewApiUrl,
network: DEFAULT_TESTNET,
}
const userId = e164PhoneNumber ? anonymizedPhone(e164PhoneNumber) : t('unknown')
const attachments = attachLogs ? await Logger.getLogsToAttach() : []
try {
await sendSupportRequest({
message,
deviceInfo,
logFiles: attachments,
userEmail: email,
userName: name,
subject: t('supportEmailSubject', { appName: APP_NAME, user: userId }),
})
// Used to prevent flickering of the activity indicator on quick uploads
// Also navigateBackAndToast is a bit slow, so the timeout helps ensure that the loadingSpinner stays until the user is redirected
setTimeout(() => setInProgress(false), 1000)
navigateBackAndToast()
} catch (error) {
Logger.error('SupportContact', 'Error while sending logs to support', error)
}
}, [message, attachLogs, e164PhoneNumber, email, name])
return (
<View style={styles.container}>
<ScrollView contentContainerStyle={styles.innerContainer}>
<Text style={styles.title} testID={'ContactTitle'}>
{t('contact')}
</Text>
<Text style={styles.headerText}>{t('message')}</Text>
<TextInput
onChangeText={setMessage}
value={message}
multiline={true}
style={styles.messageTextInput}
placeholderTextColor={colors.gray4}
underlineColorAndroid="transparent"
numberOfLines={10}
placeholder={t('contactMessagePlaceholder') ?? undefined}
showClearButton={false}
testID={'MessageEntry'}
/>
<Text style={styles.headerText}>{t('Name')}</Text>
<TextInput
onChangeText={setName}
multiline={false}
value={name}
style={styles.singleLineTextInput}
showClearButton={false}
testID={'NameEntry'}
/>
<Text style={styles.headerText}>{t('Email')}</Text>
<TextInput
textContentType="emailAddress"
keyboardType="email-address"
autoComplete="email"
onChangeText={setEmail}
multiline={false}
value={email}
style={styles.singleLineTextInput}
placeholderTextColor={colors.gray4}
placeholder={t('Email') ?? undefined}
showClearButton={false}
testID={'EmailEntry'}
/>
<View style={styles.attachLogs}>
<Switch
testID="SwitchLogs"
style={styles.logsSwitch}
value={attachLogs}
onValueChange={setAttachLogs}
/>
<Text style={fontStyles.regular}>{t('attachLogs')}</Text>
</View>
{inProgress && (
<View style={styles.loadingSpinnerContainer} testID="ImportWalletLoadingCircle">
<ActivityIndicator size="large" color={colors.primary} />
</View>
)}
<View style={styles.disclaimer}>
<Text testID="Legal" style={styles.disclaimerText}>
{t('supportLegalCheckbox')}
</Text>
</View>
<Button
disabled={!message || inProgress || !name || !email || !validateEmail(email)}
onPress={onPressSendEmail}
text={t('submit')}
type={BtnTypes.PRIMARY}
testID="SubmitContactForm"
/>
</ScrollView>
<KeyboardSpacer />
</View>
)
}
const styles = StyleSheet.create({
disclaimer: {
marginBottom: 24,
},
disclaimerText: {
...fontStyles.small,
color: colors.gray4,
},
container: {
flex: 1,
},
innerContainer: {
flexGrow: 1,
padding: 16,
},
attachLogs: {
flexShrink: 0,
flexDirection: 'row',
height: 40,
alignItems: 'center',
marginTop: 4,
},
logsSwitch: {
marginBottom: 3,
marginRight: 10,
},
messageTextInput: {
...fontStyles.regular,
paddingHorizontal: 12,
paddingVertical: 4,
marginTop: 8,
alignItems: 'flex-start',
borderColor: colors.gray2,
borderRadius: 4,
borderWidth: 1.5,
marginBottom: 16,
color: colors.black,
height: 80,
maxHeight: 150,
},
singleLineTextInput: {
...fontStyles.regular,
paddingHorizontal: 12,
marginTop: 8,
alignItems: 'flex-start',
borderColor: colors.gray2,
borderRadius: 4,
borderWidth: 1.5,
marginBottom: 16,
color: colors.black,
maxHeight: 50,
},
headerText: {
...fontStyles.small600,
},
loadingSpinnerContainer: {
marginVertical: 20,
},
title: {
...fontStyles.h1,
marginVertical: 16,
},
})
export default SupportContact