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 {useMaybeConvoForUser} from '#/state/queries/messages/get-convo-for-members'
12import {atoms as a, useTheme} from '#/alf'
13import {Button, ButtonIcon} from '#/components/Button'
14import {canBeMessaged} from '#/components/dms/util'
15import {Message_Stroke2_Corner0_Rounded as Message} from '#/components/icons/Message'
16import {useDialogControl} from '../Dialog'
17import {VerifyEmailDialog} from '../dialogs/VerifyEmailDialog'
18
19export function MessageProfileButton({
20 profile,
21}: {
22 profile: AppBskyActorDefs.ProfileView
23}) {
24 const {_} = useLingui()
25 const t = useTheme()
26 const navigation = useNavigation<NavigationProp>()
27 const {needsEmailVerification} = useEmail()
28 const verifyEmailControl = useDialogControl()
29
30 const {data: convo, isPending} = useMaybeConvoForUser(profile.did)
31
32 const onPress = React.useCallback(() => {
33 if (!convo?.id) {
34 return
35 }
36
37 if (needsEmailVerification) {
38 verifyEmailControl.open()
39 return
40 }
41
42 if (convo && !convo.lastMessage) {
43 logEvent('chat:create', {logContext: 'ProfileHeader'})
44 }
45 logEvent('chat:open', {logContext: 'ProfileHeader'})
46
47 navigation.navigate('MessagesConversation', {conversation: convo.id})
48 }, [needsEmailVerification, verifyEmailControl, convo, navigation])
49
50 if (isPending) {
51 // show pending state based on declaration
52 if (canBeMessaged(profile)) {
53 return (
54 <View
55 testID="dmBtnLoading"
56 aria-hidden={true}
57 style={[
58 a.justify_center,
59 a.align_center,
60 t.atoms.bg_contrast_25,
61 a.rounded_full,
62 {width: 34, height: 34},
63 ]}>
64 <Message style={[t.atoms.text, {opacity: 0.3}]} size="md" />
65 </View>
66 )
67 } else {
68 return null
69 }
70 }
71
72 if (convo) {
73 return (
74 <>
75 <Button
76 accessibilityRole="button"
77 testID="dmBtn"
78 size="small"
79 color="secondary"
80 variant="solid"
81 shape="round"
82 label={_(msg`Message ${profile.handle}`)}
83 style={[a.justify_center]}
84 onPress={onPress}>
85 <ButtonIcon icon={Message} size="md" />
86 </Button>
87 <VerifyEmailDialog
88 reasonText={_(
89 msg`Before you may message another user, you must first verify your email.`,
90 )}
91 control={verifyEmailControl}
92 />
93 </>
94 )
95 } else {
96 return null
97 }
98}