-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMessageComponent.tsx
734 lines (708 loc) · 21.9 KB
/
MessageComponent.tsx
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
import {
eventEmitter,
EventType,
Messages,
IPeer,
ICallEvent,
IMessage,
IRoomEvent,
messagesService,
ITyping,
ChatActions,
sharedFilesService,
IUrgencyType,
CustomMessageText,
MessageTextProps,
CustomMessageTime,
MessageTimeProps,
CustomBubbleContainer,
CustomBubbleProps,
} from 'react-native-rainbow-module';
import React, {useEffect, useState} from 'react';
import {
ActivityIndicator,
Alert,
Clipboard,
ImageBackground,
StyleSheet,
TextStyle,
View,
Text
} from 'react-native';
import {
systemMessageView,
sendingExtraView,
IStyleSendingExtraView,
contactsIsTypingView,
sendingMsgTypesIcon,
attachIcon,
selectedAttachedFilesView,
messageHeaderView,
repliedMessageView,
deletedMessageView,
IStyleHeaderView,
} from './CustomizableMsgUI';
import DocumentPicker from 'react-native-document-picker';
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
import {Strings} from './../resources/localization/Strings';
import Icon from 'react-native-vector-icons/Ionicons';
import {CombinedRootStackParamList} from '../Navigation/AppNavigationTypes';
import {NavigationProp} from '@react-navigation/native';
interface IMessageComponentProps {
peer: IPeer;
navigation: NavigationProp<CombinedRootStackParamList>;
}
// you can add what ever option as you want
enum IMessageOption {
Copy = 'Copy Text',
Forward = 'Forward',
Reply = 'Reply',
Delete = 'Delete',
Cancel = 'Cancel',
Download = 'Download file',
}
export enum Attached {
Capture = 'Capture',
ImageLibrary = 'ImageLibrary',
FilesLibrary = 'FilesLibrary',
}
export interface IAttachedFile {
uri: string;
type: Attached;
}
export const MessageComponent: React.FunctionComponent<
IMessageComponentProps
> = (props: IMessageComponentProps) => {
const [messages, setMessages] = useState<IMessage[]>([]);
const [onTypingList, setOnTypingList] = useState<ITyping[]>([]);
const [filesToUpload, setFilesToUpload] = useState<IAttachedFile[]>([]);
const [downloadedFileIds, setDownloadedFileIds] = useState<string[]>([]);
const [selectedMessage, setSelectedMessage] = useState<any>();
const [showAttachedLoader, setShowAttachedLoader] = useState<boolean>(false);
useEffect(() => {
const onMessagesUpdatedEvent = eventEmitter.addListener(
EventType.MessagesUpdated,
(eventData: IMessage[]) => {
setMessages(eventData);
// mark as read for any new incoming msg
messagesService.messagesMarkedASReadInActiveConversation(
props.peer.jId,
);
},
);
const onIsTyping = eventEmitter.addListener(
EventType.IsTyping,
(eventData: ITyping[]) => {
setOnTypingList(eventData);
},
);
const uploadFilesResult = eventEmitter.addListener(
EventType.FileAttachFinished,
(eventData: {isSuccess: boolean; uri: string; errorMsg?: string}) => {
const filteredFilesToUpload = filesToUpload.filter(
(item: IAttachedFile) => item.uri !== eventData.uri,
);
setFilesToUpload(filteredFilesToUpload);
setShowAttachedLoader(false);
if (eventData.isSuccess === false) {
Alert.alert(Strings.failedToDownload, eventData.errorMsg);
}
},
);
const downloadResult = eventEmitter.addListener(
EventType.FileDownloadFinished,
(eventData: {isSuccess: boolean; fileId: string; errorMsg?: string}) => {
const filteredIds = downloadedFileIds.filter(
(id: string) => id !== eventData.fileId,
);
setDownloadedFileIds(filteredIds);
if (eventData.isSuccess === false) {
Alert.alert(Strings.failedToDownload, eventData.errorMsg);
}
},
);
return () => {
onMessagesUpdatedEvent.remove();
onIsTyping.remove();
uploadFilesResult.remove();
downloadResult.remove();
};
}, [downloadedFileIds, filesToUpload, props.peer.jId]);
const resetToDefault = () => {
setSelectedMessage(undefined);
};
const onLongPress = (context: any, message: IMessage) => {
const options: IMessageOption[] = [
IMessageOption.Reply,
IMessageOption.Forward,
IMessageOption.Copy,
];
if (message.fileDescriptorId) {
// Incase the message obj have a `fileDescriptorId` this means this msg have file or image!
options.push(IMessageOption.Download);
}
if (message.isSent) {
// Show delete option only for the sent msg
options.push(IMessageOption.Delete);
}
// Make the cancel option the last one
options.push(IMessageOption.Cancel);
const cancelButtonIndex = options.length - 1;
context.actionSheet().showActionSheetWithOptions(
{
options,
cancelButtonIndex,
},
(buttonIndex: number) => {
switch (options[buttonIndex]) {
case IMessageOption.Copy:
Clipboard.setString(message.text);
break;
case IMessageOption.Download:
// set the downloaded file Ids so you can show the loader on the message
downloadedFileIds.push(message.fileDescriptorId);
setDownloadedFileIds([...downloadedFileIds]);
sharedFilesService.downloadFile(message.fileDescriptorId);
break;
case IMessageOption.Forward:
props.navigation.navigate('ForwardedView', {message: message});
break;
case IMessageOption.Reply:
// set message type to be replied
message.isReplied = true;
setSelectedMessage(message);
break;
case IMessageOption.Delete:
messagesService.deleteMessage(message._id);
break;
default:
break;
}
},
);
};
const renderSystemMessage = (currentMessage: IMessage) => {
const conference = currentMessage.conference;
const call: ICallEvent = currentMessage.call;
const roomEvent: IRoomEvent = currentMessage.roomEvent;
if (conference) {
return systemMessageView(conference.msgContent);
} else if (call) {
return systemMessageView(call.msgContent);
} else if (roomEvent) {
return systemMessageView(roomEvent.msgContent);
} else {
return;
}
};
const renderMessageFileImage = (currentMessage: IMessage) => {
const image = currentMessage.image;
const downloadedId = downloadedFileIds.find(
id => id === currentMessage.fileDescriptorId,
);
const showLoadingIndicator = downloadedId !== undefined;
return (
<View>
{currentMessage.image === 'icon' ? (
<Icon name="document" style={defaultStyle.msgDocument} />
) : (
<ImageBackground
style={defaultStyle.ImageBackground}
resizeMode={'contain'}
source={{uri: image}}
/>
)}
{showLoadingIndicator && (
<ActivityIndicator
size="large"
color="#808080"
style={defaultStyle.downloadingLoader}
/>
)}
</View>
);
};
const renderActions = () => {
return (
<React.Fragment>
<ChatActions
containerStyle={defaultStyle.msgTypeActionContainerStyle}
icon={sendingMsgTypesIcon}
options={{
'Important msg': () => {
const msg = {
urgency: IUrgencyType.MEDIUM,
};
setSelectedMessage(msg);
},
'Information msg': () => {
const msg = {
urgency: IUrgencyType.LOW,
};
setSelectedMessage(msg);
},
Cancel: () => {
console.log('Cancel');
},
}}
/>
<ChatActions
containerStyle={defaultStyle.attachActionContainerStyle}
icon={attachIcon}
options={{
'Attach Image': () => {
openAttached(Attached.ImageLibrary);
},
'Attach Files': () => {
openAttached(Attached.FilesLibrary);
},
'Open Camera': () => {
openAttached(Attached.Capture);
},
Cancel: () => {
console.log('Cancel');
},
}}
/>
</React.Fragment>
);
};
const openAttached = async (type: Attached) => {
switch (type) {
case Attached.FilesLibrary:
try {
const results = await DocumentPicker.pickMultiple({
type: [DocumentPicker.types.allFiles],
});
for (const res of results) {
if (res.uri) {
const fileType =
res.type === DocumentPicker.types.images
? Attached.ImageLibrary
: Attached.FilesLibrary;
const fileObj: IAttachedFile = {uri: res.uri, type: fileType};
filesToUpload.push(fileObj);
setFilesToUpload([...filesToUpload]);
}
}
} catch (err) {
if (DocumentPicker.isCancel(err)) {
// User cancelled the picker, exit any dialogs or menus and move on
} else {
console.error(`openAttached ERROR: ${err}`);
}
}
break;
case Attached.Capture:
launchCamera(
{
saveToPhotos: true,
mediaType: 'photo',
includeBase64: false,
},
response => {
if (response.assets) {
response.assets.map(uri => {
if (uri.uri) {
const fileObj: IAttachedFile = {
uri: uri.uri,
type: Attached.ImageLibrary,
};
filesToUpload.push(fileObj);
setFilesToUpload([...filesToUpload]);
}
});
}
},
);
break;
case Attached.ImageLibrary:
launchImageLibrary(
{
quality: 1.0,
selectionLimit: 0,
mediaType: 'mixed',
includeBase64: false,
},
response => {
if (response.assets) {
response.assets.map(uri => {
if (uri.uri) {
const fileObj: IAttachedFile = {
uri: uri.uri,
type: Attached.ImageLibrary,
};
filesToUpload.push(fileObj);
setFilesToUpload([...filesToUpload]);
}
});
}
},
);
break;
}
};
const cancelUpload = (file: IAttachedFile) => () => {
const remainingFiles = filesToUpload.filter(
(item: IAttachedFile) => item.uri !== file.uri,
);
setFilesToUpload([...remainingFiles]);
};
const renderIsTypingArea = () => {
return onTypingList.length > 0 ? contactsIsTypingView(onTypingList) : null;
};
const renderAttachmentArea = () => {
return filesToUpload.length > 0
? selectedAttachedFilesView(
filesToUpload,
showAttachedLoader,
cancelUpload,
)
: null;
};
const renderRepliedArea = () => {
return selectedMessage?.isReplied
? sendingExtraView(
selectedMessage.user.name,
resetToDefault,
repliedViewStyle,
selectedMessage.text,
)
: null;
};
const renderUrgencyArea = () => {
switch (selectedMessage?.urgency) {
case IUrgencyType.MEDIUM:
return sendingExtraView(
'Send Important...',
resetToDefault,
importantMsgViewStyle,
);
case IUrgencyType.LOW:
return sendingExtraView(
'Send Information...',
resetToDefault,
infoMsgViewStyle,
);
case IUrgencyType.HIGH:
return sendingExtraView(
'Send Emergency...',
resetToDefault,
importantMsgViewStyle,
);
default:
return;
}
};
const renderSendingExtraView = () => {
return (
<>
{renderUrgencyArea()}
{renderRepliedArea()}
{renderIsTypingArea()}
{renderAttachmentArea()}
</>
);
};
const renderMessageCustomView = (currentMessage: IMessage) => {
const filterMessage: IMessage[] = messages.filter(
data => data._id === currentMessage.associatedMsgId,
);
const warningIcon = <Icon name="warning" color="#c85c2c" size={25} />;
const infoIcon = (
<Icon
name="information-circle-outline"
size={25}
style={infoHeaderViewStyle.iconStyle}
/>
);
const arrowForwardIcon = (
<Icon name="arrow-forward-circle-outline" size={25} />
);
return (
<>
{currentMessage.urgency === IUrgencyType.MEDIUM
? messageHeaderView(
warningIcon,
importantHeaderViewStyle,
Strings.Important,
)
: null}
{currentMessage.urgency === IUrgencyType.LOW
? messageHeaderView(
infoIcon,
infoHeaderViewStyle,
Strings.Information,
)
: null}
{currentMessage.isDeleted ? deletedMessageView() : null}
{currentMessage.isForwarded
? messageHeaderView(
arrowForwardIcon,
forwardHeaderViewStyle,
Strings.Forwarded,
)
: null}
{currentMessage.isReplied && filterMessage.length > 0
? repliedMessageView(
filterMessage[0].text,
filterMessage[0].user.name,
null,
)
: null}
</>
);
};
const onSend = (message: IMessage) => {
if (selectedMessage && selectedMessage.isReplied) {
message.associatedMsgId = selectedMessage?._id;
messagesService.replyMessage(message);
}
// Do not send any, incase there is NO attach or text to send
else if (filesToUpload.length > 0 || message.text.length > 0) {
if (filesToUpload.length > 0) {
setShowAttachedLoader(true);
}
message.urgency =
(selectedMessage && selectedMessage.urgency) ?? IUrgencyType.NONE;
const fileUriArray = filesToUpload.map((item: IAttachedFile) => item.uri);
messagesService.sendMessage(message, message.urgency, fileUriArray);
}
resetToDefault();
};
const renderCustomMessageText = (Props: MessageTextProps<IMessage>) => {
/**
* This function is used to customize the font style for standard message:
* You can uncommitted the line 425 to customize the default font style.
*/
const textStyle: TextStyle = {
fontSize: 18,
fontWeight: 'bold',
fontFamily: 'AvenirNext-Regular',
color: 'lightgray',
};
const linkStyle = {fontSize: 18, color: 'green'};
const customTextStyle = {color: 'red'}; // text style for both messages(left & right)
const containerStyle = {backgroundColor: 'pink'};
return (
<CustomMessageText
textStyle={{left: textStyle, right: textStyle}}
linkStyle={{left: linkStyle, right: linkStyle}}
customTextStyle={customTextStyle}
{...Props}
/>
);
};
const dateCustomStyle: TextStyle = {
color: 'red',
fontWeight: 'bold',
fontStyle: 'italic',
backgroundColor: '#B2D7DA',
};
const renderCustomMessageTime = (Props: MessageTimeProps<IMessage>) => {
const timeTextStyle: TextStyle = {
fontSize: 18,
color: 'yellow',
fontWeight: 'bold',
};
return (
<CustomMessageTime
timeTextStyle={{left: timeTextStyle, right: timeTextStyle}}
{...Props}
/>
);
};
const renderUsername = (props: CustomBubbleProps<IMessage>) => {
const {previousMessage, currentMessage} = props;
if (previousMessage?.user && !isCurrentUserMessage(props)) {
const isSameUserInPrevMessage =
previousMessage.user._id === currentMessage?.user._id;
return !isSameUserInPrevMessage && currentMessage?.user.name;
}
return null;
};
const isCurrentUserMessage = (props: CustomBubbleProps<IMessage>) => {
const {currentMessage, user} = props;
return currentMessage?.user._id === user?._id;
};
const renderBubbleContainer = (Props: CustomBubbleProps<IMessage>) => {
/**
* wrapperStyle: style of the whole message container(including the bottom time container)
* containerStyle: message container style, top left and right(without bottom time container)
* containerToNextStyle: next message container style
* containerToPreviousStyle: previous message container style
* bottomContainerStyle: message time container in the bottom
*
* *Note*: You have to set the borderRadius for each corner separately (e.g. borderBottomRightRadius) ,
* setting borderRadius won't work because the borderBottomRightRadius, borderTopRightRadius etc. in the
* react-native-gifted-chat code overrides it, since the more exact style (separate for each corner) always
* overrides the more general style
*/
return (
<View style={{ flexDirection: 'column' }} >
<Text style={{fontSize: 12, marginLeft: 10, fontWeight: 'bold'}}>
{renderUsername(Props)}{' '}
</Text>
<CustomBubbleContainer
{...Props}
wrapperStyle={{
left: {backgroundColor: '#497EF9'},
right: {backgroundColor: '#B09CF9'},
}}
containerStyle={{
left: {borderRadius: 20},
right: {
borderTopRightRadius: 20,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
}}
containerToNextStyle={{
left: {
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
right: {
borderTopRightRadius: 20,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
}}
containerToPreviousStyle={{
left: {
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
right: {
borderTopRightRadius: 20,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
}}
bottomContainerStyle={{
left: {
backgroundColor: '#A569BD',
borderTopRightRadius: 40,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
right: {
backgroundColor: 'green',
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
}}
/>
</View>
);
};
return (
<Messages
conversation={props.peer}
messages={messages}
onSend={onSend}
onMessageLongPressed={onLongPress}
renderSystemMessage={renderSystemMessage}
renderSendingExtraView={renderSendingExtraView}
renderChatActions={renderActions}
renderMessageCustomView={renderMessageCustomView}
renderMessageImage={renderMessageFileImage}
// dateCustomStyle={dateCustomStyle} // to custom the message time text style
// renderCustomTime={renderCustomMessageTime} // to custom the system message date
// renderMessageText={renderCustomMessageText} // custom the text style and text background inside the message container
// renderBubbleContainer={renderBubbleContainer}
// renderSendButton={()=><Icon name='send' />}
/>
);
};
const repliedViewStyle: IStyleSendingExtraView = StyleSheet.create({
containerStyle: {padding: 10, backgroundColor: '#fdeed9', borderRadius: 10},
textStyle: {color: '#e3902b', fontWeight: 'bold', fontSize: 15},
msgStyle: {fontSize: 15},
});
const infoMsgViewStyle: IStyleSendingExtraView = StyleSheet.create({
containerStyle: {backgroundColor: '#cae1fb', minHeight: 40, padding: 10},
textStyle: {color: '#3f8df5'},
});
const importantMsgViewStyle: IStyleSendingExtraView = StyleSheet.create({
containerStyle: {backgroundColor: '#f9d38b', minHeight: 40, padding: 10},
textStyle: {color: '#c85c2c'},
});
const importantHeaderViewStyle: IStyleHeaderView = StyleSheet.create({
containerStyle: {
backgroundColor: '#f9d38b',
borderRadius: 10,
width: '100%',
alignContent: 'center',
flexDirection: 'row',
padding: 5,
},
iconStyle: {color: '#c85c2c'},
textStyle: {
color: '#c85c2c',
fontSize: 15,
paddingTop: 10,
paddingBottom: 10,
},
});
const infoHeaderViewStyle: IStyleHeaderView = StyleSheet.create({
containerStyle: {
backgroundColor: '#cae1fb',
borderRadius: 10,
width: '100%',
alignContent: 'center',
flexDirection: 'row',
padding: 5,
},
iconStyle: {color: '#3f8df5'},
textStyle: {
color: '#3f8df5',
fontSize: 15,
paddingTop: 10,
paddingBottom: 10,
},
});
const forwardHeaderViewStyle: IStyleHeaderView = StyleSheet.create({
containerStyle: {
backgroundColor: 'gray',
borderRadius: 10,
width: '100%',
alignContent: 'center',
flexDirection: 'row',
padding: 5,
},
iconStyle: {color: 'white'},
textStyle: {color: 'white', fontSize: 15, paddingTop: 10, paddingBottom: 10},
});
const defaultStyle = StyleSheet.create({
msgTypeActionContainerStyle: {
width: 44,
height: 44,
alignItems: 'center',
justifyContent: 'center',
marginBottom: 0,
marginLeft: 4,
marginRight: 4,
},
attachActionContainerStyle: {
width: 44,
height: 44,
alignItems: 'center',
justifyContent: 'center',
marginBottom: 0,
marginLeft: -15,
},
filesToUploadContainer: {
backgroundColor: '#DCDCDC',
minHeight: 40,
padding: 10,
flexDirection: 'row',
},
msgDocument: {margin: 20, fontSize: 50, color: 'red'},
ImageBackground: {width: '100%', aspectRatio: 1, height: undefined},
downloadingLoader: {position: 'absolute', top: '50%', left: '50%'},
});