import React from 'react'; import PropTypes from 'prop-types'; import { Vibration, ViewPropTypes } from 'react-native'; import { connect } from 'react-redux'; import equal from 'deep-equal'; import Message from './Message'; import { errorActionsShow as errorActionsShowAction, toggleReactionPicker as toggleReactionPickerAction, replyBroadcast as replyBroadcastAction } from '../../actions/messages'; @connect(state => ({ baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', customEmojis: state.customEmojis, editing: state.messages.editing, Message_GroupingPeriod: state.settings.Message_GroupingPeriod, Message_TimeFormat: state.settings.Message_TimeFormat, message: state.messages.message, useRealName: state.settings.UI_Use_Real_Name }), dispatch => ({ errorActionsShow: actionMessage => dispatch(errorActionsShowAction(actionMessage)), replyBroadcast: message => dispatch(replyBroadcastAction(message)), toggleReactionPicker: message => dispatch(toggleReactionPickerAction(message)) })) export default class MessageContainer extends React.Component { static propTypes = { item: PropTypes.object.isRequired, reactions: PropTypes.any.isRequired, user: PropTypes.shape({ id: PropTypes.string.isRequired, username: PropTypes.string.isRequired, token: PropTypes.string.isRequired }), customTimeFormat: PropTypes.string, style: ViewPropTypes.style, status: PropTypes.number, archived: PropTypes.bool, broadcast: PropTypes.bool, previousItem: PropTypes.object, _updatedAt: PropTypes.instanceOf(Date), // redux baseUrl: PropTypes.string, customEmojis: PropTypes.object, editing: PropTypes.bool, Message_GroupingPeriod: PropTypes.number, Message_TimeFormat: PropTypes.string, message: PropTypes.object, useRealName: PropTypes.bool, // methods - props onLongPress: PropTypes.func, onReactionPress: PropTypes.func, // methods - redux errorActionsShow: PropTypes.func, replyBroadcast: PropTypes.func, toggleReactionPicker: PropTypes.func } static defaultProps = { onLongPress: () => {}, _updatedAt: new Date(), archived: false, broadcast: false } constructor(props) { super(props); this.state = { reactionsModal: false }; this.closeReactions = this.closeReactions.bind(this); } shouldComponentUpdate(nextProps, nextState) { const { reactionsModal } = this.state; const { status, reactions, broadcast, editing, _updatedAt } = this.props; if (reactionsModal !== nextState.reactionsModal) { return true; } if (status !== nextProps.status) { return true; } // eslint-disable-next-line if (!!_updatedAt ^ !!nextProps._updatedAt) { return true; } if (!equal(reactions, nextProps.reactions)) { return true; } if (broadcast !== nextProps.broadcast) { return true; } if (editing !== nextProps.editing) { return true; } return _updatedAt.toGMTString() !== nextProps._updatedAt.toGMTString(); } onLongPress = () => { const { onLongPress } = this.props; onLongPress(this.parseMessage()); } onErrorPress = () => { const { errorActionsShow } = this.props; errorActionsShow(this.parseMessage()); } onReactionPress = (emoji) => { const { onReactionPress, item } = this.props; onReactionPress(emoji, item._id); } onReactionLongPress = () => { this.setState({ reactionsModal: true }); Vibration.vibrate(50); } get timeFormat() { const { customTimeFormat, Message_TimeFormat } = this.props; return customTimeFormat || Message_TimeFormat; } closeReactions = () => { this.setState({ reactionsModal: false }); } isHeader = () => { const { item, previousItem, broadcast, Message_GroupingPeriod } = this.props; if (previousItem && ( (previousItem.ts.toDateString() === item.ts.toDateString()) && (previousItem.u.username === item.u.username) && !(previousItem.groupable === false || item.groupable === false || broadcast === true) && (previousItem.status === item.status) && (item.ts - previousItem.ts < Message_GroupingPeriod * 1000) )) { return false; } return true; } parseMessage = () => { const { item } = this.props; return JSON.parse(JSON.stringify(item)); } toggleReactionPicker = () => { const { toggleReactionPicker } = this.props; toggleReactionPicker(this.parseMessage()); } replyBroadcast = () => { const { replyBroadcast } = this.props; replyBroadcast(this.parseMessage()); } render() { const { reactionsModal } = this.state; const { item, message, editing, user, style, archived, baseUrl, customEmojis, useRealName, broadcast } = this.props; const { msg, ts, attachments, urls, reactions, t, status, avatar, u, alias, editedBy, role } = item; const isEditing = message._id === item._id && editing; return ( ); } }