1- import React from 'react' ;
1+ import React , { useCallback } from 'react' ;
22
33import { Pressable } from 'react-native' ;
44
@@ -12,34 +12,41 @@ import { SendRight } from '../../icons/SendRight';
1212import { SendUp } from '../../icons/SendUp' ;
1313
1414import type { DefaultStreamChatGenerics } from '../../types/types' ;
15+ import { useChannelContext } from '../../contexts' ;
16+ import { AIStates , AIStatesEnum , useAIState } from '../AITypingIndicatorView' ;
17+ import { EventAPIResponse } from 'stream-chat' ;
1518
1619type SendButtonPropsWithContext <
1720 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
1821> = Pick < MessageInputContextValue < StreamChatGenerics > , 'giphyActive' | 'sendMessage' > & {
1922 /** Disables the button */ disabled : boolean ;
23+ aiState : AIStatesEnum ;
24+ stopGenerating : ( ) => Promise < EventAPIResponse < StreamChatGenerics > > ;
2025} ;
2126
2227const SendButtonWithContext = <
2328 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
2429> (
2530 props : SendButtonPropsWithContext < StreamChatGenerics > ,
2631) => {
27- const { disabled = false , giphyActive, sendMessage } = props ;
32+ const { disabled = false , giphyActive, sendMessage, aiState , stopGenerating } = props ;
2833 const {
2934 theme : {
3035 colors : { accent_blue, grey_gainsboro } ,
3136 messageInput : { searchIcon, sendButton, sendRightIcon, sendUpIcon } ,
3237 } ,
3338 } = useTheme ( ) ;
3439
40+ const shouldDisplayStopAIGeneration = [ AIStates . Thinking , AIStates . Generating ] . includes ( aiState ) ;
41+
3542 return (
3643 < Pressable
3744 disabled = { disabled }
38- onPress = { disabled ? ( ) => null : ( ) => sendMessage ( ) }
45+ onPress = { disabled ? ( ) => null : shouldDisplayStopAIGeneration ? ( ) => stopGenerating ( ) : ( ) => sendMessage ( ) }
3946 style = { [ sendButton ] }
4047 testID = 'send-button'
4148 >
42- { giphyActive ? (
49+ { giphyActive || shouldDisplayStopAIGeneration ? ( // TODO: Fix the icon please.
4350 < Search pathFill = { disabled ? grey_gainsboro : accent_blue } { ...searchIcon } />
4451 ) : disabled ? (
4552 < SendRight fill = { grey_gainsboro } size = { 32 } { ...sendRightIcon } />
@@ -58,11 +65,13 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
5865 disabled : prevDisabled ,
5966 giphyActive : prevGiphyActive ,
6067 sendMessage : prevSendMessage ,
68+ aiState : prevAiState ,
6169 } = prevProps ;
6270 const {
6371 disabled : nextDisabled ,
6472 giphyActive : nextGiphyActive ,
6573 sendMessage : nextSendMessage ,
74+ aiState : nextAiState ,
6675 } = nextProps ;
6776
6877 const disabledEqual = prevDisabled === nextDisabled ;
@@ -74,6 +83,9 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
7483 const sendMessageEqual = prevSendMessage === nextSendMessage ;
7584 if ( ! sendMessageEqual ) return false ;
7685
86+ const aiStateEqual = prevAiState === nextAiState ;
87+ if ( ! aiStateEqual ) return false ;
88+
7789 return true ;
7890} ;
7991
@@ -95,12 +107,18 @@ export const SendButton = <
95107 props : SendButtonProps < StreamChatGenerics > ,
96108) => {
97109 const { giphyActive, sendMessage } = useMessageInputContext < StreamChatGenerics > ( ) ;
110+ const { channel } = useChannelContext < StreamChatGenerics > ( ) ;
111+ const { aiState } = useAIState ( channel ) ;
112+
113+ const stopGenerating = useCallback ( ( ) => channel . sendEvent ( { type : 'stop_generating' , cid : channel . cid } ) , [ channel ] )
98114
99115 return (
100116 < MemoizedSendButton
101117 { ...{ giphyActive, sendMessage } }
102118 { ...props }
103119 { ...{ disabled : props . disabled || false } }
120+ aiState = { aiState }
121+ stopGenerating = { stopGenerating }
104122 />
105123 ) ;
106124} ;
0 commit comments