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';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { View, Text, StyleSheet, ViewPropTypes } from 'react-native';
|
2018-05-18 16:41:47 +00:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2018-02-16 18:34:25 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import SimpleMarkdown from 'simple-markdown';
|
|
|
|
|
2017-09-01 20:20:34 +00:00
|
|
|
import Avatar from '../containers/Avatar';
|
2018-02-16 18:34:25 +00:00
|
|
|
import Status from '../containers/status';
|
2017-12-20 15:57:33 +00:00
|
|
|
import Touch from '../utils/touch/index'; //eslint-disable-line
|
2018-02-16 18:34:25 +00:00
|
|
|
import Markdown from '../containers/message/Markdown';
|
2018-05-18 16:41:47 +00:00
|
|
|
import RoomTypeIcon from '../containers/RoomTypeIcon';
|
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',
|
2017-12-11 20:37:33 +00:00
|
|
|
paddingHorizontal: 16,
|
2018-02-16 18:34:25 +00:00
|
|
|
paddingVertical: 12,
|
|
|
|
alignItems: 'center',
|
|
|
|
borderBottomWidth: 0.5,
|
|
|
|
borderBottomColor: '#ddd'
|
2017-08-09 18:01:54 +00:00
|
|
|
},
|
|
|
|
number: {
|
2018-02-16 18:34:25 +00:00
|
|
|
minWidth: 25,
|
|
|
|
borderRadius: 4,
|
2017-08-13 15:10:50 +00:00
|
|
|
backgroundColor: '#1d74f5',
|
2017-08-09 18:01:54 +00:00
|
|
|
color: '#fff',
|
|
|
|
overflow: 'hidden',
|
2017-08-13 15:10:50 +00:00
|
|
|
fontSize: 14,
|
2018-02-16 18:34:25 +00:00
|
|
|
paddingVertical: 4,
|
2017-11-22 16:40:59 +00:00
|
|
|
paddingHorizontal: 5,
|
2018-02-16 18:34:25 +00:00
|
|
|
|
|
|
|
textAlign: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
2017-08-09 18:01:54 +00:00
|
|
|
},
|
2017-11-08 20:23:46 +00:00
|
|
|
roomNameView: {
|
2017-08-17 19:31:27 +00:00
|
|
|
flex: 1,
|
2018-02-16 18:34:25 +00:00
|
|
|
height: '100%',
|
2017-08-17 19:31:27 +00:00
|
|
|
marginLeft: 16,
|
|
|
|
marginRight: 4
|
2017-08-10 16:25:50 +00:00
|
|
|
},
|
2017-11-08 20:23:46 +00:00
|
|
|
roomName: {
|
|
|
|
flex: 1,
|
2018-02-16 18:34:25 +00:00
|
|
|
fontSize: 18,
|
|
|
|
color: '#444',
|
|
|
|
marginRight: 8
|
|
|
|
},
|
|
|
|
lastMessage: {
|
|
|
|
flex: 1,
|
|
|
|
flexShrink: 1,
|
|
|
|
marginRight: 8,
|
|
|
|
maxHeight: 20,
|
|
|
|
overflow: 'hidden',
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
justifyContent: 'flex-start'
|
2017-11-08 20:23:46 +00:00
|
|
|
},
|
2017-11-22 16:40:59 +00:00
|
|
|
alert: {
|
|
|
|
fontWeight: 'bold'
|
|
|
|
},
|
|
|
|
favorite: {
|
|
|
|
// backgroundColor: '#eee'
|
|
|
|
},
|
2018-02-16 18:34:25 +00:00
|
|
|
row: {
|
2018-04-24 19:34:03 +00:00
|
|
|
// width: '100%',
|
|
|
|
// flex: 1,
|
2018-02-16 18:34:25 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
justifyContent: 'flex-end'
|
|
|
|
},
|
|
|
|
firstRow: {
|
|
|
|
width: '100%',
|
2017-11-08 20:23:46 +00:00
|
|
|
flex: 1,
|
2018-02-16 18:34:25 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
update: {
|
2017-11-08 20:23:46 +00:00
|
|
|
fontSize: 10,
|
2018-02-16 18:34:25 +00:00
|
|
|
color: '#888',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
2018-03-02 15:11:34 +00:00
|
|
|
updateAlert: {
|
|
|
|
color: '#1d74f5'
|
|
|
|
},
|
2018-02-16 18:34:25 +00:00
|
|
|
status: {
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: -3,
|
|
|
|
right: -3,
|
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: '#fff'
|
2018-05-18 16:41:47 +00:00
|
|
|
},
|
|
|
|
type: {
|
|
|
|
marginRight: 5,
|
|
|
|
marginTop: 3
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
|
|
|
});
|
2018-02-16 18:34:25 +00:00
|
|
|
const markdownStyle = { block: { marginBottom: 0, flexWrap: 'wrap', flexDirection: 'row' } };
|
|
|
|
|
|
|
|
const parseInline = (parse, content, state) => {
|
|
|
|
const isCurrentlyInline = state.inline || false;
|
|
|
|
state.inline = true;
|
|
|
|
const result = parse(content, state);
|
|
|
|
state.inline = isCurrentlyInline;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const parseCaptureInline = (capture, parse, state) => ({ content: parseInline(parse, capture[1], state) });
|
|
|
|
const customRules = {
|
|
|
|
strong: {
|
|
|
|
order: -4,
|
|
|
|
match: SimpleMarkdown.inlineRegex(/^\*\*([\s\S]+?)\*\*(?!\*)/),
|
|
|
|
parse: parseCaptureInline,
|
|
|
|
react: (node, output, state) => ({
|
|
|
|
type: 'strong',
|
|
|
|
key: state.key,
|
|
|
|
props: {
|
|
|
|
children: output(node.content, state)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
order: -3,
|
|
|
|
match: SimpleMarkdown.inlineRegex(/^[\s\S]+?(?=[^0-9A-Za-z\s\u00c0-\uffff]|\n\n| {2,}\n|\w+:\S|$)/),
|
|
|
|
parse: capture => ({ content: capture[0] }),
|
|
|
|
react: node => node.content
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderNumber = (unread, userMentions) => {
|
|
|
|
if (!unread || unread <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread >= 1000) {
|
|
|
|
unread = '999+';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userMentions > 0) {
|
|
|
|
unread = `@ ${ unread }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Text style={styles.number}>
|
|
|
|
{ unread }
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2018-05-24 20:17:00 +00:00
|
|
|
const attrs = ['name', 'unread', 'userMentions', 'alert', 'showLastMessage', 'type'];
|
2018-02-16 18:34:25 +00:00
|
|
|
@connect(state => ({
|
|
|
|
user: state.login && state.login.user,
|
2018-04-24 19:34:03 +00:00
|
|
|
StoreLastMessage: state.settings.Store_Last_Message
|
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-02-16 18:34:25 +00:00
|
|
|
StoreLastMessage: PropTypes.bool,
|
2017-11-18 20:17:24 +00:00
|
|
|
_updatedAt: PropTypes.instanceOf(Date),
|
2018-02-16 18:34:25 +00:00
|
|
|
lastMessage: PropTypes.object,
|
2018-04-24 19:34:03 +00:00
|
|
|
showLastMessage: PropTypes.bool,
|
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,
|
2018-04-24 19:34:03 +00:00
|
|
|
onLongPress: PropTypes.func,
|
|
|
|
user: PropTypes.object,
|
|
|
|
avatarSize: PropTypes.number,
|
2018-05-23 13:39:18 +00:00
|
|
|
statusStyle: ViewPropTypes.style,
|
|
|
|
testID: PropTypes.string
|
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 = {
|
|
|
|
showLastMessage: true,
|
|
|
|
avatarSize: 46
|
|
|
|
}
|
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
const oldlastMessage = this.props.lastMessage;
|
|
|
|
const newLastmessage = nextProps.lastMessage;
|
|
|
|
|
2018-05-24 20:17:00 +00:00
|
|
|
if (oldlastMessage && newLastmessage && oldlastMessage.ts.toGMTString() !== newLastmessage.ts.toGMTString()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.props._updatedAt && nextProps._updatedAt && nextProps._updatedAt.toGMTString() !== this.props._updatedAt.toGMTString()) {
|
2018-04-24 19:34:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return attrs.some(key => nextProps[key] !== this.props[key]);
|
|
|
|
}
|
2017-08-10 16:25:50 +00:00
|
|
|
get icon() {
|
2018-02-16 18:34:25 +00:00
|
|
|
const {
|
2018-04-24 19:34:03 +00:00
|
|
|
type, name, id, avatarSize, statusStyle
|
2018-02-16 18:34:25 +00:00
|
|
|
} = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
return (<Avatar text={name} size={avatarSize} type={type}>{type === 'd' ? <Status style={[styles.status, statusStyle]} id={id} /> : null }</Avatar>);
|
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 {
|
2018-04-24 19:34:03 +00:00
|
|
|
lastMessage, type, showLastMessage
|
2018-02-16 18:34:25 +00:00
|
|
|
} = this.props;
|
2017-12-08 13:56:53 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (!this.props.StoreLastMessage || !showLastMessage) {
|
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) {
|
|
|
|
return '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 = '';
|
|
|
|
|
|
|
|
if (lastMessage.u.username === this.props.user.username) {
|
|
|
|
prefix = 'You: ';
|
|
|
|
} else if (type !== 'd') {
|
|
|
|
prefix = `${ lastMessage.u.username }: `;
|
2017-11-22 16:40:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 18:34:25 +00:00
|
|
|
const msg = `${ prefix }${ lastMessage.msg.replace(/[\n\t\r]/igm, '') }`;
|
|
|
|
const maxChars = 35;
|
|
|
|
return `${ msg.slice(0, maxChars) }${ msg.replace(/:[a-z0-9]+:/gi, ':::').length > maxChars ? '...' : '' }`;
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 16:41:47 +00:00
|
|
|
get type() {
|
|
|
|
const icon = {
|
|
|
|
c: 'pound',
|
|
|
|
p: 'lock',
|
|
|
|
l: 'account',
|
|
|
|
d: 'at'
|
|
|
|
}[this.props.type];
|
|
|
|
return <Icon name={icon} size={15} style={styles.type} />;
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:34:25 +00:00
|
|
|
formatDate = date => moment(date).calendar(null, {
|
|
|
|
lastDay: '[Yesterday]',
|
|
|
|
sameDay: 'h:mm A',
|
|
|
|
lastWeek: 'dddd',
|
|
|
|
sameElse: 'MMM D'
|
|
|
|
})
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
render() {
|
2017-11-22 16:40:59 +00:00
|
|
|
const {
|
2018-05-23 13:39:18 +00:00
|
|
|
favorite, unread, userMentions, name, _updatedAt, alert, type, testID
|
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) {
|
|
|
|
accessibilityLabel += `, ${ unread } alert`;
|
|
|
|
} else if (unread > 1) {
|
|
|
|
accessibilityLabel += `, ${ unread } alerts`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userMentions > 0) {
|
|
|
|
accessibilityLabel += ', you were mentioned';
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (date) {
|
|
|
|
accessibilityLabel += `, last message ${ date }`;
|
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<Touch
|
|
|
|
onPress={this.props.onPress}
|
|
|
|
onLongPress={this.props.onLongPress}
|
|
|
|
underlayColor='#FFFFFF'
|
|
|
|
activeOpacity={0.5}
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
|
|
|
accessibilityTraits='selected'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={testID}
|
2018-04-24 19:34:03 +00:00
|
|
|
>
|
2017-12-20 15:57:33 +00:00
|
|
|
<View style={[styles.container, favorite && styles.favorite]}>
|
|
|
|
{this.icon}
|
|
|
|
<View style={styles.roomNameView}>
|
2018-02-16 18:34:25 +00:00
|
|
|
<View style={styles.firstRow}>
|
2018-05-18 16:41:47 +00:00
|
|
|
<RoomTypeIcon type={type} />
|
2018-02-16 18:34:25 +00:00
|
|
|
<Text style={[styles.roomName, alert && styles.alert]} ellipsizeMode='tail' numberOfLines={1}>{ name }</Text>
|
2018-03-02 15:11:34 +00:00
|
|
|
{_updatedAt ? <Text style={[styles.update, alert && styles.updateAlert]} ellipsizeMode='tail' numberOfLines={1}>{ date }</Text> : null}
|
2018-02-16 18:34:25 +00:00
|
|
|
</View>
|
|
|
|
<View style={styles.row}>
|
|
|
|
<Markdown
|
|
|
|
msg={this.lastMessage}
|
|
|
|
style={styles.lastMessage}
|
|
|
|
markdownStyle={markdownStyle}
|
|
|
|
customRules={customRules}
|
|
|
|
renderInline
|
|
|
|
numberOfLines={1}
|
|
|
|
/>
|
|
|
|
{renderNumber(unread, userMentions)}
|
|
|
|
</View>
|
2017-12-20 15:57:33 +00:00
|
|
|
</View>
|
2017-11-08 20:23:46 +00:00
|
|
|
</View>
|
2017-12-20 15:57:33 +00:00
|
|
|
</Touch>
|
2017-08-09 01:40:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|