Skip to content

Commit 34ddec9

Browse files
authored
fix: read state for messages (#2890)
1 parent efe1074 commit 34ddec9

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useMemo } from 'react';
2+
3+
import type { ChannelState } from 'stream-chat';
4+
5+
import { PaginatedMessageListContextValue } from '../../../contexts/paginatedMessageListContext/PaginatedMessageListContext';
6+
import { ThreadContextValue } from '../../../contexts/threadContext/ThreadContext';
7+
import type { DefaultStreamChatGenerics } from '../../../types/types';
8+
import { getReadStates } from '../utils/getReadStates';
9+
10+
type UseLastReadDataParams<
11+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
12+
> = {
13+
messages:
14+
| PaginatedMessageListContextValue<StreamChatGenerics>['messages']
15+
| ThreadContextValue<StreamChatGenerics>['threadMessages'];
16+
userID: string | undefined;
17+
read?: ChannelState<StreamChatGenerics>['read'];
18+
returnAllReadData?: boolean;
19+
};
20+
21+
export const useLastReadData = <
22+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
23+
>(
24+
props: UseLastReadDataParams<StreamChatGenerics>,
25+
) => {
26+
const { messages, read, returnAllReadData = true, userID } = props;
27+
28+
return useMemo(
29+
() =>
30+
getReadStates(
31+
messages.filter(({ user }) => user?.id === userID),
32+
read,
33+
returnAllReadData,
34+
),
35+
[messages, read, returnAllReadData, userID],
36+
);
37+
};

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { ChannelState, MessageResponse } from 'stream-chat';
22

3+
import { useLastReadData } from './useLastReadData';
4+
35
import {
46
ChannelContextValue,
57
useChannelContext,
@@ -14,7 +16,6 @@ import { useThreadContext } from '../../../contexts/threadContext/ThreadContext'
1416
import type { DefaultStreamChatGenerics } from '../../../types/types';
1517
import { getDateSeparators } from '../utils/getDateSeparators';
1618
import { getGroupStyles } from '../utils/getGroupStyles';
17-
import { getReadStates } from '../utils/getReadStates';
1819

1920
export type UseMessageListParams = {
2021
deletedMessagesVisibilityType?: DeletedMessagesVisibilityType;
@@ -81,7 +82,11 @@ export const useMessageList = <
8182
userId: client.userID,
8283
});
8384

84-
const readData = getReadStates(client.userID, messageList, readList);
85+
const readData = useLastReadData({
86+
messages: messageList,
87+
read: readList,
88+
userID: client.userID,
89+
});
8590

8691
const messagesWithStylesReadByAndDateSeparator = messageList
8792
.filter((msg) => {

package/src/components/MessageList/utils/getReadStates.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import type { DefaultStreamChatGenerics } from '../../../types/types';
66
export const getReadStates = <
77
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
88
>(
9-
clientUserId: string | undefined,
109
messages:
1110
| PaginatedMessageListContextValue<StreamChatGenerics>['messages']
1211
| ThreadContextValue<StreamChatGenerics>['threadMessages'],
1312
read?: ChannelContextValue<StreamChatGenerics>['read'],
13+
returnAllReadData?: boolean,
1414
) => {
1515
const readData: Record<string, number> = {};
1616

@@ -32,20 +32,20 @@ export const getReadStates = <
3232
if (msg.created_at && msg.created_at < readState.last_read) {
3333
userLastReadMsgId = msg.id;
3434

35-
// if true, save other user's read data for all messages they've read
36-
if (!readData[userLastReadMsgId]) {
37-
readData[userLastReadMsgId] = 0;
38-
}
35+
if (returnAllReadData) {
36+
// if true, save other user's read data for all messages they've read
37+
if (!readData[userLastReadMsgId]) {
38+
readData[userLastReadMsgId] = 0;
39+
}
3940

40-
// Only increment read count if the message is not sent by the current user
41-
if (msg.user?.id !== clientUserId) {
41+
// Only increment read count if the message is not sent by the current user
4242
readData[userLastReadMsgId] = readData[userLastReadMsgId] + 1;
4343
}
4444
}
4545
});
4646

4747
// if true, only save read data for other user's last read message
48-
if (userLastReadMsgId) {
48+
if (userLastReadMsgId && !returnAllReadData) {
4949
if (!readData[userLastReadMsgId]) {
5050
readData[userLastReadMsgId] = 0;
5151
}

0 commit comments

Comments
 (0)