@@ -26,7 +26,7 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
2626 const { sdk, sbOptions, groupChannelFragmentOptions } = useSendbirdChat ( ) ;
2727 const { setMessageToEdit, setMessageToReply } = useContext ( GroupChannelContexts . Fragment ) ;
2828 const groupChannelPubSub = useContext ( GroupChannelContexts . PubSub ) ;
29- const { flatListRef, lazyScrollToBottom, lazyScrollToIndex , onPressReplyMessageInThread } = useContext (
29+ const { flatListRef, lazyScrollToBottom, lazyScrollToMessageId , onPressReplyMessageInThread } = useContext (
3030 GroupChannelContexts . MessageList ,
3131 ) ;
3232
@@ -40,6 +40,7 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
4040 const viewableMessages = useRef < SendbirdMessage [ ] > ( ) ;
4141 const hasUserMarkedAsUnreadRef = useRef ( false ) ;
4242 const [ unreadFirstMessage , setUnreadFirstMessage ] = useState < SendbirdMessage | undefined > ( undefined ) ;
43+ const pendingBottomReachedRef = useRef < { timeout : number ; timestamp : number } | null > ( null ) ;
4344
4445 const updateHasSeenNewLine = useCallback (
4546 ( hasSeenNewLine : boolean ) => {
@@ -63,14 +64,16 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
6364
6465 const scrollToMessageWithCreatedAt = useFreshCallback (
6566 ( createdAt : number , focusAnimated : boolean , timeout : number ) : boolean => {
66- const foundMessageIndex = props . messages . findIndex ( ( it ) => it . createdAt === createdAt ) ;
67- const isIncludedInList = foundMessageIndex > - 1 ;
67+ const foundMessage = props . messages . find ( ( it ) => it . createdAt === createdAt ) ;
68+ const isIncludedInList = ! ! foundMessage ;
69+ pendingBottomReachedRef . current = null ;
6870
6971 if ( isIncludedInList ) {
7072 if ( focusAnimated ) {
7173 setTimeout ( ( ) => props . onUpdateSearchItem ( { startingPoint : createdAt } ) , MESSAGE_FOCUS_ANIMATION_DELAY ) ;
7274 }
73- lazyScrollToIndex ( { index : foundMessageIndex , animated : true , timeout } ) ;
75+ pendingBottomReachedRef . current = { timeout, timestamp : Date . now ( ) } ;
76+ lazyScrollToMessageId ( { messageId : foundMessage . messageId , animated : true , timeout } ) ;
7477 } else {
7578 if ( props . channel . messageOffsetTimestamp <= createdAt ) {
7679 if ( focusAnimated ) {
@@ -346,6 +349,23 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
346349 } ,
347350 ) ;
348351
352+ const onBottomReached = useFreshCallback ( ( ) => {
353+ if ( props . hasNext ( ) ) {
354+ if ( pendingBottomReachedRef . current ) {
355+ const currentTime = Date . now ( ) ;
356+ const elapsedTime = currentTime - pendingBottomReachedRef . current . timestamp ;
357+
358+ const timeoutThreshold = 500 ;
359+ if ( elapsedTime >= pendingBottomReachedRef . current . timeout + timeoutThreshold ) {
360+ props . onBottomReached ?.( ) ;
361+ pendingBottomReachedRef . current = null ;
362+ }
363+ } else {
364+ props . onBottomReached ?.( ) ;
365+ }
366+ }
367+ } ) ;
368+
349369 return (
350370 < ChannelMessageList
351371 { ...props }
@@ -359,6 +379,7 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
359379 onPressNewMessagesButton = { scrollToBottom }
360380 onPressScrollToBottomButton = { scrollToBottom }
361381 onPressMarkAsUnreadMessage = { onPressMarkAsUnreadMessage }
382+ onBottomReached = { onBottomReached }
362383 unreadFirstMessage = { unreadFirstMessage }
363384 unreadMessagesFloatingProps = { unreadMessagesFloatingPropsRef . current }
364385 />
0 commit comments