Repository navigation
Expand file tree
/
Copy pathuseDeleteHandler.ts
More file actions
59 lines (51 loc) · 1.87 KB
/
Copy pathuseDeleteHandler.ts
File metadata and controls
59 lines (51 loc) · 1.87 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { validateAndGetMessage } from '../utils';
import { useChannel, useChatContext } from '../../../context';
import { useTranslationContext } from '../../../context/TranslationContext';
import { useMessagePaginator } from '../../../hooks';
import { useThreadContext } from '../../Threads';
import { type DeleteMessageOptions, formatMessage, type LocalMessage } from 'stream-chat';
import type { MessageContextValue } from '../../../context';
export type DeleteMessageNotifications = {
getErrorNotification?: (message: LocalMessage) => string;
notify?: (notificationText: string, type: 'success' | 'error') => void;
};
export const useDeleteHandler = (
message?: LocalMessage,
notifications: DeleteMessageNotifications = {},
): MessageContextValue['handleDelete'] => {
const { getErrorNotification, notify } = notifications;
const channel = useChannel();
const thread = useThreadContext();
const { client } = useChatContext();
const { t } = useTranslationContext();
const messagePaginator = useMessagePaginator();
return async (options?: DeleteMessageOptions) => {
if (!message?.id || !client) {
return;
}
try {
const entity = thread ?? channel;
if (entity.messageOperations) {
await entity.messageOperations.delete({
localMessage: formatMessage(message),
options,
});
} else {
const deletedMessage = (
await client.deleteMessage({ id: message.id, ...options })
).message;
messagePaginator.ingestItem(formatMessage(deletedMessage));
}
} catch (e) {
const errorMessage =
getErrorNotification && validateAndGetMessage(getErrorNotification, [message]);
if (notify)
notify(
errorMessage ||
t('common.errorDeletingMessage.label', 'Error deleting message'),
'error',
);
throw e;
}
};
};