mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {View} from 'react-native'
3import {AppBskyActorDefs} from '@atproto/api'
4import {msg} from '@lingui/macro'
5import {useLingui} from '@lingui/react'
6import {useNavigation} from '@react-navigation/native'
7
8import {useEmail} from '#/lib/hooks/useEmail'
9import {NavigationProp} from '#/lib/routes/types'
10import {logEvent} from '#/lib/statsig/statsig'
11import {useGetConvoAvailabilityQuery} from '#/state/queries/messages/get-convo-availability'
12import {useGetConvoForMembers} from '#/state/queries/messages/get-convo-for-members'
13import * as Toast from '#/view/com/util/Toast'
14import {atoms as a, useTheme} from '#/alf'
15import {Button, ButtonIcon} from '#/components/Button'
16import {useDialogControl} from '#/components/Dialog'
17import {VerifyEmailDialog} from '#/components/dialogs/VerifyEmailDialog'
18import {canBeMessaged} from '#/components/dms/util'
19import {Message_Stroke2_Corner0_Rounded as Message} from '#/components/icons/Message'
20
21export function MessageProfileButton({
22 profile,
23}: {
24 profile: AppBskyActorDefs.ProfileViewDetailed
25}) {
26 const {_} = useLingui()
27 const t = useTheme()
28 const navigation = useNavigation<NavigationProp>()
29 const {needsEmailVerification} = useEmail()
30 const verifyEmailControl = useDialogControl()
31
32 const {data: convoAvailability} = useGetConvoAvailabilityQuery(profile.did)
33 const {mutate: initiateConvo} = useGetConvoForMembers({
34 onSuccess: ({convo}) => {
35 logEvent('chat:open', {logContext: 'ProfileHeader'})
36 navigation.navigate('MessagesConversation', {conversation: convo.id})
37 },
38 onError: () => {
39 Toast.show(_(msg`Failed to create conversation`))
40 },
41 })
42
43 const onPress = React.useCallback(() => {
44 if (!convoAvailability?.canChat) {
45 return
46 }
47
48 if (needsEmailVerification) {
49 verifyEmailControl.open()
50 return
51 }
52
53 if (convoAvailability.convo) {
54 logEvent('chat:open', {logContext: 'ProfileHeader'})
55 navigation.navigate('MessagesConversation', {
56 conversation: convoAvailability.convo.id,
57 })
58 } else {
59 logEvent('chat:create', {logContext: 'ProfileHeader'})
60 initiateConvo([profile.did])
61 }
62 }, [
63 needsEmailVerification,
64 verifyEmailControl,
65 navigation,
66 profile.did,
67 initiateConvo,
68 convoAvailability,
69 ])
70
71 if (!convoAvailability) {
72 // show pending state based on declaration
73 if (canBeMessaged(profile)) {
74 return (
75 <View
76 testID="dmBtnLoading"
77 aria-hidden={true}
78 style={[
79 a.justify_center,
80 a.align_center,
81 t.atoms.bg_contrast_25,
82 a.rounded_full,
83 {width: 34, height: 34},
84 ]}>
85 <Message style={[t.atoms.text, {opacity: 0.3}]} size="md" />
86 </View>
87 )
88 } else {
89 return null
90 }
91 }
92
93 if (convoAvailability.canChat) {
94 return (
95 <>
96 <Button
97 accessibilityRole="button"
98 testID="dmBtn"
99 size="small"
100 color="secondary"
101 variant="solid"
102 shape="round"
103 label={_(msg`Message ${profile.handle}`)}
104 style={[a.justify_center]}
105 onPress={onPress}>
106 <ButtonIcon icon={Message} size="md" />
107 </Button>
108 <VerifyEmailDialog
109 reasonText={_(
110 msg`Before you may message another user, you must first verify your email.`,
111 )}
112 control={verifyEmailControl}
113 />
114 </>
115 )
116 } else {
117 return null
118 }
119}