@@ -188,6 +188,13 @@ type ChannelPropsForwardedToComponentContext<
188188 VirtualMessage ?: ComponentContextValue < StreamChatGenerics > [ 'VirtualMessage' ] ;
189189} ;
190190
191+ const isUserResponseArray = <
192+ StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
193+ > (
194+ output : string [ ] | UserResponse < StreamChatGenerics > [ ] ,
195+ ) : output is UserResponse < StreamChatGenerics > [ ] =>
196+ ( output as UserResponse < StreamChatGenerics > [ ] ) [ 0 ] ?. id != null ;
197+
191198export type ChannelProps <
192199 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
193200 V extends CustomTrigger = CustomTrigger
@@ -591,16 +598,17 @@ const ChannelInner = <
591598 // Adds a temporary notification to message list, will be removed after 5 seconds
592599 const addNotification = makeAddNotifications ( setNotifications , notificationTimeouts ) ;
593600
594- const loadMoreFinished = debounce (
595- ( hasMore : boolean , messages : ChannelState < StreamChatGenerics > [ 'messages' ] ) => {
596- if ( ! isMounted . current ) return ;
597- dispatch ( { hasMore, messages, type : 'loadMoreFinished' } ) ;
598- } ,
599- 2000 ,
600- {
601- leading : true ,
602- trailing : true ,
603- } ,
601+ // eslint-disable-next-line react-hooks/exhaustive-deps
602+ const loadMoreFinished = useCallback (
603+ debounce (
604+ ( hasMore : boolean , messages : ChannelState < StreamChatGenerics > [ 'messages' ] ) => {
605+ if ( ! isMounted . current ) return ;
606+ dispatch ( { hasMore, messages, type : 'loadMoreFinished' } ) ;
607+ } ,
608+ 2000 ,
609+ { leading : true , trailing : true } ,
610+ ) ,
611+ [ ] ,
604612 ) ;
605613
606614 const loadMore = async ( limit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE ) => {
@@ -637,7 +645,7 @@ const ChannelInner = <
637645 } ;
638646
639647 const loadMoreNewer = async ( limit = 100 ) => {
640- if ( ! online . current || ! window . navigator . onLine ) return 0 ;
648+ if ( ! online . current || ! window . navigator . onLine || ! state . hasMoreNewer ) return 0 ;
641649
642650 const newestMessage = state ?. messages ?. [ state ?. messages ?. length - 1 ] ;
643651 if ( state . loadingMore || state . loadingMoreNewer ) return 0 ;
@@ -659,9 +667,13 @@ const ChannelInner = <
659667 return 0 ;
660668 }
661669
662- const hasMoreNewer = channel . state . messages !== channel . state . latestMessages ;
670+ const hasMoreNewerMessages = channel . state . messages !== channel . state . latestMessages ;
663671
664- dispatch ( { hasMoreNewer, messages : channel . state . messages , type : 'loadMoreNewerFinished' } ) ;
672+ dispatch ( {
673+ hasMoreNewer : hasMoreNewerMessages ,
674+ messages : channel . state . messages ,
675+ type : 'loadMoreNewerFinished' ,
676+ } ) ;
665677 return queryResponse . messages . length ;
666678 } ;
667679
@@ -738,11 +750,6 @@ const ChannelInner = <
738750 } ) ;
739751 } ;
740752
741- const isUserResponseArray = (
742- output : string [ ] | UserResponse < StreamChatGenerics > [ ] ,
743- ) : output is UserResponse < StreamChatGenerics > [ ] =>
744- ( output as UserResponse < StreamChatGenerics > [ ] ) [ 0 ] ?. id != null ;
745-
746753 const doSendMessage = async (
747754 message : MessageToSend < StreamChatGenerics > | StreamMessage < StreamChatGenerics > ,
748755 customMessageData ?: Partial < Message < StreamChatGenerics > > ,
@@ -751,7 +758,7 @@ const ChannelInner = <
751758 const { attachments, id, mentioned_users = [ ] , parent_id, text } = message ;
752759
753760 // channel.sendMessage expects an array of user id strings
754- const mentions = isUserResponseArray ( mentioned_users )
761+ const mentions = isUserResponseArray < StreamChatGenerics > ( mentioned_users )
755762 ? mentioned_users . map ( ( { id } ) => id )
756763 : mentioned_users ;
757764
@@ -894,43 +901,47 @@ const ChannelInner = <
894901 dispatch ( { type : 'closeThread' } ) ;
895902 } ;
896903
897- const loadMoreThreadFinished = debounce (
898- (
899- threadHasMore : boolean ,
900- threadMessages : Array < ReturnType < ChannelState < StreamChatGenerics > [ 'formatMessage' ] > > ,
901- ) => {
902- dispatch ( {
903- threadHasMore,
904- threadMessages,
905- type : 'loadMoreThreadFinished' ,
906- } ) ;
907- } ,
908- 2000 ,
909- { leading : true , trailing : true } ,
904+ // eslint-disable-next-line react-hooks/exhaustive-deps
905+ const loadMoreThreadFinished = useCallback (
906+ debounce (
907+ (
908+ threadHasMore : boolean ,
909+ threadMessages : Array < ReturnType < ChannelState < StreamChatGenerics > [ 'formatMessage' ] > > ,
910+ ) => {
911+ dispatch ( {
912+ threadHasMore,
913+ threadMessages,
914+ type : 'loadMoreThreadFinished' ,
915+ } ) ;
916+ } ,
917+ 2000 ,
918+ { leading : true , trailing : true } ,
919+ ) ,
920+ [ ] ,
910921 ) ;
911922
912923 const loadMoreThread = async ( limit : number = DEFAULT_THREAD_PAGE_SIZE ) => {
913924 // FIXME: should prevent loading more, if state.thread.reply_count === channel.state.threads[parentID].length
914925 if ( state . threadLoadingMore || ! state . thread ) return ;
915926
916927 dispatch ( { type : 'startLoadingThread' } ) ;
917- const parentID = state . thread . id ;
928+ const parentId = state . thread . id ;
918929
919- if ( ! parentID ) {
930+ if ( ! parentId ) {
920931 return dispatch ( { type : 'closeThread' } ) ;
921932 }
922933
923- const oldMessages = channel . state . threads [ parentID ] || [ ] ;
924- const oldestMessageID = oldMessages [ 0 ] ?. id ;
934+ const oldMessages = channel . state . threads [ parentId ] || [ ] ;
935+ const oldestMessageId = oldMessages [ 0 ] ?. id ;
925936
926937 try {
927- const queryResponse = await channel . getReplies ( parentID , {
928- id_lt : oldestMessageID ,
938+ const queryResponse = await channel . getReplies ( parentId , {
939+ id_lt : oldestMessageId ,
929940 limit,
930941 } ) ;
931942
932943 const threadHasMoreMessages = hasMoreMessagesProbably ( queryResponse . messages . length , limit ) ;
933- const newThreadMessages = channel . state . threads [ parentID ] || [ ] ;
944+ const newThreadMessages = channel . state . threads [ parentId ] || [ ] ;
934945
935946 // next set loadingMore to false so we can start asking for more data
936947 loadMoreThreadFinished ( threadHasMoreMessages , newThreadMessages ) ;
0 commit comments