@@ -11,7 +11,10 @@ import {
1111
1212import { CustomDataManagerState , TextComposerState } from 'stream-chat' ;
1313
14- import { useChannelContext } from '../../contexts/channelContext/ChannelContext' ;
14+ import {
15+ ChannelContextValue ,
16+ useChannelContext ,
17+ } from '../../contexts/channelContext/ChannelContext' ;
1518import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer' ;
1619import {
1720 MessageInputContextValue ,
@@ -25,16 +28,19 @@ import {
2528
2629import { useStateStore } from '../../hooks/useStateStore' ;
2730
28- type AutoCompleteInputProps = TextInputProps &
29- Partial < Pick < MessageInputContextValue , 'setInputBoxRef' > > &
30- Partial < Pick < TranslationContextValue , 't' > > & {
31+ type AutoCompleteInputPropsWithContext = TextInputProps &
32+ Pick < ChannelContextValue , 'channel' > &
33+ Pick < MessageInputContextValue , 'setInputBoxRef' > &
34+ Pick < TranslationContextValue , 't' > & {
3135 /**
3236 * This is currently passed in from MessageInput to avoid rerenders
3337 * that would happen if we put this in the MessageInputContext
3438 */
3539 cooldownActive ?: boolean ;
3640 } ;
3741
42+ type AutoCompleteInputProps = Partial < AutoCompleteInputPropsWithContext > ;
43+
3844const textComposerStateSelector = ( state : TextComposerState ) => ( {
3945 text : state . text ,
4046} ) ;
@@ -45,20 +51,14 @@ const customComposerDataSelector = (state: CustomDataManagerState) => ({
4551
4652const MAX_NUMBER_OF_LINES = 5 ;
4753
48- export const AutoCompleteInput = ( props : AutoCompleteInputProps ) => {
49- const { cooldownActive = false , setInputBoxRef : propSetInputBoxRef , t : propT , ...rest } = props ;
54+ const AutoCompleteInputWithContext = ( props : AutoCompleteInputPropsWithContext ) => {
55+ const { channel , cooldownActive = false , setInputBoxRef, t, ...rest } = props ;
5056 const [ localText , setLocalText ] = useState ( '' ) ;
5157 const [ textHeight , setTextHeight ] = useState ( 0 ) ;
5258 const messageComposer = useMessageComposer ( ) ;
5359 const { customDataManager, textComposer } = messageComposer ;
5460 const { text } = useStateStore ( textComposer . state , textComposerStateSelector ) ;
5561 const { command } = useStateStore ( customDataManager . state , customComposerDataSelector ) ;
56- const { channel } = useChannelContext ( ) ;
57- const { setInputBoxRef : contextSetInputBoxRef } = useMessageInputContext ( ) ;
58- const { t : contextT } = useTranslationContext ( ) ;
59-
60- const setInputBoxRef = propSetInputBoxRef || contextSetInputBoxRef ;
61- const t = propT || contextT ;
6262
6363 const maxMessageLength = useMemo ( ( ) => {
6464 return channel . getConfig ( ) ?. max_message_length ;
@@ -142,6 +142,53 @@ export const AutoCompleteInput = (props: AutoCompleteInputProps) => {
142142 ) ;
143143} ;
144144
145+ const areEqual = (
146+ prevProps : AutoCompleteInputPropsWithContext ,
147+ nextProps : AutoCompleteInputPropsWithContext ,
148+ ) => {
149+ const { channel : prevChannel , cooldownActive : prevCooldownActive , t : prevT } = prevProps ;
150+ const { channel : nextChannel , cooldownActive : nextCooldownActive , t : nextT } = nextProps ;
151+
152+ const tEqual = prevT === nextT ;
153+ if ( ! tEqual ) {
154+ return false ;
155+ }
156+
157+ const cooldownActiveEqual = prevCooldownActive === nextCooldownActive ;
158+ if ( ! cooldownActiveEqual ) {
159+ return false ;
160+ }
161+
162+ const channelEqual = prevChannel . cid === nextChannel . cid ;
163+ if ( ! channelEqual ) {
164+ return false ;
165+ }
166+
167+ return true ;
168+ } ;
169+
170+ const MemoizedAutoCompleteInput = React . memo (
171+ AutoCompleteInputWithContext ,
172+ areEqual ,
173+ ) as typeof AutoCompleteInputWithContext ;
174+
175+ export const AutoCompleteInput = ( props : AutoCompleteInputProps ) => {
176+ const { setInputBoxRef } = useMessageInputContext ( ) ;
177+ const { t } = useTranslationContext ( ) ;
178+ const { channel } = useChannelContext ( ) ;
179+
180+ return (
181+ < MemoizedAutoCompleteInput
182+ { ...{
183+ channel,
184+ setInputBoxRef,
185+ t,
186+ } }
187+ { ...props }
188+ />
189+ ) ;
190+ } ;
191+
145192const styles = StyleSheet . create ( {
146193 inputBox : {
147194 flex : 1 ,
0 commit comments