@@ -411,7 +411,11 @@ const ChannelInner = <
411
411
channelReducer ,
412
412
// channel.initialized === false if client.channel().query() was not called, e.g. ChannelList is not used
413
413
// => Channel will call channel.watch() in useLayoutEffect => state.loading is used to signal the watch() call state
414
- { ...initialState , loading : ! channel . initialized } ,
414
+ {
415
+ ...initialState ,
416
+ hasMore : channel . state . messagePagination . hasPrev ,
417
+ loading : ! channel . initialized ,
418
+ } ,
415
419
) ;
416
420
417
421
const isMounted = useIsMounted ( ) ;
@@ -569,7 +573,6 @@ const ChannelInner = <
569
573
useLayoutEffect ( ( ) => {
570
574
let errored = false ;
571
575
let done = false ;
572
- let channelInitializedExternally = true ;
573
576
574
577
( async ( ) => {
575
578
if ( ! channel . initialized && initializeOnMount ) {
@@ -595,7 +598,6 @@ const ChannelInner = <
595
598
await getChannel ( { channel, client, members, options : channelQueryOptions } ) ;
596
599
const config = channel . getConfig ( ) ;
597
600
setChannelConfig ( config ) ;
598
- channelInitializedExternally = false ;
599
601
} catch ( e ) {
600
602
dispatch ( { error : e as Error , type : 'setError' } ) ;
601
603
errored = true ;
@@ -608,12 +610,7 @@ const ChannelInner = <
608
610
if ( ! errored ) {
609
611
dispatch ( {
610
612
channel,
611
- hasMore :
612
- channelInitializedExternally ||
613
- hasMoreMessagesProbably (
614
- channel . state . messages . length ,
615
- channelQueryOptions . messages . limit ,
616
- ) ,
613
+ hasMore : channel . state . messagePagination . hasPrev ,
617
614
type : 'initStateFromChannel' ,
618
615
} ) ;
619
616
@@ -688,7 +685,8 @@ const ChannelInner = <
688
685
) ;
689
686
690
687
const loadMore = async ( limit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE ) => {
691
- if ( ! online . current || ! window . navigator . onLine || ! state . hasMore ) return 0 ;
688
+ if ( ! online . current || ! window . navigator . onLine || ! channel . state . messagePagination . hasPrev )
689
+ return 0 ;
692
690
693
691
// prevent duplicate loading events...
694
692
const oldestMessage = state ?. messages ?. [ 0 ] ;
@@ -714,14 +712,14 @@ const ChannelInner = <
714
712
return 0 ;
715
713
}
716
714
717
- const hasMoreMessages = queryResponse . messages . length === perPage ;
718
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
715
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
719
716
720
717
return queryResponse . messages . length ;
721
718
} ;
722
719
723
720
const loadMoreNewer = async ( limit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE ) => {
724
- if ( ! online . current || ! window . navigator . onLine || ! state . hasMoreNewer ) return 0 ;
721
+ if ( ! online . current || ! window . navigator . onLine || ! channel . state . messagePagination . hasNext )
722
+ return 0 ;
725
723
726
724
const newestMessage = state ?. messages ?. [ state ?. messages ?. length - 1 ] ;
727
725
if ( state . loadingMore || state . loadingMoreNewer ) return 0 ;
@@ -743,10 +741,8 @@ const ChannelInner = <
743
741
return 0 ;
744
742
}
745
743
746
- const hasMoreNewerMessages = channel . state . messages !== channel . state . latestMessages ;
747
-
748
744
dispatch ( {
749
- hasMoreNewer : hasMoreNewerMessages ,
745
+ hasMoreNewer : channel . state . messagePagination . hasNext ,
750
746
messages : channel . state . messages ,
751
747
type : 'loadMoreNewerFinished' ,
752
748
} ) ;
@@ -764,18 +760,9 @@ const ChannelInner = <
764
760
dispatch ( { loadingMore : true , type : 'setLoadingMore' } ) ;
765
761
await channel . state . loadMessageIntoState ( messageId , undefined , messageLimit ) ;
766
762
767
- /**
768
- * if the message we are jumping to has less than half of the page size older messages,
769
- * we have jumped to the beginning of the channel.
770
- */
771
- const indexOfMessage = channel . state . messages . findIndex (
772
- ( message ) => message . id === messageId ,
773
- ) ;
774
- const hasMoreMessages = indexOfMessage >= Math . floor ( messageLimit / 2 ) ;
775
-
776
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
763
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
777
764
dispatch ( {
778
- hasMoreNewer : channel . state . messages !== channel . state . latestMessages ,
765
+ hasMoreNewer : channel . state . messagePagination . hasNext ,
779
766
highlightedMessageId : messageId ,
780
767
type : 'jumpToMessageFinished' ,
781
768
} ) ;
@@ -794,9 +781,7 @@ const ChannelInner = <
794
781
795
782
const jumpToLatestMessage : ChannelActionContextValue < StreamChatGenerics > [ 'jumpToLatestMessage' ] = useCallback ( async ( ) => {
796
783
await channel . state . loadMessageIntoState ( 'latest' ) ;
797
- // FIXME: we cannot rely on constant value 25 as the page size can be customized by integrators
798
- const hasMoreOlder = channel . state . messages . length >= 25 ;
799
- loadMoreFinished ( hasMoreOlder , channel . state . messages ) ;
784
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
800
785
dispatch ( {
801
786
type : 'jumpToLatestMessage' ,
802
787
} ) ;
@@ -811,7 +796,6 @@ const ChannelInner = <
811
796
let lastReadMessageId = channelUnreadUiState ?. last_read_message_id ;
812
797
let firstUnreadMessageId = channelUnreadUiState ?. first_unread_message_id ;
813
798
let isInCurrentMessageSet = false ;
814
- let hasMoreMessages = true ;
815
799
816
800
if ( firstUnreadMessageId ) {
817
801
const result = findInMsgSetById ( firstUnreadMessageId , channel . state . messages ) ;
@@ -850,14 +834,14 @@ const ChannelInner = <
850
834
) . messages ;
851
835
} catch ( e ) {
852
836
addNotification ( t ( 'Failed to jump to the first unread message' ) , 'error' ) ;
853
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
837
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
854
838
return ;
855
839
}
856
840
857
841
const firstMessageWithCreationDate = messages . find ( ( msg ) => msg . created_at ) ;
858
842
if ( ! firstMessageWithCreationDate ) {
859
843
addNotification ( t ( 'Failed to jump to the first unread message' ) , 'error' ) ;
860
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
844
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
861
845
return ;
862
846
}
863
847
const firstMessageTimestamp = new Date (
@@ -866,13 +850,11 @@ const ChannelInner = <
866
850
if ( lastReadTimestamp < firstMessageTimestamp ) {
867
851
// whole channel is unread
868
852
firstUnreadMessageId = firstMessageWithCreationDate . id ;
869
- hasMoreMessages = false ;
870
853
} else {
871
854
const result = findInMsgSetByDate ( channelUnreadUiState . last_read , messages ) ;
872
855
lastReadMessageId = result . target ?. id ;
873
- hasMoreMessages = result . index >= Math . floor ( queryMessageLimit / 2 ) ;
874
856
}
875
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
857
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
876
858
}
877
859
}
878
860
@@ -893,13 +875,12 @@ const ChannelInner = <
893
875
const indexOfTarget = channel . state . messages . findIndex (
894
876
( message ) => message . id === targetId ,
895
877
) as number ;
896
- hasMoreMessages = indexOfTarget >= Math . floor ( queryMessageLimit / 2 ) ;
897
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
878
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
898
879
firstUnreadMessageId =
899
880
firstUnreadMessageId ?? channel . state . messages [ indexOfTarget + 1 ] ?. id ;
900
881
} catch ( e ) {
901
882
addNotification ( t ( 'Failed to jump to the first unread message' ) , 'error' ) ;
902
- loadMoreFinished ( hasMoreMessages , channel . state . messages ) ;
883
+ loadMoreFinished ( channel . state . messagePagination . hasPrev , channel . state . messages ) ;
903
884
return ;
904
885
}
905
886
}
@@ -916,7 +897,7 @@ const ChannelInner = <
916
897
} ) ;
917
898
918
899
dispatch ( {
919
- hasMoreNewer : channel . state . messages !== channel . state . latestMessages ,
900
+ hasMoreNewer : channel . state . messagePagination . hasNext ,
920
901
highlightedMessageId : firstUnreadMessageId ,
921
902
type : 'jumpToMessageFinished' ,
922
903
} ) ;
0 commit comments