Rocket.Chat.ReactNative/app/components/RoomItem.js

122 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-08-09 01:40:55 +00:00
import React from 'react';
2017-08-13 03:19:14 +00:00
import { CachedImage } from 'react-native-img-cache';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
2017-08-09 01:40:55 +00:00
import PropTypes from 'prop-types';
2017-08-09 18:01:54 +00:00
import { View, Text, StyleSheet } from 'react-native';
2017-08-09 01:40:55 +00:00
2017-08-13 03:19:14 +00:00
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
2017-08-09 01:40:55 +00:00
const styles = StyleSheet.create({
2017-08-09 18:01:54 +00:00
container: {
flex: 1,
flexDirection: 'row',
2017-08-13 03:19:14 +00:00
padding: 10,
paddingLeft: 14,
2017-08-09 18:01:54 +00:00
alignItems: 'center'
},
number: {
2017-08-09 20:08:50 +00:00
minWidth: 20,
padding: 2,
2017-08-09 18:01:54 +00:00
borderRadius: 5,
2017-08-09 20:08:50 +00:00
backgroundColor: '#aaa',
2017-08-09 18:01:54 +00:00
color: '#fff',
textAlign: 'center',
overflow: 'hidden',
marginRight: 15,
fontSize: 14
2017-08-09 18:01:54 +00:00
},
2017-08-09 01:40:55 +00:00
roomItem: {
flexGrow: 1,
fontSize: 20,
color: '#444'
},
2017-08-13 03:19:14 +00:00
iconContainer: {
marginTop: 5,
2017-08-13 03:19:14 +00:00
marginRight: 10,
backgroundColor: '#ccc',
height: 40,
width: 40,
borderRadius: 20,
overflow: 'hidden',
justifyContent: 'center',
alignItems: 'center'
},
icon: {
fontSize: 20,
color: '#fff',
backgroundColor: '#ccc',
height: 36,
width: 36,
borderRadius: 18,
overflow: 'hidden',
textAlign: 'center',
lineHeight: 36
},
avatar: {
width: 40,
height: 40,
position: 'absolute'
},
avatarInitials: {
fontSize: 22,
color: '#ffffff'
2017-08-09 01:40:55 +00:00
}
});
export default class RoomItem extends React.PureComponent {
static propTypes = {
2017-08-13 03:19:14 +00:00
item: PropTypes.object.isRequired,
baseUrl: PropTypes.string.isRequired
}
get icon() {
const icon = {
d: 'at',
c: 'pound',
p: 'lock',
l: 'account'
}[this.props.item.t];
2017-08-13 03:19:14 +00:00
if (!icon) {
return null;
}
2017-08-13 03:19:14 +00:00
if (this.props.item.t === 'd') {
const { name } = this.props.item;
const { initials, color } = avatarInitialsAndColor(name);
return (
<View style={[styles.iconContainer, { backgroundColor: color }]}>
<Text style={styles.avatarInitials}>{initials}</Text>
<CachedImage style={styles.avatar} source={{ uri: `${ this.props.baseUrl }/avatar/${ name }` }} />
</View>
);
}
return (
<View style={styles.iconContainer}>
<MaterialCommunityIcons name={icon} style={styles.icon} />
</View>
);
2017-08-09 01:40:55 +00:00
}
2017-08-13 03:19:14 +00:00
2017-08-09 18:01:54 +00:00
renderNumber = (item) => {
if (item.unread) {
return (
<Text style={styles.number}>
{ item.unread }
</Text>
);
}
}
2017-08-09 01:40:55 +00:00
render() {
2017-08-13 03:19:14 +00:00
const { name } = this.props.item;
2017-08-09 01:40:55 +00:00
return (
2017-08-09 18:01:54 +00:00
<View style={styles.container}>
{this.icon}
<Text style={styles.roomItem}>{ name }</Text>
2017-08-09 18:01:54 +00:00
{this.renderNumber(this.props.item)}
</View>
2017-08-09 01:40:55 +00:00
);
}
}