2017-09-01 19:17:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-10-29 13:53:32 +00:00
|
|
|
import { View, ViewPropTypes } from 'react-native';
|
2018-05-07 20:41:36 +00:00
|
|
|
import FastImage from 'react-native-fast-image';
|
2017-09-01 19:17:53 +00:00
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
export default class Avatar extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2018-02-08 14:08:50 +00:00
|
|
|
style: ViewPropTypes.style,
|
2018-06-13 01:33:00 +00:00
|
|
|
text: PropTypes.string,
|
2018-02-08 14:08:50 +00:00
|
|
|
avatar: PropTypes.string,
|
|
|
|
size: PropTypes.number,
|
|
|
|
borderRadius: PropTypes.number,
|
2018-02-16 18:34:25 +00:00
|
|
|
type: PropTypes.string,
|
2018-10-29 13:53:32 +00:00
|
|
|
children: PropTypes.object
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
static defaultProps = {
|
|
|
|
text: '',
|
|
|
|
size: 25,
|
|
|
|
type: 'd',
|
2018-10-29 13:53:32 +00:00
|
|
|
borderRadius: 4
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 19:17:53 +00:00
|
|
|
render() {
|
2017-11-13 12:58:35 +00:00
|
|
|
const {
|
2018-10-29 13:53:32 +00:00
|
|
|
text, size, baseUrl, borderRadius, style, avatar, type, children
|
2017-11-13 12:58:35 +00:00
|
|
|
} = this.props;
|
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-12-05 20:52:08 +00:00
|
|
|
if (!text && !avatar) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-29 13:53:32 +00:00
|
|
|
const room = type === 'd' ? text : `@${ text }`;
|
2018-12-05 20:52:08 +00:00
|
|
|
// Avoid requesting several sizes by having only two sizes on cache
|
|
|
|
const uriSize = size === 100 ? 100 : 50;
|
|
|
|
const uri = avatar || `${ baseUrl }/avatar/${ room }?format=png&width=${ uriSize }&height=${ uriSize }`;
|
|
|
|
|
2018-10-29 13:53:32 +00:00
|
|
|
const image = (
|
|
|
|
<FastImage
|
|
|
|
style={avatarStyle}
|
|
|
|
source={{
|
|
|
|
uri,
|
2018-10-31 18:41:19 +00:00
|
|
|
priority: FastImage.priority.high
|
2018-10-29 13:53:32 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2017-12-08 19:13:21 +00:00
|
|
|
|
2017-09-01 19:17:53 +00:00
|
|
|
return (
|
2018-10-29 13:53:32 +00:00
|
|
|
<View style={[avatarStyle, style]}>
|
2018-05-18 16:41:47 +00:00
|
|
|
{image}
|
2018-09-25 19:28:42 +00:00
|
|
|
{children}
|
2017-12-08 19:13:21 +00:00
|
|
|
</View>
|
|
|
|
);
|
2017-09-01 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|