Skip to content

Commit bf783d1

Browse files
committed
fix: incorrect read state for messages
1 parent bf79287 commit bf783d1

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useThreadContext } from '../../../contexts/threadContext/ThreadContext'
1111
import type { DefaultStreamChatGenerics } from '../../../types/types';
1212
import { getDateSeparators } from '../utils/getDateSeparators';
1313
import { getGroupStyles } from '../utils/getGroupStyles';
14-
import { getReadStates } from '../utils/getReadStates';
14+
import { useLastReadData } from './useLastReadData';
1515

1616
export type UseMessageListParams = {
1717
deletedMessagesVisibilityType?: DeletedMessagesVisibilityType;
@@ -78,7 +78,11 @@ export const useMessageList = <
7878
userId: client.userID,
7979
});
8080

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

8387
const messagesWithStylesReadByAndDateSeparator = messageList
8488
.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)