2020-10-30 13:12:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { View } from 'react-native';
|
|
|
|
import FastImage from '@rocket.chat/react-native-fast-image';
|
|
|
|
import Touchable from 'react-native-platform-touchable';
|
|
|
|
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
|
|
|
|
|
|
|
|
import { avatarURL } from '../../utils/avatar';
|
|
|
|
import Emoji from '../markdown/Emoji';
|
|
|
|
|
|
|
|
const Avatar = React.memo(({
|
|
|
|
text,
|
|
|
|
size,
|
|
|
|
server,
|
|
|
|
borderRadius,
|
|
|
|
style,
|
|
|
|
avatar,
|
|
|
|
type,
|
|
|
|
children,
|
|
|
|
user,
|
|
|
|
onPress,
|
|
|
|
emoji,
|
|
|
|
theme,
|
|
|
|
getCustomEmoji,
|
|
|
|
avatarETag,
|
2020-10-30 13:51:04 +00:00
|
|
|
isStatic,
|
2020-10-30 15:54:02 +00:00
|
|
|
rid,
|
2020-11-04 16:53:44 +00:00
|
|
|
blockUnauthenticatedAccess,
|
|
|
|
serverVersion
|
2020-10-30 13:12:02 +00:00
|
|
|
}) => {
|
2020-10-30 13:51:04 +00:00
|
|
|
if ((!text && !avatar && !emoji && !rid) || !server) {
|
2020-10-30 13:12:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatarStyle = {
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
borderRadius
|
|
|
|
};
|
|
|
|
|
|
|
|
let image;
|
|
|
|
if (emoji) {
|
|
|
|
image = (
|
|
|
|
<Emoji
|
|
|
|
theme={theme}
|
|
|
|
baseUrl={server}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
isMessageContainsOnlyEmoji
|
|
|
|
literal={emoji}
|
|
|
|
style={avatarStyle}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let uri = avatar;
|
|
|
|
if (!isStatic) {
|
|
|
|
uri = avatarURL({
|
|
|
|
type,
|
|
|
|
text,
|
|
|
|
size,
|
|
|
|
user,
|
|
|
|
avatar,
|
|
|
|
server,
|
2020-10-30 13:51:04 +00:00
|
|
|
avatarETag,
|
2020-11-04 16:53:44 +00:00
|
|
|
serverVersion,
|
2020-10-30 15:54:02 +00:00
|
|
|
rid,
|
|
|
|
blockUnauthenticatedAccess
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
image = (
|
|
|
|
<FastImage
|
|
|
|
style={avatarStyle}
|
|
|
|
source={{
|
|
|
|
uri,
|
|
|
|
headers: RocketChatSettings.customHeaders,
|
|
|
|
priority: FastImage.priority.high
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onPress) {
|
|
|
|
image = (
|
|
|
|
<Touchable onPress={onPress}>
|
|
|
|
{image}
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={[avatarStyle, style]}>
|
|
|
|
{image}
|
|
|
|
{children}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Avatar.propTypes = {
|
|
|
|
server: PropTypes.string,
|
|
|
|
style: PropTypes.any,
|
|
|
|
text: PropTypes.string,
|
|
|
|
avatar: PropTypes.string,
|
|
|
|
emoji: PropTypes.string,
|
|
|
|
size: PropTypes.number,
|
|
|
|
borderRadius: PropTypes.number,
|
|
|
|
type: PropTypes.string,
|
|
|
|
children: PropTypes.object,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
}),
|
|
|
|
theme: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
|
|
|
getCustomEmoji: PropTypes.func,
|
|
|
|
avatarETag: PropTypes.string,
|
2020-10-30 13:51:04 +00:00
|
|
|
isStatic: PropTypes.bool,
|
2020-10-30 15:54:02 +00:00
|
|
|
rid: PropTypes.string,
|
2020-11-04 16:53:44 +00:00
|
|
|
blockUnauthenticatedAccess: PropTypes.bool,
|
|
|
|
serverVersion: PropTypes.string
|
2020-10-30 13:12:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Avatar.defaultProps = {
|
|
|
|
text: '',
|
|
|
|
size: 25,
|
|
|
|
type: 'd',
|
|
|
|
borderRadius: 4
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Avatar;
|