Skip to content

Commit 1427f89

Browse files
committed
fix: ensure message delivery data are recalculated for the latest message delivery confirmation
1 parent cf3ad0f commit 1427f89

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/components/MessageList/hooks/useLastDeliveredData.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo } from 'react';
1+
import { useCallback, useEffect, useState } from 'react';
22
import type { Channel, LocalMessage, UserResponse } from 'stream-chat';
33

44
type UseLastDeliveredDataParams = {
@@ -7,10 +7,11 @@ type UseLastDeliveredDataParams = {
77
returnAllReadData: boolean;
88
};
99

10-
export const useLastDeliveredData = (props: UseLastDeliveredDataParams) => {
10+
export const useLastDeliveredData = (
11+
props: UseLastDeliveredDataParams,
12+
): Record<string, UserResponse[]> => {
1113
const { channel, messages, returnAllReadData } = props;
12-
13-
return useMemo(
14+
const calculate = useCallback(
1415
() =>
1516
returnAllReadData
1617
? messages.reduce(
@@ -26,4 +27,14 @@ export const useLastDeliveredData = (props: UseLastDeliveredDataParams) => {
2627
: channel.messageReceiptsTracker.groupUsersByLastDeliveredMessage(),
2728
[channel, messages, returnAllReadData],
2829
);
30+
31+
const [deliveredTo, setDeliveredTo] =
32+
useState<Record<string, UserResponse[]>>(calculate);
33+
34+
useEffect(
35+
() => channel.on('message.delivered', () => setDeliveredTo(calculate)).unsubscribe,
36+
[channel, calculate],
37+
);
38+
39+
return deliveredTo;
2940
};

0 commit comments

Comments
 (0)