|
1 | | -import type { IMessage } from '@rocket.chat/core-typings'; |
| 1 | +import type { IMessage, IRoom } from '@rocket.chat/core-typings'; |
2 | 2 | import { GenericModal, GenericModalSkeleton } from '@rocket.chat/ui-client'; |
3 | | -import { useMethod, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; |
4 | | -import { useQuery } from '@tanstack/react-query'; |
| 3 | +import { useEndpoint, useStream, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; |
| 4 | +import { useQuery, useQueryClient } from '@tanstack/react-query'; |
5 | 5 | import type { ReactElement } from 'react'; |
6 | 6 | import { useEffect } from 'react'; |
7 | 7 | import { useTranslation } from 'react-i18next'; |
8 | 8 |
|
9 | 9 | import ReadReceiptRow from './ReadReceiptRow'; |
| 10 | +import { mapReadReceiptFromApi } from '../../../../lib/utils/mapReadReceiptFromApi'; |
10 | 11 |
|
11 | 12 | type ReadReceiptsModalProps = { |
12 | 13 | messageId: IMessage['_id']; |
| 14 | + rid?: IRoom['_id']; |
13 | 15 | onClose: () => void; |
14 | 16 | }; |
15 | 17 |
|
16 | | -const ReadReceiptsModal = ({ messageId, onClose }: ReadReceiptsModalProps): ReactElement => { |
| 18 | +const ReadReceiptsModal = ({ messageId, rid, onClose }: ReadReceiptsModalProps): ReactElement => { |
17 | 19 | const { t } = useTranslation(); |
18 | 20 | const dispatchToastMessage = useToastMessageDispatch(); |
| 21 | + const queryClient = useQueryClient(); |
| 22 | + const subscribeToNotifyRoom = useStream('notify-room'); |
19 | 23 |
|
20 | | - const getReadReceipts = useMethod('getReadReceipts'); |
| 24 | + const getReadReceipts = useEndpoint('GET', '/v1/chat.getMessageReadReceipts'); |
| 25 | + |
| 26 | + const queryKey = ['read-receipts', messageId]; |
21 | 27 |
|
22 | 28 | const readReceiptsResult = useQuery({ |
23 | | - queryKey: ['read-receipts', messageId], |
24 | | - queryFn: () => getReadReceipts({ messageId }), |
| 29 | + queryKey, |
| 30 | + queryFn: async () => (await getReadReceipts({ messageId })).receipts.map(mapReadReceiptFromApi), |
25 | 31 | }); |
26 | 32 |
|
| 33 | + useEffect(() => { |
| 34 | + if (!rid) { |
| 35 | + return; |
| 36 | + } |
| 37 | + return subscribeToNotifyRoom(`${rid}/messagesRead`, () => { |
| 38 | + queryClient.invalidateQueries({ queryKey }); |
| 39 | + }); |
| 40 | + }, [rid, queryClient, queryKey, subscribeToNotifyRoom]); |
| 41 | + |
27 | 42 | useEffect(() => { |
28 | 43 | if (readReceiptsResult.isError) { |
29 | 44 | dispatchToastMessage({ type: 'error', message: readReceiptsResult.error }); |
|
0 commit comments