Skip to content
Merged
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
19 changes: 15 additions & 4 deletions src/components/MessageList/hooks/useLastDeliveredData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useCallback, useEffect, useState } from 'react';
import type { Channel, LocalMessage, UserResponse } from 'stream-chat';

type UseLastDeliveredDataParams = {
Expand All @@ -7,10 +7,11 @@ type UseLastDeliveredDataParams = {
returnAllReadData: boolean;
};

export const useLastDeliveredData = (props: UseLastDeliveredDataParams) => {
export const useLastDeliveredData = (
props: UseLastDeliveredDataParams,
): Record<string, UserResponse[]> => {
const { channel, messages, returnAllReadData } = props;

return useMemo(
const calculate = useCallback(
() =>
returnAllReadData
? messages.reduce(
Expand All @@ -26,4 +27,14 @@ export const useLastDeliveredData = (props: UseLastDeliveredDataParams) => {
: channel.messageReceiptsTracker.groupUsersByLastDeliveredMessage(),
[channel, messages, returnAllReadData],
);

const [deliveredTo, setDeliveredTo] =
useState<Record<string, UserResponse[]>>(calculate);

useEffect(
() => channel.on('message.delivered', () => setDeliveredTo(calculate)).unsubscribe,
[channel, calculate],
);

return deliveredTo;
};