mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at samuel/patch-onpaste 107 lines 3.3 kB view raw
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 // Matches size of button below to avoid layout shift 77 {width: 33, height: 33}, 78 ]}> 79 <Message style={[t.atoms.text, {opacity: 0.3}]} size="md" /> 80 </View> 81 ) 82 } else { 83 return null 84 } 85 } 86 87 if (convoAvailability.canChat) { 88 return ( 89 <> 90 <Button 91 accessibilityRole="button" 92 testID="dmBtn" 93 size="small" 94 color="secondary" 95 variant="solid" 96 shape="round" 97 label={_(msg`Message ${profile.handle}`)} 98 style={[a.justify_center]} 99 onPress={wrappedOnPress}> 100 <ButtonIcon icon={Message} size="md" /> 101 </Button> 102 </> 103 ) 104 } else { 105 return null 106 } 107}