|
| 1 | +import React from 'react'; |
| 2 | +import { Alert } from 'react-native'; |
| 3 | +import { TouchableOpacity } from 'react-native-gesture-handler'; |
| 4 | +import { useNavigation } from '@react-navigation/core'; |
| 5 | + |
| 6 | +import { |
| 7 | + DefaultAttachmentType, |
| 8 | + DefaultChannelType, |
| 9 | + DefaultCommandType, |
| 10 | + DefaultEventType, |
| 11 | + DefaultMessageType, |
| 12 | + DefaultReactionType, |
| 13 | + DefaultUserType, |
| 14 | + MessageInputContextValue, |
| 15 | + Search, |
| 16 | + SendRight, |
| 17 | + SendUp, |
| 18 | + UnknownType, |
| 19 | + useChannelContext, |
| 20 | + useMessageInputContext, |
| 21 | + useTheme, |
| 22 | +} from 'stream-chat-react-native'; |
| 23 | + |
| 24 | +import { NewDirectMessagingScreenNavigationProp } from '../screens/NewDirectMessagingScreen'; |
| 25 | + |
| 26 | +import { |
| 27 | + LocalAttachmentType, |
| 28 | + LocalChannelType, |
| 29 | + LocalCommandType, |
| 30 | + LocalEventType, |
| 31 | + LocalMessageType, |
| 32 | + LocalReactionType, |
| 33 | + LocalUserType, |
| 34 | +} from '../types'; |
| 35 | + |
| 36 | +type NewDirectMessagingSendButtonPropsWithContext< |
| 37 | + At extends UnknownType = DefaultAttachmentType, |
| 38 | + Ch extends UnknownType = DefaultChannelType, |
| 39 | + Co extends string = DefaultCommandType, |
| 40 | + Ev extends UnknownType = DefaultEventType, |
| 41 | + Me extends UnknownType = DefaultMessageType, |
| 42 | + Re extends UnknownType = DefaultReactionType, |
| 43 | + Us extends UnknownType = DefaultUserType |
| 44 | +> = Pick< |
| 45 | + MessageInputContextValue<At, Ch, Co, Ev, Me, Re, Us>, |
| 46 | + 'giphyActive' | 'sendMessage' |
| 47 | +> & { |
| 48 | + /** Disables the button */ disabled: boolean; |
| 49 | +}; |
| 50 | + |
| 51 | +const SendButtonWithContext = < |
| 52 | + At extends UnknownType = DefaultAttachmentType, |
| 53 | + Ch extends UnknownType = DefaultChannelType, |
| 54 | + Co extends string = DefaultCommandType, |
| 55 | + Ev extends UnknownType = DefaultEventType, |
| 56 | + Me extends UnknownType = DefaultMessageType, |
| 57 | + Re extends UnknownType = DefaultReactionType, |
| 58 | + Us extends UnknownType = DefaultUserType |
| 59 | +>( |
| 60 | + props: NewDirectMessagingSendButtonPropsWithContext< |
| 61 | + At, |
| 62 | + Ch, |
| 63 | + Co, |
| 64 | + Ev, |
| 65 | + Me, |
| 66 | + Re, |
| 67 | + Us |
| 68 | + >, |
| 69 | +) => { |
| 70 | + const { disabled = false, giphyActive, sendMessage } = props; |
| 71 | + const { |
| 72 | + theme: { |
| 73 | + colors: { accent_blue, grey_gainsboro }, |
| 74 | + messageInput: { sendButton }, |
| 75 | + }, |
| 76 | + } = useTheme(); |
| 77 | + |
| 78 | + return ( |
| 79 | + <TouchableOpacity |
| 80 | + disabled={disabled} |
| 81 | + onPress={sendMessage} |
| 82 | + style={[sendButton]} |
| 83 | + testID='send-button' |
| 84 | + > |
| 85 | + {giphyActive && <Search pathFill={accent_blue} />} |
| 86 | + {!giphyActive && disabled && <SendRight pathFill={grey_gainsboro} />} |
| 87 | + {!giphyActive && !disabled && <SendUp pathFill={accent_blue} />} |
| 88 | + </TouchableOpacity> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +const areEqual = < |
| 93 | + At extends UnknownType = DefaultAttachmentType, |
| 94 | + Ch extends UnknownType = DefaultChannelType, |
| 95 | + Co extends string = DefaultCommandType, |
| 96 | + Ev extends UnknownType = DefaultEventType, |
| 97 | + Me extends UnknownType = DefaultMessageType, |
| 98 | + Re extends UnknownType = DefaultReactionType, |
| 99 | + Us extends UnknownType = DefaultUserType |
| 100 | +>( |
| 101 | + prevProps: NewDirectMessagingSendButtonPropsWithContext< |
| 102 | + At, |
| 103 | + Ch, |
| 104 | + Co, |
| 105 | + Ev, |
| 106 | + Me, |
| 107 | + Re, |
| 108 | + Us |
| 109 | + >, |
| 110 | + nextProps: NewDirectMessagingSendButtonPropsWithContext< |
| 111 | + At, |
| 112 | + Ch, |
| 113 | + Co, |
| 114 | + Ev, |
| 115 | + Me, |
| 116 | + Re, |
| 117 | + Us |
| 118 | + >, |
| 119 | +) => { |
| 120 | + const { |
| 121 | + disabled: prevDisabled, |
| 122 | + giphyActive: prevGiphyActive, |
| 123 | + sendMessage: prevSendMessage, |
| 124 | + } = prevProps; |
| 125 | + const { |
| 126 | + disabled: nextDisabled, |
| 127 | + giphyActive: nextGiphyActive, |
| 128 | + sendMessage: nextSendMessage, |
| 129 | + } = nextProps; |
| 130 | + |
| 131 | + const disabledEqual = prevDisabled === nextDisabled; |
| 132 | + if (!disabledEqual) return false; |
| 133 | + |
| 134 | + const giphyActiveEqual = prevGiphyActive === nextGiphyActive; |
| 135 | + if (!giphyActiveEqual) return false; |
| 136 | + |
| 137 | + const sendMessageEqual = prevSendMessage === nextSendMessage; |
| 138 | + if (!sendMessageEqual) return false; |
| 139 | + |
| 140 | + return true; |
| 141 | +}; |
| 142 | + |
| 143 | +const MemoizedNewDirectMessagingSendButton = React.memo( |
| 144 | + SendButtonWithContext, |
| 145 | + areEqual, |
| 146 | +) as typeof SendButtonWithContext; |
| 147 | + |
| 148 | +export type SendButtonProps< |
| 149 | + At extends UnknownType = DefaultAttachmentType, |
| 150 | + Ch extends UnknownType = DefaultChannelType, |
| 151 | + Co extends string = DefaultCommandType, |
| 152 | + Ev extends UnknownType = DefaultEventType, |
| 153 | + Me extends UnknownType = DefaultMessageType, |
| 154 | + Re extends UnknownType = DefaultReactionType, |
| 155 | + Us extends UnknownType = DefaultUserType |
| 156 | +> = Partial< |
| 157 | + NewDirectMessagingSendButtonPropsWithContext<At, Ch, Co, Ev, Me, Re, Us> |
| 158 | +>; |
| 159 | + |
| 160 | +/** |
| 161 | + * UI Component for send button in MessageInput component. |
| 162 | + */ |
| 163 | +export const NewDirectMessagingSendButton = ( |
| 164 | + props: SendButtonProps< |
| 165 | + LocalAttachmentType, |
| 166 | + LocalChannelType, |
| 167 | + LocalCommandType, |
| 168 | + LocalEventType, |
| 169 | + LocalMessageType, |
| 170 | + LocalReactionType, |
| 171 | + LocalUserType |
| 172 | + >, |
| 173 | +) => { |
| 174 | + const navigation = useNavigation<NewDirectMessagingScreenNavigationProp>(); |
| 175 | + const { channel } = useChannelContext< |
| 176 | + LocalAttachmentType, |
| 177 | + LocalChannelType, |
| 178 | + LocalCommandType, |
| 179 | + LocalEventType, |
| 180 | + LocalMessageType, |
| 181 | + LocalReactionType, |
| 182 | + LocalUserType |
| 183 | + >(); |
| 184 | + |
| 185 | + const { giphyActive, text } = useMessageInputContext< |
| 186 | + LocalAttachmentType, |
| 187 | + LocalChannelType, |
| 188 | + LocalCommandType, |
| 189 | + LocalEventType, |
| 190 | + LocalMessageType, |
| 191 | + LocalReactionType, |
| 192 | + LocalUserType |
| 193 | + >(); |
| 194 | + |
| 195 | + const sendMessage = async () => { |
| 196 | + if (!channel) return; |
| 197 | + channel.initialized = false; |
| 198 | + await channel.query({}); |
| 199 | + try { |
| 200 | + await channel.sendMessage({ text }); |
| 201 | + navigation.replace('ChannelScreen', { |
| 202 | + channelId: channel.id, |
| 203 | + }); |
| 204 | + } catch (e) { |
| 205 | + Alert.alert('Error sending a message'); |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + return ( |
| 210 | + <MemoizedNewDirectMessagingSendButton< |
| 211 | + LocalAttachmentType, |
| 212 | + LocalChannelType, |
| 213 | + LocalCommandType, |
| 214 | + LocalEventType, |
| 215 | + LocalMessageType, |
| 216 | + LocalReactionType, |
| 217 | + LocalUserType |
| 218 | + > |
| 219 | + {...{ giphyActive, sendMessage }} |
| 220 | + {...props} |
| 221 | + {...{ disabled: props.disabled || false }} |
| 222 | + /> |
| 223 | + ); |
| 224 | +}; |
0 commit comments