vn-verdnaturachat/app/containers/Avatar.js

63 lines
1.3 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { View, ViewPropTypes } from 'react-native';
import FastImage from 'react-native-fast-image';
export default class Avatar extends React.PureComponent {
static propTypes = {
baseUrl: PropTypes.string.isRequired,
style: ViewPropTypes.style,
text: PropTypes.string,
avatar: PropTypes.string,
size: PropTypes.number,
borderRadius: PropTypes.number,
type: PropTypes.string,
children: PropTypes.object
}
static defaultProps = {
text: '',
size: 25,
type: 'd',
borderRadius: 4
}
render() {
2017-11-13 12:58:35 +00:00
const {
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,
height: size,
borderRadius
2017-09-01 20:20:34 +00:00
};
2018-12-05 20:52:08 +00:00
if (!text && !avatar) {
return null;
}
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 }`;
const image = (
<FastImage
style={avatarStyle}
source={{
uri,
priority: FastImage.priority.high
}}
/>
);
return (
<View style={[avatarStyle, style]}>
{image}
{children}
</View>
);
}
}