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';
|
2017-08-18 21:30:16 +00:00
|
|
|
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
|
2017-09-01 20:20:34 +00:00
|
|
|
import Avatar from '../containers/Avatar';
|
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: {
|
2017-08-17 19:31:27 +00:00
|
|
|
// flex: 1,
|
2017-08-09 18:01:54 +00:00
|
|
|
flexDirection: 'row',
|
2017-08-13 15:10:50 +00:00
|
|
|
paddingLeft: 16,
|
2017-08-17 19:31:27 +00:00
|
|
|
paddingRight: 16,
|
2017-08-13 15:10:50 +00:00
|
|
|
height: 56,
|
2017-08-09 18:01:54 +00:00
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
number: {
|
2017-08-09 20:08:50 +00:00
|
|
|
minWidth: 20,
|
2017-11-22 16:40:59 +00:00
|
|
|
borderRadius: 3,
|
2017-08-13 15:10:50 +00:00
|
|
|
backgroundColor: '#1d74f5',
|
2017-08-09 18:01:54 +00:00
|
|
|
color: '#fff',
|
|
|
|
textAlign: 'center',
|
|
|
|
overflow: 'hidden',
|
2017-08-13 15:10:50 +00:00
|
|
|
fontSize: 14,
|
2017-11-22 16:40:59 +00:00
|
|
|
paddingHorizontal: 5,
|
|
|
|
paddingVertical: 2
|
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,
|
|
|
|
marginLeft: 16,
|
|
|
|
marginRight: 4
|
2017-08-10 16:25:50 +00:00
|
|
|
},
|
2017-11-08 20:23:46 +00:00
|
|
|
roomName: {
|
|
|
|
paddingTop: 10,
|
|
|
|
flex: 1,
|
|
|
|
fontSize: 16,
|
|
|
|
height: 16,
|
|
|
|
color: '#444'
|
|
|
|
},
|
2017-11-22 16:40:59 +00:00
|
|
|
alert: {
|
|
|
|
fontWeight: 'bold'
|
|
|
|
},
|
|
|
|
favorite: {
|
|
|
|
// backgroundColor: '#eee'
|
|
|
|
},
|
2017-11-08 20:23:46 +00:00
|
|
|
update: {
|
|
|
|
flex: 1,
|
|
|
|
fontSize: 10,
|
|
|
|
height: 10,
|
|
|
|
color: '#888'
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class RoomItem extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2017-08-17 19:31:27 +00:00
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
2017-11-18 20:17:24 +00:00
|
|
|
_updatedAt: PropTypes.instanceOf(Date),
|
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,
|
2017-08-22 01:24:41 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2017-12-08 13:56:53 +00:00
|
|
|
onPress: PropTypes.func
|
2017-08-10 16:25:50 +00:00
|
|
|
}
|
2017-08-17 19:31:27 +00:00
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
get icon() {
|
2017-08-17 19:31:27 +00:00
|
|
|
const { type, name, baseUrl } = this.props;
|
2017-12-08 19:13:21 +00:00
|
|
|
return <Avatar text={name} baseUrl={baseUrl} size={40} type={type} />;
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
2017-08-13 03:19:14 +00:00
|
|
|
|
2017-12-08 13:56:53 +00:00
|
|
|
formatDate = date => moment(date).calendar(null, {
|
|
|
|
lastDay: 'dddd',
|
|
|
|
sameDay: 'HH:mm',
|
|
|
|
lastWeek: 'dddd',
|
|
|
|
sameElse: 'MMM D'
|
|
|
|
})
|
|
|
|
|
2017-11-22 16:40:59 +00:00
|
|
|
renderNumber = (unread, userMentions) => {
|
2017-08-17 19:31:27 +00:00
|
|
|
if (!unread || unread <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread >= 1000) {
|
|
|
|
unread = '999+';
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
2017-08-17 19:31:27 +00:00
|
|
|
|
2017-11-22 16:40:59 +00:00
|
|
|
if (userMentions > 0) {
|
|
|
|
unread = `@ ${ unread }`;
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:31:27 +00:00
|
|
|
return (
|
|
|
|
<Text style={styles.number}>
|
|
|
|
{ unread }
|
|
|
|
</Text>
|
|
|
|
);
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
render() {
|
2017-11-22 16:40:59 +00:00
|
|
|
const {
|
|
|
|
favorite, alert, unread, userMentions, name, _updatedAt
|
|
|
|
} = this.props;
|
2017-11-13 12:49:19 +00:00
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
return (
|
2017-11-22 16:40:59 +00:00
|
|
|
<TouchableOpacity onPress={this.props.onPress} style={[styles.container, favorite && styles.favorite]}>
|
2017-08-10 16:25:50 +00:00
|
|
|
{this.icon}
|
2017-11-08 20:23:46 +00:00
|
|
|
<View style={styles.roomNameView}>
|
2017-11-22 16:40:59 +00:00
|
|
|
<Text style={[styles.roomName, alert && styles.alert]} ellipsizeMode='tail' numberOfLines={1}>{ name }</Text>
|
2017-12-08 13:56:53 +00:00
|
|
|
{_updatedAt ? <Text style={styles.update} ellipsizeMode='tail' numberOfLines={1}>{ this.formatDate(_updatedAt) }</Text> : null}
|
2017-11-08 20:23:46 +00:00
|
|
|
</View>
|
2017-11-22 16:40:59 +00:00
|
|
|
{this.renderNumber(unread, userMentions)}
|
2017-08-18 21:30:16 +00:00
|
|
|
</TouchableOpacity>
|
2017-08-09 01:40:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|