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,
|
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-06-13 01:33:00 +00:00
|
|
|
children: PropTypes.object,
|
|
|
|
forceInitials: PropTypes.bool
|
2018-02-08 14:08:50 +00:00
|
|
|
};
|
2018-06-13 01:33:00 +00:00
|
|
|
static defaultProps = {
|
|
|
|
text: '',
|
|
|
|
size: 25,
|
|
|
|
type: 'd',
|
2018-08-31 16:46:33 +00:00
|
|
|
borderRadius: 4,
|
2018-06-13 01:33:00 +00:00
|
|
|
forceInitials: false
|
|
|
|
};
|
2018-06-20 13:40:33 +00:00
|
|
|
state = { showInitials: true };
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2018-06-20 13:40:33 +00:00
|
|
|
// componentDidMount() {
|
|
|
|
// const { text, type } = this.props;
|
|
|
|
// if (type === 'd') {
|
|
|
|
// this.users = this.userQuery(text);
|
|
|
|
// this.users.addListener(this.update);
|
|
|
|
// this.update();
|
|
|
|
// }
|
|
|
|
// }
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2018-06-20 13:40:33 +00:00
|
|
|
// componentWillReceiveProps(nextProps) {
|
|
|
|
// if (nextProps.text !== this.props.text && nextProps.type === 'd') {
|
|
|
|
// if (this.users) {
|
|
|
|
// this.users.removeAllListeners();
|
|
|
|
// }
|
|
|
|
// this.users = this.userQuery(nextProps.text);
|
|
|
|
// this.users.addListener(this.update);
|
|
|
|
// this.update();
|
|
|
|
// }
|
|
|
|
// }
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2018-06-20 13:40:33 +00:00
|
|
|
// componentWillUnmount() {
|
|
|
|
// if (this.users) {
|
|
|
|
// this.users.removeAllListeners();
|
|
|
|
// }
|
|
|
|
// }
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2018-06-20 13:40:33 +00:00
|
|
|
// get avatarVersion() {
|
|
|
|
// // return (this.state.user && this.state.user.avatarVersion) || 0;
|
|
|
|
// return 0;
|
|
|
|
// }
|
2018-06-13 01:33:00 +00:00
|
|
|
|
|
|
|
/** FIXME: Workaround
|
|
|
|
* While we don't have containers/components structure, this is breaking tests.
|
|
|
|
* In that case, avatar would be a component, it would receive an `avatarVersion` param
|
|
|
|
* and we would have a avatar container in charge of making queries.
|
|
|
|
* Also, it would make possible to write unit tests like these.
|
|
|
|
*/
|
2018-06-20 13:40:33 +00:00
|
|
|
// userQuery = (username) => {
|
|
|
|
// if (database && database.databases && database.databases.activeDB) {
|
|
|
|
// return database.objects('users').filtered('username = $0', username);
|
|
|
|
// }
|
|
|
|
// return {
|
|
|
|
// addListener: () => {},
|
|
|
|
// removeAllListeners: () => {}
|
|
|
|
// };
|
|
|
|
// }
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2018-06-20 13:40:33 +00:00
|
|
|
// update = () => {
|
|
|
|
// if (this.users.length) {
|
|
|
|
// this.setState({ user: this.users[0] });
|
|
|
|
// }
|
|
|
|
// }
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2017-09-01 19:17:53 +00:00
|
|
|
render() {
|
2017-11-13 12:58:35 +00:00
|
|
|
const {
|
2018-06-13 01:33:00 +00:00
|
|
|
text, size, baseUrl, borderRadius, style, avatar, type, forceInitials
|
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;
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
if (type === 'd' && !forceInitials) {
|
2018-06-20 13:40:33 +00:00
|
|
|
const uri = avatar || `${ baseUrl }/avatar/${ text }`;
|
2018-06-13 01:33:00 +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
|
|
|
/>
|
2018-06-13 01:33:00 +00:00
|
|
|
) : null;
|
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-06-13 01:33:00 +00:00
|
|
|
{this.state.showInitials ?
|
2018-05-18 16:41:47 +00:00
|
|
|
<Text
|
|
|
|
style={[styles.avatarInitials, avatarInitialsStyle]}
|
|
|
|
allowFontScaling={false}
|
|
|
|
>
|
|
|
|
{initials}
|
|
|
|
</Text>
|
2018-06-13 01:33:00 +00:00
|
|
|
: null
|
2018-05-18 16:41:47 +00:00
|
|
|
}
|
|
|
|
{image}
|
|
|
|
{this.props.children}
|
2017-12-08 19:13:21 +00:00
|
|
|
</View>
|
|
|
|
);
|
2017-09-01 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|