Skip to content

Commit 366516e

Browse files
committed
fix: implementation
1 parent a8e3803 commit 366516e

File tree

4 files changed

+40
-139
lines changed

4 files changed

+40
-139
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { ScrollViewProps, StyleSheet, View, ViewabilityConfig, ViewToken } from
44
import { FlashList, FlashListProps, FlashListRef } from '@shopify/flash-list';
55
import type { Channel, Event, LocalMessage, MessageResponse } from 'stream-chat';
66

7-
import { useMessageFlashList } from './hooks/useMessageFlashList';
8-
7+
import { useMessageList } from './hooks/useMessageList';
98
import { useShouldScrollToRecentOnNewOwnMessage } from './hooks/useShouldScrollToRecentOnNewOwnMessage';
109
import { InlineLoadingMoreIndicator } from './InlineLoadingMoreIndicator';
1110
import { InlineLoadingMoreRecentIndicator } from './InlineLoadingMoreRecentIndicator';
1211
import { InlineLoadingMoreRecentThreadIndicator } from './InlineLoadingMoreRecentThreadIndicator';
13-
import { getLastReceivedMessage } from './utils/getLastReceivedMessage';
12+
13+
import { getLastReceivedMessageFlashList } from './utils/getLastReceivedMessageFlashList';
1414

1515
import {
1616
AttachmentPickerContextValue,
@@ -263,7 +263,6 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
263263
*/
264264
const scrollToDebounceTimeoutRef = useRef<ReturnType<typeof setTimeout>>(undefined);
265265

266-
const messageListLengthBeforeUpdate = useRef(0);
267266
const channelResyncScrollSet = useRef<boolean>(true);
268267
const { theme } = useTheme();
269268

@@ -281,7 +280,8 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
281280
);
282281

283282
const { dateSeparatorsRef, messageGroupStylesRef, processedMessageList, rawMessageList } =
284-
useMessageFlashList({
283+
useMessageList({
284+
isFlashList: true,
285285
noGroupByUser,
286286
threadList,
287287
});
@@ -295,6 +295,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
295295

296296
const latestNonCurrentMessageBeforeUpdateRef = useRef<LocalMessage>(undefined);
297297

298+
const messageListLengthBeforeUpdate = useRef(0);
298299
const messageListLengthAfterUpdate = processedMessageList.length;
299300

300301
const shouldScrollToRecentOnNewOwnMessageRef = useShouldScrollToRecentOnNewOwnMessage(
@@ -303,7 +304,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
303304
);
304305

305306
const lastReceivedId = useMemo(
306-
() => getLastReceivedMessage(processedMessageList)?.id,
307+
() => getLastReceivedMessageFlashList(processedMessageList)?.id,
307308
[processedMessageList],
308309
);
309310

@@ -338,22 +339,18 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
338339
loadChannelAroundMessage({ messageId: targetedMessage, setTargetedMessage });
339340
} else {
340341
scrollToDebounceTimeoutRef.current = setTimeout(() => {
341-
if (!flashListRef.current) {
342-
return;
343-
}
344342
clearTimeout(scrollToDebounceTimeoutRef.current);
345343

346344
// now scroll to it
347-
flashListRef.current.scrollToIndex({
345+
flashListRef.current?.scrollToIndex({
348346
animated: true,
349347
index: indexOfParentInMessageList,
350348
viewPosition: 0.5,
351349
});
352350
setTargetedMessage(undefined);
353351
}, WAIT_FOR_SCROLL_TIMEOUT);
354352
}
355-
// eslint-disable-next-line react-hooks/exhaustive-deps
356-
}, [targetedMessage]);
353+
}, [loadChannelAroundMessage, processedMessageList, setTargetedMessage, targetedMessage]);
357354

358355
const goToMessage = useStableCallback(async (messageId: string) => {
359356
const indexOfParentInMessageList = processedMessageList.findIndex(
@@ -460,7 +457,6 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
460457

461458
if (!didMergeMessageSetsWithNoUpdates) {
462459
const shouldScrollToRecentOnNewOwnMessage = shouldScrollToRecentOnNewOwnMessageRef.current();
463-
464460
// we should scroll to bottom where ever we are now
465461
// as we have sent a new own message
466462
if (shouldScrollToRecentOnNewOwnMessage) {
@@ -469,6 +465,9 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
469465
}
470466
}, [channel, processedMessageList, shouldScrollToRecentOnNewOwnMessageRef, threadList]);
471467

468+
/**
469+
* Effect to scroll to the bottom of the message list when a new message is received if the scroll to bottom button is not visible.
470+
*/
472471
useEffect(() => {
473472
const handleEvent = (event: Event) => {
474473
if (event.message?.user?.id !== client.userID) {

package/src/components/MessageList/hooks/useMessageFlashList.ts

Lines changed: 0 additions & 123 deletions
This file was deleted.

package/src/components/MessageList/hooks/useMessageList.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type UseMessageListParams = {
2020
noGroupByUser?: boolean;
2121
threadList?: boolean;
2222
isLiveStreaming?: boolean;
23+
isFlashList?: boolean;
2324
};
2425

2526
export type GroupType = string;
@@ -50,7 +51,7 @@ export const shouldIncludeMessageInList = (
5051
};
5152

5253
export const useMessageList = (params: UseMessageListParams) => {
53-
const { noGroupByUser, threadList, isLiveStreaming } = params;
54+
const { noGroupByUser, threadList, isLiveStreaming, isFlashList } = params;
5455
const { client } = useChatContext();
5556
const { hideDateSeparators, maxTimeBetweenGroupedMessages } = useChannelContext();
5657
const { deletedMessagesVisibilityType, getMessagesGroupStyles = getGroupStyles } =
@@ -106,11 +107,15 @@ export const useMessageList = (params: UseMessageListParams) => {
106107
userId: client.userID,
107108
})
108109
) {
109-
newMessageList.unshift(message);
110+
if (isFlashList) {
111+
newMessageList.push(message);
112+
} else {
113+
newMessageList.unshift(message);
114+
}
110115
}
111116
}
112117
return newMessageList;
113-
}, [client.userID, deletedMessagesVisibilityType, messageList]);
118+
}, [client.userID, deletedMessagesVisibilityType, isFlashList, messageList]);
114119

115120
const data = useRAFCoalescedValue(processedMessageList, isLiveStreaming);
116121

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { LocalMessage } from 'stream-chat';
2+
3+
import { MessageStatusTypes } from '../../../utils/utils';
4+
5+
export const getLastReceivedMessageFlashList = (messages: LocalMessage[]) => {
6+
/**
7+
* There are no status on dates so they will be skipped
8+
*/
9+
for (let i = messages.length - 1; i >= 0; i--) {
10+
const message = messages[i];
11+
if (
12+
message?.status === MessageStatusTypes.RECEIVED ||
13+
message?.status === MessageStatusTypes.SENDING
14+
) {
15+
return message;
16+
}
17+
}
18+
19+
return;
20+
};

0 commit comments

Comments
 (0)