2017-11-21 14:55:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-02-08 14:08:50 +00:00
|
|
|
import { View, TouchableHighlight, Text, TouchableOpacity, Vibration } from 'react-native';
|
2017-11-21 14:55:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-12-13 15:00:26 +00:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialIcons';
|
2017-12-11 20:37:33 +00:00
|
|
|
import moment from 'moment';
|
2018-01-30 19:48:26 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-02-08 14:08:50 +00:00
|
|
|
import { KeyboardUtils } from 'react-native-keyboard-input';
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
import { actionsShow, errorActionsShow, toggleReactionPicker } from '../../actions/messages';
|
2017-12-02 13:19:58 +00:00
|
|
|
import Image from './Image';
|
2017-11-21 14:55:50 +00:00
|
|
|
import User from './User';
|
|
|
|
import Avatar from '../Avatar';
|
2017-12-02 13:19:58 +00:00
|
|
|
import Audio from './Audio';
|
|
|
|
import Video from './Video';
|
|
|
|
import Markdown from './Markdown';
|
|
|
|
import Url from './Url';
|
|
|
|
import Reply from './Reply';
|
2018-01-30 19:48:26 +00:00
|
|
|
import ReactionsModal from './ReactionsModal';
|
|
|
|
import Emoji from './Emoji';
|
2017-12-13 15:00:26 +00:00
|
|
|
import messageStatus from '../../constants/messagesStatus';
|
2018-01-09 17:12:55 +00:00
|
|
|
import styles from './styles';
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
@connect(state => ({
|
|
|
|
message: state.messages.message,
|
2018-01-16 18:48:05 +00:00
|
|
|
editing: state.messages.editing,
|
|
|
|
customEmojis: state.customEmojis
|
2017-11-21 14:55:50 +00:00
|
|
|
}), dispatch => ({
|
2017-12-13 15:00:26 +00:00
|
|
|
actionsShow: actionMessage => dispatch(actionsShow(actionMessage)),
|
2018-01-30 19:48:26 +00:00
|
|
|
errorActionsShow: actionMessage => dispatch(errorActionsShow(actionMessage)),
|
|
|
|
toggleReactionPicker: message => dispatch(toggleReactionPicker(message))
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
|
|
|
export default class Message extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
item: PropTypes.object.isRequired,
|
2018-01-30 19:48:26 +00:00
|
|
|
reactions: PropTypes.object.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
|
|
|
Message_TimeFormat: PropTypes.string.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
message: PropTypes.object.isRequired,
|
2017-12-02 13:19:58 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
editing: PropTypes.bool,
|
2017-12-13 15:00:26 +00:00
|
|
|
actionsShow: PropTypes.func,
|
2018-01-09 17:12:55 +00:00
|
|
|
errorActionsShow: PropTypes.func,
|
2018-01-30 19:48:26 +00:00
|
|
|
customEmojis: PropTypes.object,
|
|
|
|
toggleReactionPicker: PropTypes.func,
|
|
|
|
onReactionPress: PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { reactionsModal: false };
|
|
|
|
this.onClose = this.onClose.bind(this);
|
2018-01-09 17:12:55 +00:00
|
|
|
}
|
|
|
|
componentWillReceiveProps() {
|
|
|
|
this.extraStyle = this.extraStyle || {};
|
|
|
|
if (this.props.item.status === messageStatus.TEMP || this.props.item.status === messageStatus.ERROR) {
|
|
|
|
this.extraStyle.opacity = 0.3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
if (!equal(this.props.reactions, nextProps.reactions)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.state.reactionsModal !== nextState.reactionsModal) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-09 17:12:55 +00:00
|
|
|
return this.props.item._updatedAt.toGMTString() !== nextProps.item._updatedAt.toGMTString() || this.props.item.status !== nextProps.item.status;
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 15:07:09 +00:00
|
|
|
onPress = () => {
|
2018-02-08 14:08:50 +00:00
|
|
|
KeyboardUtils.dismiss();
|
2018-01-30 15:07:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
onLongPress() {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.props.actionsShow(this.parseMessage());
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 15:00:26 +00:00
|
|
|
onErrorPress() {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.props.errorActionsShow(this.parseMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
onReactionPress(emoji) {
|
|
|
|
this.props.onReactionPress(emoji, this.props.item._id);
|
|
|
|
}
|
|
|
|
onClose() {
|
|
|
|
this.setState({ reactionsModal: false });
|
|
|
|
}
|
|
|
|
onReactionLongPress() {
|
|
|
|
this.setState({ reactionsModal: true });
|
|
|
|
Vibration.vibrate(50);
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 15:43:17 +00:00
|
|
|
getInfoMessage() {
|
|
|
|
let message = '';
|
|
|
|
const messageType = this.props.item.t;
|
|
|
|
|
|
|
|
if (messageType === 'rm') {
|
|
|
|
message = 'Message removed';
|
|
|
|
} else if (messageType === 'uj') {
|
|
|
|
message = 'Has joined the channel.';
|
|
|
|
} else if (messageType === 'r') {
|
|
|
|
message = `Room name changed to: ${ this.props.item.msg } by ${ this.props.item.u.username }`;
|
|
|
|
} else if (messageType === 'message_pinned') {
|
|
|
|
message = 'Message pinned';
|
|
|
|
} else if (messageType === 'ul') {
|
|
|
|
message = 'Has left the channel.';
|
|
|
|
} else if (messageType === 'ru') {
|
|
|
|
message = `User ${ this.props.item.msg } removed by ${ this.props.item.u.username }`;
|
|
|
|
} else if (messageType === 'au') {
|
|
|
|
message = `User ${ this.props.item.msg } added by ${ this.props.item.u.username }`;
|
|
|
|
} else if (messageType === 'user-muted') {
|
|
|
|
message = `User ${ this.props.item.msg } muted by ${ this.props.item.u.username }`;
|
|
|
|
} else if (messageType === 'user-unmuted') {
|
|
|
|
message = `User ${ this.props.item.msg } unmuted by ${ this.props.item.u.username }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
parseMessage = () => JSON.parse(JSON.stringify(this.props.item));
|
|
|
|
|
2017-12-19 15:43:17 +00:00
|
|
|
isInfoMessage() {
|
|
|
|
return ['r', 'au', 'ru', 'ul', 'uj', 'rm', 'user-muted', 'user-unmuted', 'message_pinned'].includes(this.props.item.t);
|
|
|
|
}
|
|
|
|
|
|
|
|
isDeleted() {
|
|
|
|
return this.props.item.t === 'rm';
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 15:00:26 +00:00
|
|
|
hasError() {
|
|
|
|
return this.props.item.status === messageStatus.ERROR;
|
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
attachments() {
|
2017-12-02 13:19:58 +00:00
|
|
|
if (this.props.item.attachments.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = this.props.item.attachments[0];
|
|
|
|
const { baseUrl, user } = this.props;
|
|
|
|
if (file.image_type) {
|
|
|
|
return <Image file={file} baseUrl={baseUrl} user={user} />;
|
|
|
|
} else if (file.audio_type) {
|
|
|
|
return <Audio file={file} baseUrl={baseUrl} user={user} />;
|
|
|
|
} else if (file.video_type) {
|
|
|
|
return <Video file={file} baseUrl={baseUrl} user={user} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Reply attachment={file} timeFormat={this.props.Message_TimeFormat} />;
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderMessageContent() {
|
2017-12-19 15:43:17 +00:00
|
|
|
if (this.isInfoMessage()) {
|
|
|
|
return <Text style={styles.textInfo}>{this.getInfoMessage()}</Text>;
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
2018-01-16 18:48:05 +00:00
|
|
|
const { item, customEmojis, baseUrl } = this.props;
|
|
|
|
return <Markdown msg={item.msg} customEmojis={customEmojis} baseUrl={baseUrl} />;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
renderUrl() {
|
|
|
|
if (this.props.item.urls.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.item.urls.map(url => (
|
|
|
|
<Url url={url} key={url._id} />
|
|
|
|
));
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 15:00:26 +00:00
|
|
|
renderError = () => {
|
|
|
|
if (!this.hasError()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TouchableOpacity onPress={() => this.onErrorPress()}>
|
|
|
|
<Icon name='error-outline' color='red' size={20} style={{ padding: 10, paddingRight: 12, paddingLeft: 0 }} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
renderReaction(reaction) {
|
|
|
|
const reacted = reaction.usernames.findIndex(item => item.value === this.props.user.username) !== -1;
|
|
|
|
const reactedContainerStyle = reacted ? { borderColor: '#bde1fe', backgroundColor: '#f3f9ff' } : {};
|
|
|
|
const reactedCount = reacted ? { color: '#4fb0fc' } : {};
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => this.onReactionPress(reaction.emoji)}
|
|
|
|
onLongPress={() => this.onReactionLongPress()}
|
|
|
|
key={reaction.emoji}
|
|
|
|
>
|
|
|
|
<View style={[styles.reactionContainer, reactedContainerStyle]}>
|
|
|
|
<Emoji
|
|
|
|
content={reaction.emoji}
|
2018-02-08 14:08:50 +00:00
|
|
|
standardEmojiStyle={styles.reactionEmoji}
|
|
|
|
customEmojiStyle={styles.reactionCustomEmoji}
|
2018-01-30 19:48:26 +00:00
|
|
|
customEmojis={this.props.customEmojis}
|
|
|
|
/>
|
|
|
|
<Text style={[styles.reactionCount, reactedCount]}>{ reaction.usernames.length }</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderReactions() {
|
|
|
|
if (this.props.item.reactions.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.reactionsContainer}>
|
|
|
|
{this.props.item.reactions.map(reaction => this.renderReaction(reaction))}
|
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => this.props.toggleReactionPicker(this.parseMessage())}
|
|
|
|
key='add-reaction'
|
|
|
|
style={styles.reactionContainer}
|
|
|
|
>
|
|
|
|
<Icon name='insert-emoticon' color='#aaaaaa' size={15} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
render() {
|
2017-11-24 20:44:52 +00:00
|
|
|
const {
|
2018-01-30 19:48:26 +00:00
|
|
|
item, message, editing, baseUrl, customEmojis
|
2017-11-24 20:44:52 +00:00
|
|
|
} = this.props;
|
2017-11-21 14:55:50 +00:00
|
|
|
const username = item.alias || item.u.username;
|
2017-11-24 20:44:52 +00:00
|
|
|
const isEditing = message._id === item._id && editing;
|
2018-01-30 19:48:26 +00:00
|
|
|
const accessibilityLabel = `Message from ${ username } at ${ moment(item.ts).format(this.props.Message_TimeFormat) }, ${ this.props.item.msg }`;
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
return (
|
2017-12-13 15:00:26 +00:00
|
|
|
<TouchableHighlight
|
2018-01-30 15:07:09 +00:00
|
|
|
onPress={() => this.onPress()}
|
2017-11-24 20:44:52 +00:00
|
|
|
onLongPress={() => this.onLongPress()}
|
2017-12-13 15:00:26 +00:00
|
|
|
disabled={this.isDeleted() || this.hasError()}
|
|
|
|
underlayColor='#FFFFFF'
|
|
|
|
activeOpacity={0.3}
|
|
|
|
style={[styles.message, isEditing ? styles.editing : null]}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel={accessibilityLabel}
|
2017-11-21 14:55:50 +00:00
|
|
|
>
|
2018-02-08 14:08:50 +00:00
|
|
|
<View style={styles.flex}>
|
2017-12-13 15:00:26 +00:00
|
|
|
{this.renderError()}
|
2018-02-08 14:08:50 +00:00
|
|
|
<View style={[this.extraStyle, styles.flex]}>
|
2017-12-13 15:00:26 +00:00
|
|
|
<Avatar
|
2018-02-08 14:08:50 +00:00
|
|
|
style={styles.avatar}
|
2017-12-13 15:00:26 +00:00
|
|
|
text={item.avatar ? '' : username}
|
|
|
|
size={40}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
avatar={item.avatar}
|
|
|
|
/>
|
|
|
|
<View style={[styles.content]}>
|
|
|
|
<User
|
|
|
|
onPress={this._onPress}
|
|
|
|
item={item}
|
|
|
|
Message_TimeFormat={this.props.Message_TimeFormat}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
/>
|
|
|
|
{this.renderMessageContent()}
|
|
|
|
{this.attachments()}
|
|
|
|
{this.renderUrl()}
|
2018-01-30 19:48:26 +00:00
|
|
|
{this.renderReactions()}
|
2017-12-13 15:00:26 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
2018-01-30 19:48:26 +00:00
|
|
|
{this.state.reactionsModal ?
|
|
|
|
<ReactionsModal
|
|
|
|
isVisible={this.state.reactionsModal}
|
|
|
|
onClose={this.onClose}
|
|
|
|
reactions={item.reactions}
|
|
|
|
user={this.props.user}
|
|
|
|
customEmojis={customEmojis}
|
|
|
|
/>
|
|
|
|
: null
|
|
|
|
}
|
2018-02-08 14:08:50 +00:00
|
|
|
</View>
|
2017-12-13 15:00:26 +00:00
|
|
|
</TouchableHighlight>
|
2017-11-21 14:55:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|