This repository was archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.js
More file actions
145 lines (145 loc) · 4.87 KB
/
Message.js
File metadata and controls
145 lines (145 loc) · 4.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
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
import PropTypes from 'prop-types';
import React from 'react';
import { View, ViewPropTypes, StyleSheet } from 'react-native';
import Avatar from './Avatar';
import Bubble from './Bubble';
import SystemMessage from './SystemMessage';
import Day from './Day';
import { isSameUser } from './utils';
const styles = {
left: StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'flex-end',
justifyContent: 'flex-start',
marginLeft: 8,
marginRight: 0,
},
}),
right: StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'flex-end',
justifyContent: 'flex-end',
marginLeft: 0,
marginRight: 8,
},
}),
};
export default class Message extends React.Component {
shouldComponentUpdate(nextProps) {
const next = nextProps.currentMessage;
const current = this.props.currentMessage;
const { previousMessage, nextMessage } = this.props;
const nextPropsMessage = nextProps.nextMessage;
const nextPropsPreviousMessage = nextProps.previousMessage;
const shouldUpdate = (this.props.shouldUpdateMessage &&
this.props.shouldUpdateMessage(this.props, nextProps)) ||
false;
return (next.sent !== current.sent ||
next.received !== current.received ||
next.pending !== current.pending ||
next.createdAt !== current.createdAt ||
next.text !== current.text ||
next.image !== current.image ||
next.video !== current.video ||
next.audio !== current.audio ||
previousMessage !== nextPropsPreviousMessage ||
nextMessage !== nextPropsMessage ||
shouldUpdate);
}
renderDay() {
if (this.props.currentMessage && this.props.currentMessage.createdAt) {
const { containerStyle, ...props } = this.props;
if (this.props.renderDay) {
return this.props.renderDay(props);
}
return <Day {...props}/>;
}
return null;
}
renderBubble() {
const { containerStyle, ...props } = this.props;
if (this.props.renderBubble) {
return this.props.renderBubble(props);
}
return <Bubble {...props}/>;
}
renderSystemMessage() {
const { containerStyle, ...props } = this.props;
if (this.props.renderSystemMessage) {
return this.props.renderSystemMessage(props);
}
return <SystemMessage {...props}/>;
}
renderAvatar() {
const { user, currentMessage, showUserAvatar } = this.props;
if (user &&
user._id &&
currentMessage &&
currentMessage.user &&
user._id === currentMessage.user._id &&
!showUserAvatar) {
return null;
}
if (currentMessage && currentMessage.user && currentMessage.user.avatar === null) {
return null;
}
const { containerStyle, ...props } = this.props;
return <Avatar {...props}/>;
}
render() {
const { currentMessage, nextMessage, position, containerStyle } = this.props;
if (currentMessage) {
const sameUser = isSameUser(currentMessage, nextMessage);
return (<View>
{this.renderDay()}
{currentMessage.system ? (this.renderSystemMessage()) : (<View style={[
styles[position].container,
{ marginBottom: sameUser ? 2 : 28 },
!this.props.inverted && { marginBottom: 2 },
containerStyle && containerStyle[position],
]}>
{this.props.position === 'left' ? this.renderAvatar() : null}
{this.renderBubble()}
{this.props.position === 'right' ? this.renderAvatar() : null}
</View>)}
</View>);
}
return null;
}
}
Message.defaultProps = {
renderAvatar: undefined,
renderBubble: null,
renderDay: null,
renderSystemMessage: null,
position: 'left',
currentMessage: {},
nextMessage: {},
previousMessage: {},
user: {},
containerStyle: {},
showUserAvatar: false,
inverted: true,
shouldUpdateMessage: undefined,
};
Message.propTypes = {
renderAvatar: PropTypes.func,
showUserAvatar: PropTypes.bool,
renderBubble: PropTypes.func,
renderDay: PropTypes.func,
renderSystemMessage: PropTypes.func,
position: PropTypes.oneOf(['left', 'right']),
currentMessage: PropTypes.object,
nextMessage: PropTypes.object,
previousMessage: PropTypes.object,
user: PropTypes.object,
inverted: PropTypes.bool,
containerStyle: PropTypes.shape({
left: ViewPropTypes.style,
right: ViewPropTypes.style,
}),
shouldUpdateMessage: PropTypes.func,
};
//# sourceMappingURL=Message.js.map