Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect read state for messages #2888

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions package/src/components/Message/MessageSimple/MessageStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ import { MessageStatusTypes } from '../../../utils/utils';

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

const styles = StyleSheet.create({
readByCount: {
fontSize: 11,
fontWeight: '700',
paddingRight: 3,
},
statusContainer: {
alignItems: 'flex-end',
flexDirection: 'row',
justifyContent: 'center',
paddingRight: 3,
},
});

export type MessageStatusPropsWithContext<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = Pick<MessageContextValue<StreamChatGenerics>, 'message' | 'threadList'>;
Expand Down Expand Up @@ -131,3 +117,17 @@ export const MessageStatus = <
};

MessageStatus.displayName = 'MessageStatus{messageSimple{status}}';

const styles = StyleSheet.create({
readByCount: {
fontSize: 11,
fontWeight: '700',
paddingRight: 3,
},
statusContainer: {
alignItems: 'flex-end',
flexDirection: 'row',
justifyContent: 'center',
paddingRight: 3,
},
});
37 changes: 37 additions & 0 deletions package/src/components/MessageList/hooks/useLastReadData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useMemo } from 'react';

import type { ChannelState } from 'stream-chat';

import { PaginatedMessageListContextValue } from '../../../contexts/paginatedMessageListContext/PaginatedMessageListContext';
import { ThreadContextValue } from '../../../contexts/threadContext/ThreadContext';
import type { DefaultStreamChatGenerics } from '../../../types/types';
import { getReadStates } from '../utils/getReadStates';

type UseLastReadDataParams<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = {
messages:
| PaginatedMessageListContextValue<StreamChatGenerics>['messages']
| ThreadContextValue<StreamChatGenerics>['threadMessages'];
userID: string | undefined;
read?: ChannelState<StreamChatGenerics>['read'];
returnAllReadData?: boolean;
};

export const useLastReadData = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
props: UseLastReadDataParams<StreamChatGenerics>,
) => {
const { messages, read, returnAllReadData = true, userID } = props;

return useMemo(
() =>
getReadStates(
messages.filter(({ user }) => user?.id === userID),
read,
returnAllReadData,
),
[messages, read, returnAllReadData, userID],
);
};
9 changes: 7 additions & 2 deletions package/src/components/MessageList/hooks/useMessageList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ChannelState, MessageResponse } from 'stream-chat';

import { useLastReadData } from './useLastReadData';

import { useChannelContext } from '../../../contexts/channelContext/ChannelContext';
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
import {
Expand All @@ -11,7 +13,6 @@ import { useThreadContext } from '../../../contexts/threadContext/ThreadContext'
import type { DefaultStreamChatGenerics } from '../../../types/types';
import { getDateSeparators } from '../utils/getDateSeparators';
import { getGroupStyles } from '../utils/getGroupStyles';
import { getReadStates } from '../utils/getReadStates';

export type UseMessageListParams = {
deletedMessagesVisibilityType?: DeletedMessagesVisibilityType;
Expand Down Expand Up @@ -78,7 +79,11 @@ export const useMessageList = <
userId: client.userID,
});

const readData = getReadStates(client.userID, messageList, readList);
const readData = useLastReadData({
messages: messageList,
read: readList,
userID: client.userID,
});

const messagesWithStylesReadByAndDateSeparator = messageList
.filter((msg) => {
Expand Down
16 changes: 7 additions & 9 deletions package/src/components/MessageList/utils/getReadStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import type { DefaultStreamChatGenerics } from '../../../types/types';
export const getReadStates = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
clientUserId: string | undefined,
messages:
| PaginatedMessageListContextValue<StreamChatGenerics>['messages']
| ThreadContextValue<StreamChatGenerics>['threadMessages'],
read?: ChannelState<StreamChatGenerics>['read'],
returnAllReadData?: boolean,
) => {
const readData: Record<string, number> = {};

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

// if true, save other user's read data for all messages they've read
if (!readData[userLastReadMsgId]) {
readData[userLastReadMsgId] = 0;
}

// Only increment read count if the message is not sent by the current user
if (msg.user?.id !== clientUserId) {
if (returnAllReadData) {
// if true, save other user's read data for all messages they've read
if (!readData[userLastReadMsgId]) {
readData[userLastReadMsgId] = 0;
}
readData[userLastReadMsgId] = readData[userLastReadMsgId] + 1;
}
}
});

// if true, only save read data for other user's last read message
if (userLastReadMsgId) {
if (userLastReadMsgId && !returnAllReadData) {
if (!readData[userLastReadMsgId]) {
readData[userLastReadMsgId] = 0;
}
Expand Down
Loading