-
Notifications
You must be signed in to change notification settings - Fork 106
/
SocialProof.tsx
299 lines (268 loc) · 8.28 KB
/
SocialProof.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { useCallback, useEffect, useState } from 'react'
import {
Name,
Status,
BooleanKeys,
accountSelectors,
TwitterProfile,
InstagramProfile,
accountActions,
TikTokProfile
} from '@audius/common'
import { useDispatch, useSelector } from 'react-redux'
import { ReactComponent as IconValidationX } from 'assets/img/iconValidationX.svg'
import { useModalState } from 'common/hooks/useModalState'
import { make, TrackEvent, useRecord } from 'common/store/analytics/actions'
import { InstagramAuthButton } from 'components/instagram-auth/InstagramAuthButton'
import LoadingSpinner from 'components/loading-spinner/LoadingSpinner'
import { TikTokAuthButton } from 'components/tiktok-auth/TikTokAuthButton'
import { TwitterAuthButton } from 'components/twitter-auth/TwitterAuthButton'
import { useRemoteVar } from 'hooks/useRemoteConfig'
import ModalDrawer from 'pages/audio-rewards-page/components/modals/ModalDrawer'
import styles from './SocialProof.module.css'
const {
instagramLogin: instagramLoginAction,
twitterLogin: twitterLoginAction,
tikTokLogin: tikTokLoginAction
} = accountActions
const getUserHandle = accountSelectors.getUserHandle
const messages = {
modalTitle: 'Confirm Your Identity',
description: 'Please confirm your identity before you can continue',
failure:
'Sorry, unable to retrieve information or the account is already used',
errorTwitter: 'Unable to confirm your Twitter account',
errorInstagram: 'Unable to confirm your Instagram account',
twitterConfirm: 'Confirm with Twitter',
instagramConfirm: 'Confirm with Instagram',
tiktokConfirm: 'Confirm with TikTok'
}
type VerifyBodyProps = {
handle: string
onClick: () => void
onFailure: (kind: 'instagram' | 'twitter' | 'tiktok', error: Error) => void
onTwitterLogin: (uuid: string, profile: TwitterProfile) => void
onInstagramLogin: (uuid: string, profile: InstagramProfile) => void
onTikTokLogin: (uuid: string, profile: TikTokProfile) => void
error?: string
}
const VerifyBody = ({
error,
handle,
onClick,
onTwitterLogin,
onInstagramLogin,
onTikTokLogin,
onFailure
}: VerifyBodyProps) => {
const isTwitterEnabled = useRemoteVar(
BooleanKeys.DISPLAY_TWITTER_VERIFICATION_WEB_AND_DESKTOP
)
const isInstagramEnabled = useRemoteVar(
BooleanKeys.DISPLAY_INSTAGRAM_VERIFICATION_WEB_AND_DESKTOP
)
const isTikTokEnabled = useRemoteVar(
BooleanKeys.DISPLAY_TIKTOK_VERIFICATION_WEB_AND_DESKTOP
)
const record = useRecord()
const handleClickTwitter = useCallback(() => {
onClick()
const trackEvent: TrackEvent = make(Name.SOCIAL_PROOF_OPEN, {
handle,
kind: 'twitter'
})
record(trackEvent)
}, [record, handle, onClick])
const handleClickInstagram = useCallback(() => {
onClick()
const trackEvent: TrackEvent = make(Name.SOCIAL_PROOF_OPEN, {
handle,
kind: 'instagram'
})
record(trackEvent)
}, [record, handle, onClick])
const handleClickTikTok = useCallback(() => {
onClick()
const trackEvent: TrackEvent = make(Name.SOCIAL_PROOF_OPEN, {
handle,
kind: 'tiktok'
})
record(trackEvent)
}, [record, handle, onClick])
return (
<div className={styles.container}>
<p>{messages.description}</p>
<div className={styles.btnContainer}>
{isTwitterEnabled ? (
<TwitterAuthButton
onSuccess={onTwitterLogin}
onFailure={(error: Error) => onFailure('twitter', error)}
onClick={handleClickTwitter}
text={messages.twitterConfirm}
/>
) : null}
{isInstagramEnabled && (
<InstagramAuthButton
onClick={handleClickInstagram}
onSuccess={onInstagramLogin}
onFailure={(error: Error) => onFailure('instagram', error)}
text={messages.instagramConfirm}
/>
)}
{isTikTokEnabled ? (
<TikTokAuthButton
onClick={handleClickTikTok}
onFailure={(error: Error) => onFailure('tiktok', error)}
onSuccess={onTikTokLogin}
text={messages.tiktokConfirm}
/>
) : null}
</div>
{error && (
<div className={styles.error}>
<IconValidationX className={styles.validationIcon} />
{error}
</div>
)}
</div>
)
}
const LoadingBody = () => {
return (
<div className={styles.container}>
<LoadingSpinner className={styles.loadingContainer} />
</div>
)
}
type SocialProofProps = {
onSuccess: () => void
}
/**
* A modal that verifies social proof of a user.
* When proof is made, the modal closes. There are no
* callbacks so that a server may determine proof.
*/
const SocialProof = ({ onSuccess }: SocialProofProps) => {
const dispatch = useDispatch()
const onInstagramLogin = useCallback(
(uuid: string, profile: InstagramProfile) => {
dispatch(instagramLoginAction({ uuid, profile }))
},
[dispatch]
)
const onTwitterLogin = useCallback(
(uuid: string, profile: TwitterProfile) => {
dispatch(twitterLoginAction({ uuid, profile }))
},
[dispatch]
)
const onTikTokLogin = useCallback(
(uuid: string, profile: TikTokProfile) => {
dispatch(tikTokLoginAction({ uuid, profile }))
},
[dispatch]
)
const [error, setError] = useState<string | undefined>()
const [status, setStatus] = useState(Status.IDLE)
const [isOpen, setIsOpen] = useModalState('SocialProof')
const handle = useSelector(getUserHandle)
const onClick = useCallback(() => setStatus(Status.LOADING), [setStatus])
const record = useRecord()
const onFailure = useCallback(
(kind: 'instagram' | 'twitter' | 'tiktok', error: Error) => {
// We should have an account handle by this point
if (!handle) return
setError(messages.failure)
setStatus(Status.ERROR)
const trackEvent = make(Name.SOCIAL_PROOF_ERROR, {
handle,
kind,
error: error.message
})
record(trackEvent)
},
[record, setError, setStatus, handle]
)
const handleInstagramLogin = useCallback(
(uuid: string, profile: InstagramProfile) => {
// We should have an account handle by this point
if (!handle) return
setStatus(Status.SUCCESS)
onInstagramLogin(uuid, profile)
setIsOpen(false)
const trackEvent: TrackEvent = make(Name.SOCIAL_PROOF_SUCCESS, {
handle,
kind: 'instagram',
screenName: profile.username
})
record(trackEvent)
},
[record, handle, onInstagramLogin, setIsOpen]
)
const handleTwitterLogin = useCallback(
(uuid: string, profile: TwitterProfile) => {
// We should have an account handle by this point
if (!handle) return
setStatus(Status.SUCCESS)
onTwitterLogin(uuid, profile)
setIsOpen(false)
const trackEvent: TrackEvent = make(Name.SOCIAL_PROOF_SUCCESS, {
handle,
kind: 'twitter',
screenName: profile.screen_name
})
record(trackEvent)
},
[record, handle, onTwitterLogin, setIsOpen]
)
const handleTikTokLogin = useCallback(
(uuid: string, profile: TikTokProfile) => {
// We should have an account handle by this point
if (!handle) return
setStatus(Status.SUCCESS)
onTikTokLogin(uuid, profile)
setIsOpen(false)
const trackEvent: TrackEvent = make(Name.SOCIAL_PROOF_SUCCESS, {
handle,
kind: 'tiktok',
screenName: profile.username
})
record(trackEvent)
},
[record, handle, onTikTokLogin, setIsOpen]
)
useEffect(() => {
if (status === Status.SUCCESS) {
onSuccess()
}
}, [onSuccess, status])
let body = null
if (status === Status.LOADING || handle === null) {
body = <LoadingBody />
} else if (status === Status.IDLE || status === Status.ERROR) {
body = (
<VerifyBody
handle={handle}
onClick={onClick}
onFailure={onFailure}
onInstagramLogin={handleInstagramLogin}
onTwitterLogin={handleTwitterLogin}
onTikTokLogin={handleTikTokLogin}
error={error}
/>
)
}
return (
<ModalDrawer
isOpen={isOpen}
onClose={() => setIsOpen(false)}
title={messages.modalTitle}
showTitleHeader
useGradientTitle={false}
bodyClassName={styles.modalBodyStyle}
>
{body}
</ModalDrawer>
)
}
export default SocialProof