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