2019-04-18 20:57:35 +00:00
|
|
|
import React from 'react';
|
2017-09-01 19:17:53 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-05-22 20:15:35 +00:00
|
|
|
import { View } from 'react-native';
|
2018-05-07 20:41:36 +00:00
|
|
|
import FastImage from 'react-native-fast-image';
|
2020-04-30 20:05:59 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
2020-02-20 12:58:13 +00:00
|
|
|
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
|
2020-04-30 20:05:59 +00:00
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
import { avatarURL } from '../utils/avatar';
|
2020-05-08 17:16:22 +00:00
|
|
|
import Emoji from './markdown/Emoji';
|
2019-07-05 14:46:37 +00:00
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
const Avatar = React.memo(({
|
2020-05-08 17:16:22 +00:00
|
|
|
text, size, baseUrl, borderRadius, style, avatar, type, children, userId, token, onPress, theme, emoji, getCustomEmoji
|
2019-04-18 20:57:35 +00:00
|
|
|
}) => {
|
|
|
|
const avatarStyle = {
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
borderRadius
|
|
|
|
};
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
if (!text && !avatar) {
|
|
|
|
return null;
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
const uri = avatarURL({
|
|
|
|
type, text, size, userId, token, avatar, baseUrl
|
|
|
|
});
|
2019-02-07 19:58:20 +00:00
|
|
|
|
2020-05-08 17:16:22 +00:00
|
|
|
let image = emoji ? (
|
|
|
|
<Emoji
|
|
|
|
theme={theme}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
isMessageContainsOnlyEmoji
|
|
|
|
literal={emoji}
|
|
|
|
/>
|
|
|
|
) : (
|
2019-04-18 20:57:35 +00:00
|
|
|
<FastImage
|
|
|
|
style={avatarStyle}
|
|
|
|
source={{
|
|
|
|
uri,
|
2020-02-20 12:58:13 +00:00
|
|
|
headers: RocketChatSettings.customHeaders,
|
2019-04-18 20:57:35 +00:00
|
|
|
priority: FastImage.priority.high
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2019-02-07 19:58:20 +00:00
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
if (onPress) {
|
|
|
|
image = (
|
2020-04-30 20:05:59 +00:00
|
|
|
<Touchable onPress={onPress}>
|
2019-11-25 20:01:17 +00:00
|
|
|
{image}
|
2020-04-30 20:05:59 +00:00
|
|
|
</Touchable>
|
2019-11-25 20:01:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
return (
|
|
|
|
<View style={[avatarStyle, style]}>
|
|
|
|
{image}
|
|
|
|
{children}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
2019-02-07 19:58:20 +00:00
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
Avatar.propTypes = {
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2019-05-22 20:15:35 +00:00
|
|
|
style: PropTypes.any,
|
2019-04-18 20:57:35 +00:00
|
|
|
text: PropTypes.string,
|
|
|
|
avatar: PropTypes.string,
|
2020-05-08 17:16:22 +00:00
|
|
|
emoji: PropTypes.string,
|
2019-04-18 20:57:35 +00:00
|
|
|
size: PropTypes.number,
|
|
|
|
borderRadius: PropTypes.number,
|
|
|
|
type: PropTypes.string,
|
|
|
|
children: PropTypes.object,
|
|
|
|
userId: PropTypes.string,
|
2019-11-25 20:01:17 +00:00
|
|
|
token: PropTypes.string,
|
2020-05-08 17:16:22 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
|
|
|
getCustomEmoji: PropTypes.func
|
2019-04-18 20:57:35 +00:00
|
|
|
};
|
2018-12-05 20:52:08 +00:00
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
Avatar.defaultProps = {
|
|
|
|
text: '',
|
|
|
|
size: 25,
|
|
|
|
type: 'd',
|
|
|
|
borderRadius: 4
|
|
|
|
};
|
2017-12-08 19:13:21 +00:00
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
export default Avatar;
|