@@ -10,13 +10,15 @@ import ChannelMessageList from '../../../components/ChannelMessageList';
1010import { MESSAGE_FOCUS_ANIMATION_DELAY , MESSAGE_SEARCH_SAFE_SCROLL_DELAY } from '../../../constants' ;
1111import { useLocalization , useSendbirdChat } from '../../../hooks/useContext' ;
1212import { GroupChannelContexts } from '../module/moduleContext' ;
13- import type { GroupChannelProps } from '../types' ;
13+ import type { GroupChannelProps , GroupChannelScrollToMessageFunc } from '../types' ;
1414
1515const GroupChannelMessageList = ( props : GroupChannelProps [ 'MessageList' ] ) => {
1616 const toast = useToast ( ) ;
1717 const { STRINGS } = useLocalization ( ) ;
1818 const { sdk } = useSendbirdChat ( ) ;
19- const { setMessageToEdit, setMessageToReply } = useContext ( GroupChannelContexts . Fragment ) ;
19+ const { setMessageToEdit, setMessageToReply, __internalSetScrollToMessageFunc } = useContext (
20+ GroupChannelContexts . Fragment ,
21+ ) ;
2022 const { subscribe } = useContext ( GroupChannelContexts . PubSub ) ;
2123
2224 const id = useUniqHandlerId ( 'GroupChannelMessageList' ) ;
@@ -31,13 +33,31 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
3133 } ;
3234
3335 // FIXME: Workaround, should run after data has been applied to UI.
34- const lazyScrollToIndex = ( index = 0 , animated = false , timeout = 0 ) => {
36+ const lazyScrollToIndex = ( index = 0 , animated = false , timeout = 0 , viewPosition = 0.5 ) => {
3537 setTimeout ( ( ) => {
36- ref . current ?. scrollToIndex ( { index, animated, viewPosition : 0.5 } ) ;
38+ ref . current ?. scrollToIndex ( { index, animated, viewPosition } ) ;
3739 } , timeout ) ;
3840 } ;
3941
40- const scrollToMessage = useFreshCallback ( ( createdAt : number , focusAnimated = false ) : boolean => {
42+ const scrollToMessage = useFreshCallback < GroupChannelScrollToMessageFunc > ( ( messageId , options ) => {
43+ const foundMessageIndex = props . messages . findIndex ( ( it ) => it . messageId === messageId ) ;
44+ const isIncludedInList = foundMessageIndex > - 1 ;
45+
46+ if ( isIncludedInList ) {
47+ if ( options ?. focusAnimated ) {
48+ setTimeout (
49+ ( ) => props . onUpdateSearchItem ( { startingPoint : props . messages [ foundMessageIndex ] . createdAt } ) ,
50+ MESSAGE_FOCUS_ANIMATION_DELAY ,
51+ ) ;
52+ }
53+ lazyScrollToIndex ( foundMessageIndex , true , 0 , options ?. viewPosition ) ;
54+ return true ;
55+ } else {
56+ return false ;
57+ }
58+ } ) ;
59+
60+ const scrollToMessageWithCreatedAt = useFreshCallback ( ( createdAt : number , focusAnimated = false ) : boolean => {
4161 const foundMessageIndex = props . messages . findIndex ( ( it ) => it . createdAt === createdAt ) ;
4262 const isIncludedInList = foundMessageIndex > - 1 ;
4363
@@ -54,7 +74,6 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
5474 return false ;
5575 }
5676 }
57-
5877 return true ;
5978 } ) ;
6079
@@ -102,17 +121,21 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
102121 } ) ;
103122 } , [ props . scrolledAwayFromBottom ] ) ;
104123
105- // Only trigger once when message list mount with initial props.searchItem
106- // - Search screen + searchItem > mount message list
107- // - Reset message list + searchItem > re-mount message list
108124 useEffect ( ( ) => {
125+ // Only trigger once when message list mount with initial props.searchItem
126+ // - Search screen + searchItem > mount message list
127+ // - Reset message list + searchItem > re-mount message list
109128 if ( isFirstMount && props . searchItem ) {
110- scrollToMessage ( props . searchItem . startingPoint ) ;
129+ scrollToMessageWithCreatedAt ( props . searchItem . startingPoint ) ;
111130 }
112131 } , [ isFirstMount ] ) ;
113132
133+ useEffect ( ( ) => {
134+ __internalSetScrollToMessageFunc ( ( ) => scrollToMessage ) ;
135+ } , [ ] ) ;
136+
114137 const onPressParentMessage = useFreshCallback ( ( message : SendbirdMessage ) => {
115- const canScrollToParent = scrollToMessage ( message . createdAt , true ) ;
138+ const canScrollToParent = scrollToMessageWithCreatedAt ( message . createdAt , true ) ;
116139 if ( ! canScrollToParent ) toast . show ( STRINGS . TOAST . FIND_PARENT_MSG_ERROR , 'error' ) ;
117140 } ) ;
118141
0 commit comments