-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathMessageInput.tsx
More file actions
210 lines (193 loc) · 9.03 KB
/
MessageInput.tsx
File metadata and controls
210 lines (193 loc) · 9.03 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { PropsWithChildren } from 'react';
import { DefaultTriggerProvider } from './DefaultTriggerProvider';
import { MessageInputFlat } from './MessageInputFlat';
import { useCooldownTimer } from './hooks/useCooldownTimer';
import { useCreateMessageInputContext } from './hooks/useCreateMessageInputContext';
import { useMessageInputState } from './hooks/useMessageInputState';
import { StreamMessage, useChannelStateContext } from '../../context/ChannelStateContext';
import {
ComponentContextValue,
useComponentContext,
} from '../../context/ComponentContext';
import { MessageInputContextProvider } from '../../context/MessageInputContext';
import { DialogManagerProvider } from '../../context';
import { useRegisterDropHandlers } from './WithDragAndDropUpload';
import { useStableId } from '../UtilityComponents/useStableId';
import type { Channel, Message, SendFileAPIResponse } from 'stream-chat';
import type { BaseLocalAttachmentMetadata, LocalAttachmentUploadMetadata } from './types';
import type { SearchQueryParams } from '../ChannelSearch/hooks/useChannelSearch';
import type { MessageToSend } from '../../context/ChannelActionContext';
import type {
CustomTrigger,
DefaultStreamChatGenerics,
SendMessageOptions,
UnknownType,
} from '../../types/types';
import type { URLEnrichmentConfig } from './hooks/useLinkPreviews';
import type { CustomAudioRecordingConfig } from '../MediaRecorder';
export type EmojiSearchIndexResult = {
id: string;
name: string;
skins: Array<{ native: string }>;
emoticons?: Array<string>;
native?: string;
};
export interface EmojiSearchIndex<T extends UnknownType = UnknownType> {
search: (
query: string,
) =>
| PromiseLike<Array<EmojiSearchIndexResult & T>>
| Array<EmojiSearchIndexResult & T>
| null;
}
export type MessageInputProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
V extends CustomTrigger = CustomTrigger,
> = {
/** Additional props to be passed to the underlying `AutoCompleteTextarea` component, [available props](https://www.npmjs.com/package/react-textarea-autosize) */
additionalTextareaProps?: React.TextareaHTMLAttributes<HTMLTextAreaElement>;
/**
* When enabled, recorded messages won’t be sent immediately.
* Instead, they will “stack up” with other attachments in the message composer allowing the user to send multiple attachments as part of the same message.
*/
asyncMessagesMultiSendEnabled?: boolean;
/** Allows to configure the audio recording parameters for voice messages. */
audioRecordingConfig?: CustomAudioRecordingConfig;
/** Controls whether the users will be provided with the UI to record voice messages. */
audioRecordingEnabled?: boolean;
/** Function to clear the editing state while editing a message */
clearEditingState?: () => void;
/** If true, disables the text input */
disabled?: boolean;
/** If true, the suggestion list will not display and autocomplete @mentions. Default: false. */
disableMentions?: boolean;
/** Function to override the default file upload request */
doFileUploadRequest?: (
file: LocalAttachmentUploadMetadata['file'],
channel: Channel<StreamChatGenerics>,
) => Promise<SendFileAPIResponse>;
/** Function to override the default image upload request */
doImageUploadRequest?: (
file: LocalAttachmentUploadMetadata['file'],
channel: Channel<StreamChatGenerics>,
) => Promise<SendFileAPIResponse>;
/** Mechanism to be used with autocomplete and text replace features of the `MessageInput` component, see [emoji-mart `SearchIndex`](https://github.com/missive/emoji-mart#%EF%B8%8F%EF%B8%8F-headless-search) */
emojiSearchIndex?: ComponentContextValue['emojiSearchIndex'];
/** Custom error handler function to be called with a file/image upload fails */
errorHandler?: (
error: Error,
type: string,
file: LocalAttachmentUploadMetadata['file'] & BaseLocalAttachmentMetadata,
) => void;
/** If true, focuses the text input on component mount */
focus?: boolean;
/** Generates the default value for the underlying textarea element. The function's return value takes precedence before additionalTextareaProps.defaultValue. */
getDefaultValue?: () => string | string[];
/** If true, expands the text input vertically for new lines */
grow?: boolean;
/** Allows to hide MessageInput's send button. */
hideSendButton?: boolean;
/** Custom UI component handling how the message input is rendered, defaults to and accepts the same props as [MessageInputFlat](https://github.com/GetStream/stream-chat-react/blob/master/src/components/MessageInput/MessageInputFlat.tsx) */
Input?: React.ComponentType<MessageInputProps<StreamChatGenerics, V>>;
/** Signals that the MessageInput is rendered in a message thread (Thread component) */
isThreadInput?: boolean;
/** Max number of rows the underlying `textarea` component is allowed to grow */
maxRows?: number;
/** If true, the suggestion list will search all app users for an @mention, not just current channel members/watchers. Default: false. */
mentionAllAppUsers?: boolean;
/** Object containing filters/sort/options overrides for an @mention user query */
mentionQueryParams?: SearchQueryParams<StreamChatGenerics>['userFilters'];
/** If provided, the existing message will be edited on submit */
message?: StreamMessage<StreamChatGenerics>;
/** Min number of rows the underlying `textarea` will start with. The `grow` on MessageInput prop has to be enabled for `minRows` to take effect. */
minRows?: number;
/** If true, disables file uploads for all attachments except for those with type 'image'. Default: false */
noFiles?: boolean;
/** Function to override the default submit handler */
overrideSubmitHandler?: (
message: MessageToSend<StreamChatGenerics>,
channelCid: string,
customMessageData?: Partial<Message<StreamChatGenerics>>,
options?: SendMessageOptions,
) => Promise<void> | void;
/** When replying in a thread, the parent message object */
parent?: StreamMessage<StreamChatGenerics>;
/** If true, triggers typing events on text input keystroke */
publishTypingEvent?: boolean;
/** If true, will use an optional dependency to support transliteration in the input for mentions, default is false. See: https://github.com/getstream/transliterate */
/**
* Currently, `Enter` is the default submission key and `Shift`+`Enter` is the default combination for the new line.
* If specified, this function overrides the default behavior specified previously.
*
* Example of default behaviour:
* ```tsx
* const defaultShouldSubmit = (event) => event.key === "Enter" && !event.shiftKey;
* ```
*/
shouldSubmit?: (event: KeyboardEvent) => boolean;
/** Configuration parameters for link previews. */
urlEnrichmentConfig?: URLEnrichmentConfig;
useMentionsTransliteration?: boolean;
};
const MessageInputProvider = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
V extends CustomTrigger = CustomTrigger,
>(
props: PropsWithChildren<MessageInputProps<StreamChatGenerics, V>>,
) => {
const cooldownTimerState = useCooldownTimer<StreamChatGenerics>();
const messageInputState = useMessageInputState<StreamChatGenerics, V>(props);
const { emojiSearchIndex } = useComponentContext('MessageInput');
const messageInputContextValue = useCreateMessageInputContext<StreamChatGenerics, V>({
...cooldownTimerState,
...messageInputState,
...props,
emojiSearchIndex: props.emojiSearchIndex ?? emojiSearchIndex,
});
// @ts-expect-error generics to be removed
useRegisterDropHandlers(messageInputContextValue);
return (
<MessageInputContextProvider<StreamChatGenerics, V> value={messageInputContextValue}>
{props.children}
</MessageInputContextProvider>
);
};
const UnMemoizedMessageInput = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
V extends CustomTrigger = CustomTrigger,
>(
props: MessageInputProps<StreamChatGenerics, V>,
) => {
const { Input: PropInput } = props;
const { dragAndDropWindow } = useChannelStateContext<StreamChatGenerics>();
const { Input: ContextInput, TriggerProvider = DefaultTriggerProvider } =
useComponentContext<StreamChatGenerics, V>('MessageInput');
const id = useStableId();
const Input = PropInput || ContextInput || MessageInputFlat;
const dialogManagerId = props.isThreadInput
? `message-input-dialog-manager-thread-${id}`
: `message-input-dialog-manager-${id}`;
if (dragAndDropWindow)
return (
<DialogManagerProvider id={dialogManagerId}>
<TriggerProvider>
<Input />
</TriggerProvider>
</DialogManagerProvider>
);
return (
<DialogManagerProvider id={dialogManagerId}>
<MessageInputProvider {...props}>
<TriggerProvider>
<Input />
</TriggerProvider>
</MessageInputProvider>
</DialogManagerProvider>
);
};
/**
* A high level component that has provides all functionality to the Input it renders.
*/
export const MessageInput = React.memo(
UnMemoizedMessageInput,
) as typeof UnMemoizedMessageInput;