-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathuseActionHandler.ts
More file actions
51 lines (40 loc) · 1.69 KB
/
useActionHandler.ts
File metadata and controls
51 lines (40 loc) · 1.69 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
import { useChannelActionContext } from '../../../context/ChannelActionContext';
import { useChannelStateContext } from '../../../context/ChannelStateContext';
import type React from 'react';
import type { LocalMessage } from 'stream-chat';
import { useStableCallback } from '../../../utils/useStableCallback';
export type FormData = Record<string, string>;
export type ActionHandlerReturnType = (
dataOrName?: string | FormData,
value?: string,
event?: React.BaseSyntheticEvent,
) => Promise<void> | void;
export const handleActionWarning = `Action handler was called, but it is missing one of its required arguments.
Make sure the ChannelAction and ChannelState contexts are properly set and the hook is initialized with a valid message.`;
export function useActionHandler(message?: LocalMessage): ActionHandlerReturnType {
const { removeMessage, updateMessage } = useChannelActionContext('useActionHandler');
const { channel } = useChannelStateContext('useActionHandler');
return useStableCallback(async (dataOrName, value, event) => {
if (event) event.preventDefault();
if (!message || !updateMessage || !removeMessage || !channel) {
console.warn(handleActionWarning);
return;
}
const messageId = message.id;
let formData: FormData = {};
// deprecated: value&name should be removed in favor of data obj
if (typeof dataOrName === 'string') {
formData[dataOrName] = value as string;
} else {
formData = { ...dataOrName };
}
if (messageId) {
const data = await channel.sendAction(messageId, formData);
if (data?.message) {
updateMessage(data.message);
} else {
removeMessage(message);
}
}
});
}