Skip to content

Commit be05c94

Browse files
authored
fix: incorrect read state for messages (#2888)
* fix: incorrect read state for messages * fix: lint issues
1 parent bf79287 commit be05c94

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

package/src/components/Message/MessageSimple/MessageStatus.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ import { MessageStatusTypes } from '../../../utils/utils';
1414

1515
import { isMessageWithStylesReadByAndDateSeparator } from '../../MessageList/hooks/useMessageList';
1616

17-
const styles = StyleSheet.create({
18-
readByCount: {
19-
fontSize: 11,
20-
fontWeight: '700',
21-
paddingRight: 3,
22-
},
23-
statusContainer: {
24-
alignItems: 'flex-end',
25-
flexDirection: 'row',
26-
justifyContent: 'center',
27-
paddingRight: 3,
28-
},
29-
});
30-
3117
export type MessageStatusPropsWithContext<
3218
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
3319
> = Pick<MessageContextValue<StreamChatGenerics>, 'message' | 'threadList'>;
@@ -131,3 +117,17 @@ export const MessageStatus = <
131117
};
132118

133119
MessageStatus.displayName = 'MessageStatus{messageSimple{status}}';
120+
121+
const styles = StyleSheet.create({
122+
readByCount: {
123+
fontSize: 11,
124+
fontWeight: '700',
125+
paddingRight: 3,
126+
},
127+
statusContainer: {
128+
alignItems: 'flex-end',
129+
flexDirection: 'row',
130+
justifyContent: 'center',
131+
paddingRight: 3,
132+
},
133+
});
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 { useChannelContext } from '../../../contexts/channelContext/ChannelContext';
46
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
57
import {
@@ -11,7 +13,6 @@ import { useThreadContext } from '../../../contexts/threadContext/ThreadContext'
1113
import type { DefaultStreamChatGenerics } from '../../../types/types';
1214
import { getDateSeparators } from '../utils/getDateSeparators';
1315
import { getGroupStyles } from '../utils/getGroupStyles';
14-
import { getReadStates } from '../utils/getReadStates';
1516

1617
export type UseMessageListParams = {
1718
deletedMessagesVisibilityType?: DeletedMessagesVisibilityType;
@@ -78,7 +79,11 @@ export const useMessageList = <
7879
userId: client.userID,
7980
});
8081

81-
const readData = getReadStates(client.userID, messageList, readList);
82+
const readData = useLastReadData({
83+
messages: messageList,
84+
read: readList,
85+
userID: client.userID,
86+
});
8287

8388
const messagesWithStylesReadByAndDateSeparator = messageList
8489
.filter((msg) => {

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

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

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

36-
// if true, save other user's read data for all messages they've read
37-
if (!readData[userLastReadMsgId]) {
38-
readData[userLastReadMsgId] = 0;
39-
}
40-
41-
// Only increment read count if the message is not sent by the current user
42-
if (msg.user?.id !== clientUserId) {
36+
if (returnAllReadData) {
37+
// if true, save other user's read data for all messages they've read
38+
if (!readData[userLastReadMsgId]) {
39+
readData[userLastReadMsgId] = 0;
40+
}
4341
readData[userLastReadMsgId] = readData[userLastReadMsgId] + 1;
4442
}
4543
}
4644
});
4745

4846
// if true, only save read data for other user's last read message
49-
if (userLastReadMsgId) {
47+
if (userLastReadMsgId && !returnAllReadData) {
5048
if (!readData[userLastReadMsgId]) {
5149
readData[userLastReadMsgId] = 0;
5250
}

0 commit comments

Comments
 (0)