Skip to content

Commit 3f6d111

Browse files
committed
fix: confine readBy data to Message Status component
1 parent 65881a7 commit 3f6d111

File tree

8 files changed

+37
-113
lines changed

8 files changed

+37
-113
lines changed

package/src/components/Message/Message.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
MessageStatusTypes,
4242
} from '../../utils/utils';
4343
import type { Thumbnail } from '../Attachment/utils/buildGallery/types';
44+
import { getReadState } from '../MessageList/utils/getReadState';
4445

4546
export type TouchableEmitter =
4647
| 'fileAttachment'
@@ -136,7 +137,7 @@ export type MessagePropsWithContext = Pick<
136137
Partial<
137138
Omit<MessageContextValue, 'groupStyles' | 'handleReaction' | 'message' | 'isMessageAIGenerated'>
138139
> &
139-
Pick<MessageContextValue, 'groupStyles' | 'message' | 'isMessageAIGenerated' | 'readBy'> &
140+
Pick<MessageContextValue, 'groupStyles' | 'message' | 'isMessageAIGenerated'> &
140141
Pick<
141142
MessagesContextValue,
142143
| 'sendReaction'
@@ -247,7 +248,6 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
247248
onThreadSelect,
248249
openThread,
249250
preventPress,
250-
readBy,
251251
removeMessage,
252252
retrySendMessage,
253253
selectReaction,
@@ -263,6 +263,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
263263
threadList = false,
264264
updateMessage,
265265
} = props;
266+
const { read } = useChannelContext();
266267
const isMessageAIGenerated = messagesContext.isMessageAIGenerated;
267268
const isAIGenerated = useMemo(
268269
() => isMessageAIGenerated(message),
@@ -277,6 +278,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
277278
screenPadding,
278279
},
279280
} = useTheme();
281+
const readBy = useMemo(() => getReadState(message, read), [message, read]);
280282

281283
const showMessageOverlay = async (showMessageReactions = false, selectedReaction?: string) => {
282284
await dismissKeyboard();
@@ -756,7 +758,6 @@ const areEqual = (prevProps: MessagePropsWithContext, nextProps: MessagePropsWit
756758
members: prevMembers,
757759
message: prevMessage,
758760
messagesContext: prevMessagesContext,
759-
readBy: prevReadBy,
760761
showUnreadUnderlay: prevShowUnreadUnderlay,
761762
t: prevT,
762763
} = prevProps;
@@ -769,7 +770,6 @@ const areEqual = (prevProps: MessagePropsWithContext, nextProps: MessagePropsWit
769770
members: nextMembers,
770771
message: nextMessage,
771772
messagesContext: nextMessagesContext,
772-
readBy: nextReadBy,
773773
showUnreadUnderlay: nextShowUnreadUnderlay,
774774
t: nextT,
775775
} = nextProps;
@@ -885,11 +885,6 @@ const areEqual = (prevProps: MessagePropsWithContext, nextProps: MessagePropsWit
885885
return false;
886886
}
887887

888-
const readByEqual = prevReadBy === nextReadBy;
889-
if (!readByEqual) {
890-
return false;
891-
}
892-
893888
const showUnreadUnderlayEqual = prevShowUnreadUnderlay === nextShowUnreadUnderlay;
894889
if (!showUnreadUnderlayEqual) {
895890
return false;
@@ -919,9 +914,9 @@ const areEqual = (prevProps: MessagePropsWithContext, nextProps: MessagePropsWit
919914
const MemoizedMessage = React.memo(MessageWithContext, areEqual) as typeof MessageWithContext;
920915

921916
export type MessageProps = Partial<
922-
Omit<MessagePropsWithContext, 'groupStyles' | 'handleReaction' | 'message' | 'readBy'>
917+
Omit<MessagePropsWithContext, 'groupStyles' | 'handleReaction' | 'message'>
923918
> &
924-
Pick<MessagePropsWithContext, 'groupStyles' | 'message' | 'readBy'>;
919+
Pick<MessagePropsWithContext, 'groupStyles' | 'message'>;
925920

926921
/**
927922
* Message - A high level component which implements all the logic required for a message.

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

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { useTheme } from '../../../contexts/themeContext/ThemeContext';
1010
import { Check } from '../../../icons/Check';
1111
import { CheckAll } from '../../../icons/CheckAll';
1212
import { Time } from '../../../icons/Time';
13-
1413
import { MessageStatusTypes } from '../../../utils/utils';
1514

1615
export type MessageStatusPropsWithContext = Pick<

package/src/components/MessageList/MessageList.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
318318
* NOTE: rawMessageList changes only when messages array state changes
319319
* processedMessageList changes on any state change
320320
*/
321-
const { dateSeparators, messageGroupStyles, processedMessageList, rawMessageList, readData } =
321+
const { dateSeparators, messageGroupStyles, processedMessageList, rawMessageList } =
322322
useMessageList({
323323
noGroupByUser,
324324
threadList,
@@ -766,7 +766,6 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
766766
}
767767
message={message}
768768
onThreadSelect={onThreadSelect}
769-
readBy={readData[message.id]}
770769
showUnreadUnderlay={showUnreadUnderlay}
771770
style={[messageContainer]}
772771
threadList={threadList}
@@ -820,7 +819,6 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
820819
modifiedTheme,
821820
myMessageTheme,
822821
onThreadSelect,
823-
readData,
824822
screenPadding,
825823
shouldApplyAndroidWorkaround,
826824
shouldShowUnreadUnderlay,

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

-29
This file was deleted.

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

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useMemo } from 'react';
22

3-
import type { ChannelState, LocalMessage } from 'stream-chat';
4-
5-
import { useLastReadData } from './useLastReadData';
3+
import type { LocalMessage } from 'stream-chat';
64

75
import { useChannelContext } from '../../../contexts/channelContext/ChannelContext';
86
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
@@ -48,20 +46,13 @@ export const shouldIncludeMessageInList = (
4846
export const useMessageList = (params: UseMessageListParams) => {
4947
const { noGroupByUser, threadList } = params;
5048
const { client } = useChatContext();
51-
const { hideDateSeparators, maxTimeBetweenGroupedMessages, read } = useChannelContext();
49+
const { hideDateSeparators, maxTimeBetweenGroupedMessages } = useChannelContext();
5250
const { deletedMessagesVisibilityType, getMessagesGroupStyles = getGroupStyles } =
5351
useMessagesContext();
5452
const { messages } = usePaginatedMessageListContext();
5553
const { threadMessages } = useThreadContext();
5654

5755
const messageList = threadList ? threadMessages : messages;
58-
const readList: ChannelState['read'] | undefined = threadList ? undefined : read;
59-
60-
const readData = useLastReadData({
61-
messages: messageList,
62-
read: readList,
63-
userID: client.userID,
64-
});
6556

6657
const dateSeparators = useMemo(
6758
() =>
@@ -119,7 +110,5 @@ export const useMessageList = (params: UseMessageListParams) => {
119110
processedMessageList,
120111
/** Raw messages from the channel state */
121112
rawMessageList: messageList,
122-
/** Read data */
123-
readData,
124113
};
125114
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ChannelState, LocalMessage } from 'stream-chat';
2+
3+
/**
4+
* Get the number of users who have read the message
5+
* @param message - The message to get the read state for
6+
* @param read - The read state of the channel
7+
* @returns The number of users who have read the message
8+
*/
9+
export const getReadState = (message: LocalMessage, read?: ChannelState['read']) => {
10+
if (!read) {
11+
return 0;
12+
}
13+
14+
const readState = Object.values(read).reduce((acc, readState) => {
15+
if (!readState.last_read) {
16+
return acc;
17+
}
18+
19+
if (message.created_at && message.created_at < readState.last_read) {
20+
return acc + 1;
21+
}
22+
23+
return acc;
24+
}, 0);
25+
26+
return readState;
27+
};

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

-55
This file was deleted.

package/src/components/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export * from './MessageList/TypingIndicatorContainer';
148148
export * from './MessageList/utils/getDateSeparators';
149149
export * from './MessageList/utils/getGroupStyles';
150150
export * from './MessageList/utils/getLastReceivedMessage';
151-
export * from './MessageList/utils/getReadStates';
151+
export * from './MessageList/utils/getReadState';
152152

153153
export * from './MessageMenu/MessageActionList';
154154
export * from './MessageMenu/MessageActionListItem';

0 commit comments

Comments
 (0)