Skip to content

Commit 172c3c3

Browse files
authored
feat: export initialMessagesFetch callback from hook (#829)
Addresses https://sendbird.atlassian.net/browse/AC-690 Took the logic inside of useInitialMessagesFetch's effect hook and made a callback function. And made the function to be returned from the hook to make it be from outside of UIKit. #### Before ``` function useInitialMessagesFetch(params): void { useEffect(() => { all the logics.. }, [deps]} } ``` #### After ``` function useInitialMessagesFetch(params): () => void { const fetchMessages() => useCallback(() => { all the logics.. , [deps]} useEffect(() => { fetchMessages(); }, [fetchMessages]} return fetchMessages; } ```
1 parent 7b3c218 commit 172c3c3

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

rollup.module-exports.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default {
5959
// Channel
6060
Channel: 'src/modules/Channel/index.tsx',
6161
'Channel/context': 'src/modules/Channel/context/ChannelProvider.tsx',
62+
'Channel/hooks/useInitialMessagesFetch': 'src/modules/Channel/context/hooks/useInitialMessagesFetch.ts',
6263
'Channel/utils/getMessagePartsInfo': 'src/modules/Channel/components/MessageList/getMessagePartsInfo.ts',
6364
'Channel/utils/compareMessagesForGrouping': 'src/modules/Channel/context/compareMessagesForGrouping.ts',
6465
'Channel/components/ChannelHeader': 'src/modules/Channel/components/ChannelHeader/index.tsx',

src/modules/Channel/context/hooks/useInitialMessagesFetch.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useCallback, useEffect } from 'react';
22
import { MessageListParams, ReplyType } from '@sendbird/chat/message';
33

44
import * as utils from '../utils';
@@ -34,10 +34,17 @@ function useInitialMessagesFetch(
3434
setIsScrolled,
3535
}: UseInitialMessagesFetchOptions,
3636
{ logger, scrollRef, messagesDispatcher }: UseInitialMessagesFetchParams,
37-
) {
37+
): () => void {
3838
const channelUrl = currentGroupChannel?.url;
3939

40-
useEffect(() => {
40+
/**
41+
* useCallback(() => {}, [currentGroupChannel]) was buggy, that is why we did
42+
* const channelUrl = currentGroupChannel && currentGroupChannel.url;
43+
* useCallback(() => {}, [channelUrl])
44+
* Again, this hook is supposed to execute when currentGroupChannel changes
45+
* The 'channelUrl' here is not the same memory reference from Conversation.props
46+
*/
47+
const fetchMessages = useCallback(() => {
4148
logger.info('Channel useInitialMessagesFetch: Setup started', currentGroupChannel);
4249
setIsScrolled(false);
4350
messagesDispatcher({
@@ -129,14 +136,12 @@ function useInitialMessagesFetch(
129136
});
130137
}
131138
}, [channelUrl, userFilledMessageListQuery, initialTimeStamp]);
132-
/**
133-
* Note - useEffect(() => {}, [currentGroupChannel])
134-
* was buggy, that is why we did
135-
* const channelUrl = currentGroupChannel && currentGroupChannel.url;
136-
* useEffect(() => {}, [channelUrl])
137-
* Again, this hook is supposed to execute when currentGroupChannel changes
138-
* The 'channelUrl' here is not the same memory reference from Conversation.props
139-
*/
139+
140+
useEffect(() => {
141+
fetchMessages();
142+
}, [fetchMessages]);
143+
144+
return fetchMessages;
140145
}
141146

142147
export default useInitialMessagesFetch;

0 commit comments

Comments
 (0)