-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSendInput.tsx
More file actions
333 lines (303 loc) · 10.4 KB
/
SendInput.tsx
File metadata and controls
333 lines (303 loc) · 10.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import React, { forwardRef } from 'react';
import {
NativeSyntheticEvent,
Platform,
TextInput as RNTextInput,
TextInputSelectionChangeEventData,
TouchableOpacity,
View,
} from 'react-native';
import { MentionType, MessageMetaArray } from '@sendbird/chat/message';
import {
Icon,
Modal,
TextInput,
createStyleSheet,
useAlert,
useBottomSheet,
useToast,
useUIKitTheme,
} from '@sendbird/uikit-react-native-foundation';
import { Logger, useDeferredModalState, useIIFE } from '@sendbird/uikit-utils';
import { VOICE_MESSAGE_META_ARRAY_DURATION_KEY, VOICE_MESSAGE_META_ARRAY_MESSAGE_TYPE_KEY } from '../../constants';
import { useChannelInputItems } from '../../hooks/useChannelInputItems';
import { useLocalization, usePlatformService, useSendbirdChat } from '../../hooks/useContext';
import SBUUtils from '../../libs/SBUUtils';
import type { FileType } from '../../platform/types';
import type { MentionedUser } from '../../types';
import type { ChannelInputProps } from './index';
interface SendInputProps extends ChannelInputProps {
text: string;
onChangeText: (val: string) => void;
onSelectionChange: (e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => void;
mentionedUsers: MentionedUser[];
}
const SendInput = forwardRef<RNTextInput, SendInputProps>(function SendInput(
{
style,
VoiceMessageInput,
MessageToReplyPreview,
AttachmentsButton,
onPressSendUserMessage,
onPressSendFileMessage,
text,
onChangeText,
onSelectionChange,
mentionedUsers,
inputDisabled,
inputFrozen,
inputMuted,
channel,
messageToReply,
setMessageToReply,
messageForThread,
partialTextInputProps,
placeholder,
},
ref,
) {
const { playerService, recorderService } = usePlatformService();
const { mentionManager, sbOptions } = useSendbirdChat();
const { STRINGS } = useLocalization();
const { openSheet } = useBottomSheet();
const toast = useToast();
const {
onClose,
onDismiss,
visible: voiceMessageInputVisible,
setVisible: setVoiceMessageInputVisible,
} = useDeferredModalState();
const messageReplyParams = useIIFE(() => {
const { groupChannel } = sbOptions.uikit;
if (channel.isGroupChannel()) {
if (groupChannel.channel.replyType === 'quote_reply' && messageToReply) {
return {
parentMessageId: messageToReply.messageId,
isReplyToChannel: true,
};
} else if (groupChannel.channel.replyType === 'thread' && messageForThread) {
return {
parentMessageId: messageForThread.messageId,
isReplyToChannel: true,
};
}
}
return {};
});
const messageMentionParams = useIIFE(() => {
const { groupChannel } = sbOptions.uikit;
if (!channel.isGroupChannel() || !groupChannel.channel.enableMention) return {};
return {
mentionType: MentionType.USERS,
mentionedUserIds: mentionedUsers.map((it) => it.user.userId),
mentionedMessageTemplate: mentionManager.textToMentionedMessageTemplate(
text,
mentionedUsers,
groupChannel.channel.enableMention,
),
};
});
const onFailureToSend = (error: Error) => {
toast.show(STRINGS.TOAST.SEND_MSG_ERROR, 'error');
Logger.error(STRINGS.TOAST.SEND_MSG_ERROR, error);
};
const sendUserMessage = () => {
onPressSendUserMessage({
message: text,
...messageMentionParams,
...messageReplyParams,
}).catch(onFailureToSend);
// On iOS with autoCorrect enabled, calling onChangeText('') immediately after sending
// can be ignored due to the keyboard's autocorrect not being committed yet.
// Delay the clear call slightly to allow the autocorrected text to be applied first.
if (Platform.OS === 'ios') {
const textInputRef = ref as React.MutableRefObject<RNTextInput | undefined>;
if (textInputRef.current) {
setTimeout(() => {
onChangeText('');
}, 10);
}
} else {
onChangeText('');
}
setMessageToReply?.();
};
const sendFileMessage = (file: FileType) => {
onPressSendFileMessage({
file,
...messageReplyParams,
}).catch(onFailureToSend);
setMessageToReply?.();
};
const sendVoiceMessage = (file: FileType, durationMills: number) => {
if (inputMuted) {
toast.show(STRINGS.TOAST.USER_MUTED_ERROR, 'error');
Logger.error(STRINGS.TOAST.USER_MUTED_ERROR);
} else if (inputFrozen) {
toast.show(STRINGS.TOAST.CHANNEL_FROZEN_ERROR, 'error');
Logger.error(STRINGS.TOAST.CHANNEL_FROZEN_ERROR);
} else {
onPressSendFileMessage({
file,
metaArrays: [
new MessageMetaArray({
key: VOICE_MESSAGE_META_ARRAY_DURATION_KEY,
value: [String(durationMills)],
}),
new MessageMetaArray({
key: VOICE_MESSAGE_META_ARRAY_MESSAGE_TYPE_KEY,
value: [`voice/${recorderService.options.extension}`],
}),
],
...messageReplyParams,
}).catch(onFailureToSend);
}
onChangeText('');
setMessageToReply?.();
};
const sheetItems = useChannelInputItems(channel, sendFileMessage);
const getPlaceholder = () => {
if (placeholder != null) return placeholder;
if (inputMuted) return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_MUTED;
if (inputFrozen) return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_DISABLED;
if (inputDisabled) return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_DISABLED;
if (messageToReply) return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_REPLY;
if (messageForThread) {
if (messageForThread.threadInfo && messageForThread.threadInfo.replyCount > 0) {
return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_REPLY_TO_THREAD;
} else {
return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_REPLY_IN_THREAD;
}
}
return STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_ACTIVE;
};
const voiceMessageEnabled = channel.isGroupChannel() && sbOptions.uikit.groupChannel.channel.enableVoiceMessage;
const sendButtonVisible = Boolean(text.trim());
return (
<View>
{MessageToReplyPreview && (
<MessageToReplyPreview messageToReply={messageToReply} setMessageToReply={setMessageToReply} />
)}
<View style={styles.sendInputContainer}>
{AttachmentsButton && <AttachmentsButton onPress={() => openSheet({ sheetItems })} disabled={inputDisabled} />}
<TextInput
{...partialTextInputProps}
ref={ref}
multiline
disableFullscreenUI
onSelectionChange={onSelectionChange}
editable={!inputDisabled}
onChangeText={onChangeText}
style={style}
placeholder={getPlaceholder()}
originalText={text}
supportRTLAlign
>
{mentionManager.textToMentionedComponents(
text,
mentionedUsers,
sbOptions.uikit.groupChannel.channel.enableMention,
)}
</TextInput>
{voiceMessageEnabled && (
<VoiceMessageButton
visible={!sendButtonVisible}
disabled={inputDisabled}
onPress={() => setVoiceMessageInputVisible(true)}
/>
)}
<UserMessageSendButton visible={sendButtonVisible} disabled={inputDisabled} onPress={sendUserMessage} />
{voiceMessageEnabled && VoiceMessageInput && (
<Modal
disableBackgroundClose
onClose={onClose}
onDismiss={() => {
onDismiss();
playerService.reset().catch(() => {});
recorderService.reset().catch(() => {});
}}
backgroundStyle={{ justifyContent: 'flex-end' }}
visible={voiceMessageInputVisible}
type={'slide-no-gesture'}
>
<VoiceMessageInput onClose={onClose} onSend={({ file, duration }) => sendVoiceMessage(file, duration)} />
</Modal>
)}
</View>
</View>
);
});
type InputButtonProps = { visible: boolean; disabled: boolean; onPress: () => void };
const VoiceMessageButton = ({ visible, disabled, onPress }: InputButtonProps) => {
const { STRINGS } = useLocalization();
const { alert } = useAlert();
const { playerService, recorderService } = usePlatformService();
const { colors } = useUIKitTheme();
if (!visible) return null;
const onPressWithPermissionCheck = async () => {
const recorderGranted = await recorderService.requestPermission();
if (!recorderGranted) {
alert({
title: STRINGS.DIALOG.ALERT_PERMISSIONS_TITLE,
message: STRINGS.DIALOG.ALERT_PERMISSIONS_MESSAGE(
STRINGS.LABELS.PERMISSION_MICROPHONE,
STRINGS.LABELS.PERMISSION_APP_NAME,
),
buttons: [{ text: STRINGS.DIALOG.ALERT_PERMISSIONS_OK, onPress: () => SBUUtils.openSettings() }],
});
Logger.error('Failed to request permission for recorder');
return;
}
const playerGranted = await playerService.requestPermission();
if (!playerGranted) {
alert({
title: STRINGS.DIALOG.ALERT_PERMISSIONS_TITLE,
message: STRINGS.DIALOG.ALERT_PERMISSIONS_MESSAGE(
STRINGS.LABELS.PERMISSION_DEVICE_STORAGE,
STRINGS.LABELS.PERMISSION_APP_NAME,
),
buttons: [{ text: STRINGS.DIALOG.ALERT_PERMISSIONS_OK, onPress: () => SBUUtils.openSettings() }],
});
Logger.error('Failed to request permission for player');
return;
}
onPress();
};
return (
<TouchableOpacity onPress={onPressWithPermissionCheck} disabled={disabled}>
<Icon
color={disabled ? colors.ui.input.default.disabled.highlight : colors.ui.input.default.active.highlight}
icon={'audio-on'}
size={24}
containerStyle={styles.sendIcon}
/>
</TouchableOpacity>
);
};
const UserMessageSendButton = ({ visible, disabled, onPress }: InputButtonProps) => {
const { colors } = useUIKitTheme();
if (!visible) return null;
return (
<TouchableOpacity onPress={onPress} disabled={disabled}>
<Icon
color={disabled ? colors.ui.input.default.disabled.highlight : colors.ui.input.default.active.highlight}
icon={'send'}
size={24}
containerStyle={styles.sendIcon}
/>
</TouchableOpacity>
);
};
const styles = createStyleSheet({
sendInputContainer: {
paddingVertical: 10,
paddingHorizontal: 12,
alignItems: 'center',
flexDirection: 'row',
},
sendIcon: {
marginStart: 4,
padding: 4,
},
});
export default SendInput;