-
Notifications
You must be signed in to change notification settings - Fork 10k
/
useGetMessageByID.ts
30 lines (26 loc) · 1.02 KB
/
useGetMessageByID.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { IMessage } from '@rocket.chat/core-typings';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useCallback } from 'react';
import { Messages } from '../../../../../../app/models/client';
import { onClientMessageReceived } from '../../../../../lib/onClientMessageReceived';
import { mapMessageFromApi } from '../../../../../lib/utils/mapMessageFromApi';
export const useGetMessageByID = () => {
const getMessage = useEndpoint('GET', '/v1/chat.getMessage');
return useCallback(
async (mid: IMessage['_id']) => {
try {
const { message: rawMessage } = await getMessage({ msgId: mid });
const mappedMessage = mapMessageFromApi(rawMessage);
const message = (await onClientMessageReceived(mappedMessage)) || mappedMessage;
Messages.upsert({ _id: message._id }, { $set: message });
return message;
} catch (error) {
if (typeof error === 'object' && error !== null && 'success' in error) {
throw new Error('Message not found');
}
throw error;
}
},
[getMessage],
);
};