Skip to content

Commit

Permalink
fix: read state for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Jan 10, 2025
1 parent efe1074 commit 09fc9b2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
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 {
ChannelContextValue,
useChannelContext,
Expand All @@ -14,7 +16,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 @@ -81,7 +82,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: 8 additions & 8 deletions package/src/components/MessageList/utils/getReadStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,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?: ChannelContextValue<StreamChatGenerics>['read'],
returnAllReadData?: boolean,
) => {
const readData: Record<string, number> = {};

Expand All @@ -32,20 +32,20 @@ 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;
}
if (returnAllReadData) {
// 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) {
// Only increment read count if the message is not sent by the current user
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

0 comments on commit 09fc9b2

Please sign in to comment.