2017-08-09 01:40:55 +00:00
|
|
|
import React from 'react';
|
2017-11-08 20:23:46 +00:00
|
|
|
import moment from 'moment';
|
2017-08-09 01:40:55 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { View, Text, StyleSheet } from 'react-native';
|
2018-02-16 18:34:25 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-08-31 16:46:33 +00:00
|
|
|
import { emojify } from 'react-emojione';
|
2018-09-26 19:38:06 +00:00
|
|
|
import { RectButton } from 'react-native-gesture-handler';
|
2018-02-16 18:34:25 +00:00
|
|
|
|
2017-09-01 20:20:34 +00:00
|
|
|
import Avatar from '../containers/Avatar';
|
2019-03-01 16:49:11 +00:00
|
|
|
import Status from '../containers/Status';
|
2018-05-18 16:41:47 +00:00
|
|
|
import RoomTypeIcon from '../containers/RoomTypeIcon';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2019-01-29 19:52:56 +00:00
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2017-08-13 03:19:14 +00:00
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
const styles = StyleSheet.create({
|
2017-08-09 18:01:54 +00:00
|
|
|
container: {
|
|
|
|
flexDirection: 'row',
|
2019-03-01 16:49:11 +00:00
|
|
|
alignItems: 'center',
|
|
|
|
marginHorizontal: 15
|
2017-08-09 18:01:54 +00:00
|
|
|
},
|
2018-08-31 16:46:33 +00:00
|
|
|
centerContainer: {
|
2017-08-17 19:31:27 +00:00
|
|
|
flex: 1,
|
2019-03-01 16:49:11 +00:00
|
|
|
height: '100%'
|
2017-08-10 16:25:50 +00:00
|
|
|
},
|
2018-08-31 16:46:33 +00:00
|
|
|
title: {
|
2017-11-08 20:23:46 +00:00
|
|
|
flex: 1,
|
2018-02-16 18:34:25 +00:00
|
|
|
fontSize: 18,
|
2018-08-31 16:46:33 +00:00
|
|
|
color: '#0C0D0F',
|
|
|
|
fontWeight: '400',
|
|
|
|
marginRight: 5,
|
|
|
|
paddingTop: 0,
|
|
|
|
paddingBottom: 0
|
2018-02-16 18:34:25 +00:00
|
|
|
},
|
2017-11-22 16:40:59 +00:00
|
|
|
alert: {
|
2018-08-31 16:46:33 +00:00
|
|
|
fontWeight: '600'
|
2017-11-22 16:40:59 +00:00
|
|
|
},
|
2018-02-16 18:34:25 +00:00
|
|
|
row: {
|
2018-05-29 17:09:20 +00:00
|
|
|
flex: 1,
|
2018-02-16 18:34:25 +00:00
|
|
|
flexDirection: 'row',
|
2018-08-31 16:46:33 +00:00
|
|
|
alignItems: 'flex-start'
|
2018-02-16 18:34:25 +00:00
|
|
|
},
|
2018-08-31 16:46:33 +00:00
|
|
|
titleContainer: {
|
2018-02-16 18:34:25 +00:00
|
|
|
width: '100%',
|
2019-01-29 19:52:56 +00:00
|
|
|
marginTop: isIOS ? 5 : 2,
|
2018-02-16 18:34:25 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
2018-08-31 16:46:33 +00:00
|
|
|
date: {
|
|
|
|
fontSize: 14,
|
|
|
|
color: '#9EA2A8',
|
|
|
|
fontWeight: 'normal',
|
|
|
|
paddingTop: 0,
|
|
|
|
paddingBottom: 0
|
|
|
|
},
|
|
|
|
updateAlert: {
|
2019-03-01 16:49:11 +00:00
|
|
|
color: '#1D74F5',
|
|
|
|
fontWeight: '700'
|
2018-08-31 16:46:33 +00:00
|
|
|
},
|
|
|
|
unreadNumberContainer: {
|
|
|
|
minWidth: 23,
|
|
|
|
padding: 3,
|
|
|
|
borderRadius: 4,
|
|
|
|
backgroundColor: '#1D74F5',
|
2018-02-16 18:34:25 +00:00
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
2018-08-31 16:46:33 +00:00
|
|
|
unreadNumberText: {
|
|
|
|
color: '#fff',
|
|
|
|
overflow: 'hidden',
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: '500',
|
|
|
|
letterSpacing: 0.56
|
2018-03-02 15:11:34 +00:00
|
|
|
},
|
2018-02-16 18:34:25 +00:00
|
|
|
status: {
|
2018-08-31 16:46:33 +00:00
|
|
|
marginRight: 7,
|
2018-05-18 16:41:47 +00:00
|
|
|
marginTop: 3
|
2018-08-31 16:46:33 +00:00
|
|
|
},
|
|
|
|
markdownText: {
|
|
|
|
flex: 1,
|
|
|
|
color: '#9EA2A8',
|
|
|
|
fontSize: 15,
|
|
|
|
fontWeight: 'normal'
|
2019-03-01 16:49:11 +00:00
|
|
|
},
|
|
|
|
markdownTextAlert: {
|
|
|
|
color: '#0C0D0F'
|
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
marginRight: 10
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
|
|
|
});
|
2018-02-16 18:34:25 +00:00
|
|
|
|
|
|
|
const renderNumber = (unread, userMentions) => {
|
|
|
|
if (!unread || unread <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread >= 1000) {
|
|
|
|
unread = '999+';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userMentions > 0) {
|
|
|
|
unread = `@ ${ unread }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2018-08-31 16:46:33 +00:00
|
|
|
<View style={styles.unreadNumberContainer}>
|
|
|
|
<Text style={styles.unreadNumberText}>{ unread }</Text>
|
|
|
|
</View>
|
2018-02-16 18:34:25 +00:00
|
|
|
);
|
|
|
|
};
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
const attrs = ['name', 'unread', 'userMentions', 'StoreLastMessage', 'alert', 'type'];
|
2018-02-16 18:34:25 +00:00
|
|
|
@connect(state => ({
|
2019-02-07 19:58:20 +00:00
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
username: state.login.user && state.login.user.username,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
},
|
2018-09-11 16:32:52 +00:00
|
|
|
StoreLastMessage: state.settings.Store_Last_Message,
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
2018-02-16 18:34:25 +00:00
|
|
|
}))
|
2018-04-24 19:34:03 +00:00
|
|
|
export default class RoomItem extends React.Component {
|
2017-08-09 01:40:55 +00:00
|
|
|
static propTypes = {
|
2017-08-17 19:31:27 +00:00
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2018-02-16 18:34:25 +00:00
|
|
|
StoreLastMessage: PropTypes.bool,
|
2018-10-18 17:56:49 +00:00
|
|
|
_updatedAt: PropTypes.string,
|
2018-02-16 18:34:25 +00:00
|
|
|
lastMessage: PropTypes.object,
|
2017-11-22 16:40:59 +00:00
|
|
|
favorite: PropTypes.bool,
|
|
|
|
alert: PropTypes.bool,
|
2017-08-17 19:31:27 +00:00
|
|
|
unread: PropTypes.number,
|
2017-11-22 16:40:59 +00:00
|
|
|
userMentions: PropTypes.number,
|
2018-02-16 18:34:25 +00:00
|
|
|
id: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
username: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
}),
|
2018-04-24 19:34:03 +00:00
|
|
|
avatarSize: PropTypes.number,
|
2018-08-31 16:46:33 +00:00
|
|
|
testID: PropTypes.string,
|
|
|
|
height: PropTypes.number
|
2017-08-10 16:25:50 +00:00
|
|
|
}
|
2017-08-17 19:31:27 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
static defaultProps = {
|
2018-08-31 16:46:33 +00:00
|
|
|
avatarSize: 48
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
shouldComponentUpdate(nextProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { lastMessage, _updatedAt } = this.props;
|
|
|
|
const oldlastMessage = lastMessage;
|
2018-04-24 19:34:03 +00:00
|
|
|
const newLastmessage = nextProps.lastMessage;
|
|
|
|
|
2018-10-18 17:56:49 +00:00
|
|
|
if (oldlastMessage && newLastmessage && oldlastMessage.ts !== newLastmessage.ts) {
|
2018-05-24 20:17:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-10-18 17:56:49 +00:00
|
|
|
if (_updatedAt && nextProps._updatedAt && nextProps._updatedAt !== _updatedAt) {
|
2018-04-24 19:34:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
// eslint-disable-next-line react/destructuring-assignment
|
2018-04-24 19:34:03 +00:00
|
|
|
return attrs.some(key => nextProps[key] !== this.props[key]);
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
get avatar() {
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
2019-02-07 19:58:20 +00:00
|
|
|
type, name, avatarSize, baseUrl, user
|
2018-09-11 16:32:52 +00:00
|
|
|
} = this.props;
|
2019-03-01 16:49:11 +00:00
|
|
|
return <Avatar text={name} size={avatarSize} type={type} baseUrl={baseUrl} style={styles.avatar} user={user} />;
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
2017-08-13 03:19:14 +00:00
|
|
|
|
2018-02-16 18:34:25 +00:00
|
|
|
get lastMessage() {
|
|
|
|
const {
|
2019-03-12 16:23:06 +00:00
|
|
|
lastMessage, type, StoreLastMessage, user
|
2018-02-16 18:34:25 +00:00
|
|
|
} = this.props;
|
2017-12-08 13:56:53 +00:00
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
if (!StoreLastMessage) {
|
2018-02-16 18:34:25 +00:00
|
|
|
return '';
|
2017-08-17 19:31:27 +00:00
|
|
|
}
|
2018-02-16 18:34:25 +00:00
|
|
|
if (!lastMessage) {
|
2018-06-01 17:38:13 +00:00
|
|
|
return I18n.t('No_Message');
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
2017-08-17 19:31:27 +00:00
|
|
|
|
2018-02-16 18:34:25 +00:00
|
|
|
let prefix = '';
|
2019-02-07 19:58:20 +00:00
|
|
|
const me = lastMessage.u.username === user.username;
|
2018-10-26 13:24:00 +00:00
|
|
|
|
|
|
|
if (!lastMessage.msg && Object.keys(lastMessage.attachments).length > 0) {
|
|
|
|
if (me) {
|
|
|
|
return I18n.t('User_sent_an_attachment', { user: I18n.t('You') });
|
|
|
|
} else {
|
|
|
|
return I18n.t('User_sent_an_attachment', { user: lastMessage.u.username });
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 18:34:25 +00:00
|
|
|
|
2018-10-26 13:24:00 +00:00
|
|
|
if (me) {
|
2018-06-01 17:38:13 +00:00
|
|
|
prefix = I18n.t('You_colon');
|
2018-02-16 18:34:25 +00:00
|
|
|
} else if (type !== 'd') {
|
|
|
|
prefix = `${ lastMessage.u.username }: `;
|
2017-11-22 16:40:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
let msg = `${ prefix }${ lastMessage.msg.replace(/[\n\t\r]/igm, '') }`;
|
|
|
|
msg = emojify(msg, { output: 'unicode' });
|
|
|
|
return msg;
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 16:41:47 +00:00
|
|
|
get type() {
|
2018-08-31 16:46:33 +00:00
|
|
|
const { type, id } = this.props;
|
|
|
|
if (type === 'd') {
|
2019-03-01 16:49:11 +00:00
|
|
|
return <Status style={styles.status} size={10} id={id} />;
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
2019-03-01 16:49:11 +00:00
|
|
|
return <RoomTypeIcon type={type} />;
|
2018-05-18 16:41:47 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 18:34:25 +00:00
|
|
|
formatDate = date => moment(date).calendar(null, {
|
2018-06-01 17:38:13 +00:00
|
|
|
lastDay: `[${ I18n.t('Yesterday') }]`,
|
2019-03-27 20:06:57 +00:00
|
|
|
sameDay: 'HH:mm',
|
2018-02-16 18:34:25 +00:00
|
|
|
lastWeek: 'dddd',
|
|
|
|
sameElse: 'MMM D'
|
|
|
|
})
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
render() {
|
2017-11-22 16:40:59 +00:00
|
|
|
const {
|
2018-09-26 19:38:06 +00:00
|
|
|
favorite, unread, userMentions, name, _updatedAt, alert, testID, height, onPress
|
2017-11-22 16:40:59 +00:00
|
|
|
} = this.props;
|
2017-11-13 12:49:19 +00:00
|
|
|
|
2017-12-11 20:37:33 +00:00
|
|
|
const date = this.formatDate(_updatedAt);
|
|
|
|
|
|
|
|
let accessibilityLabel = name;
|
|
|
|
if (unread === 1) {
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel += `, ${ unread } ${ I18n.t('alert') }`;
|
2017-12-11 20:37:33 +00:00
|
|
|
} else if (unread > 1) {
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel += `, ${ unread } ${ I18n.t('alerts') }`;
|
2017-12-11 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (userMentions > 0) {
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel += `, ${ I18n.t('you_were_mentioned') }`;
|
2017-12-11 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (date) {
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel += `, ${ I18n.t('last_message') } ${ date }`;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
return (
|
2018-09-26 19:38:06 +00:00
|
|
|
<RectButton
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress={onPress}
|
2018-09-26 19:38:06 +00:00
|
|
|
activeOpacity={0.8}
|
|
|
|
underlayColor='#e1e5e8'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={testID}
|
2018-04-24 19:34:03 +00:00
|
|
|
>
|
2018-09-26 19:38:06 +00:00
|
|
|
<View
|
|
|
|
style={[styles.container, favorite && styles.favorite, height && { height }]}
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
|
|
|
>
|
2018-08-31 16:46:33 +00:00
|
|
|
{this.avatar}
|
|
|
|
<View style={styles.centerContainer}>
|
|
|
|
<View style={styles.titleContainer}>
|
|
|
|
{this.type}
|
|
|
|
<Text style={[styles.title, alert && styles.alert]} ellipsizeMode='tail' numberOfLines={1}>{ name }</Text>
|
|
|
|
{_updatedAt ? <Text style={[styles.date, alert && styles.updateAlert]} ellipsizeMode='tail' numberOfLines={1}>{ date }</Text> : null}
|
2018-02-16 18:34:25 +00:00
|
|
|
</View>
|
|
|
|
<View style={styles.row}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<Text style={[styles.markdownText, alert && styles.markdownTextAlert]} numberOfLines={2}>
|
2018-08-31 16:46:33 +00:00
|
|
|
{this.lastMessage}
|
|
|
|
</Text>
|
2018-02-16 18:34:25 +00:00
|
|
|
{renderNumber(unread, userMentions)}
|
|
|
|
</View>
|
2017-12-20 15:57:33 +00:00
|
|
|
</View>
|
2017-11-08 20:23:46 +00:00
|
|
|
</View>
|
2018-09-26 19:38:06 +00:00
|
|
|
</RectButton>
|
2017-08-09 01:40:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|