2017-09-01 19:17:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-12-08 19:36:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-02-08 14:08:50 +00:00
|
|
|
import { StyleSheet, Text, View, ViewPropTypes } from 'react-native';
|
2018-05-07 20:41:36 +00:00
|
|
|
import FastImage from 'react-native-fast-image';
|
2017-09-01 20:20:34 +00:00
|
|
|
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
|
2017-09-01 19:17:53 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2017-09-01 19:42:50 +00:00
|
|
|
iconContainer: {
|
2018-02-16 18:34:25 +00:00
|
|
|
// overflow: 'hidden',
|
2017-09-01 19:42:50 +00:00
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
2017-09-01 19:17:53 +00:00
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
position: 'absolute'
|
|
|
|
},
|
|
|
|
avatarInitials: {
|
|
|
|
color: '#ffffff'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
@connect(state => ({
|
2017-12-27 15:22:06 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
2017-12-08 19:36:03 +00:00
|
|
|
}))
|
2018-02-08 14:08:50 +00:00
|
|
|
export default class Avatar extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
style: ViewPropTypes.style,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
avatar: PropTypes.string,
|
|
|
|
size: PropTypes.number,
|
|
|
|
borderRadius: PropTypes.number,
|
2018-02-16 18:34:25 +00:00
|
|
|
type: PropTypes.string,
|
|
|
|
children: PropTypes.object
|
2018-02-08 14:08:50 +00:00
|
|
|
};
|
2018-05-07 20:41:36 +00:00
|
|
|
state = { showInitials: true };
|
2017-09-01 19:17:53 +00:00
|
|
|
render() {
|
2017-11-13 12:58:35 +00:00
|
|
|
const {
|
2018-04-24 19:34:03 +00:00
|
|
|
text = '', size = 25, baseUrl, borderRadius = 2, style, avatar, type = 'd'
|
2017-11-13 12:58:35 +00:00
|
|
|
} = this.props;
|
2017-09-01 19:42:50 +00:00
|
|
|
const { initials, color } = avatarInitialsAndColor(`${ text }`);
|
2017-09-01 20:20:34 +00:00
|
|
|
|
|
|
|
const iconContainerStyle = {
|
|
|
|
backgroundColor: color,
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
borderRadius
|
|
|
|
};
|
|
|
|
|
|
|
|
const avatarInitialsStyle = {
|
2018-05-18 16:41:47 +00:00
|
|
|
fontSize: size / 1.6,
|
|
|
|
fontWeight: '800'
|
2017-09-01 20:20:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const avatarStyle = {
|
|
|
|
width: size,
|
2017-09-21 17:08:00 +00:00
|
|
|
height: size,
|
|
|
|
borderRadius
|
2017-09-01 20:20:34 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 16:41:47 +00:00
|
|
|
let image;
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
if (type === 'd') {
|
|
|
|
const uri = avatar || `${ baseUrl }/avatar/${ text }`;
|
2018-05-18 16:41:47 +00:00
|
|
|
image = uri && (
|
2018-05-07 20:41:36 +00:00
|
|
|
<FastImage
|
2017-12-08 19:13:21 +00:00
|
|
|
style={[styles.avatar, avatarStyle]}
|
2018-05-07 20:41:36 +00:00
|
|
|
source={{
|
|
|
|
uri,
|
|
|
|
priority: FastImage.priority.high
|
|
|
|
}}
|
2017-12-08 19:13:21 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-01 19:17:53 +00:00
|
|
|
return (
|
2017-09-01 20:20:34 +00:00
|
|
|
<View style={[styles.iconContainer, iconContainerStyle, style]}>
|
2018-05-18 16:41:47 +00:00
|
|
|
{this.state.showInitials &&
|
|
|
|
<Text
|
|
|
|
style={[styles.avatarInitials, avatarInitialsStyle]}
|
|
|
|
allowFontScaling={false}
|
|
|
|
>
|
|
|
|
{initials}
|
|
|
|
</Text>
|
|
|
|
}
|
|
|
|
{image}
|
|
|
|
{this.props.children}
|
2017-12-08 19:13:21 +00:00
|
|
|
</View>
|
|
|
|
);
|
2017-09-01 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|