2020-03-30 19:20:50 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, Text, View } from 'react-native';
|
2020-03-30 19:20:50 +00:00
|
|
|
import { createImageProgress } from 'react-native-image-progress';
|
|
|
|
import * as Progress from 'react-native-progress';
|
2020-07-17 17:39:06 +00:00
|
|
|
import FastImage from '@rocket.chat/react-native-fast-image';
|
2020-03-30 19:20:50 +00:00
|
|
|
|
|
|
|
import sharedStyles from '../Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2020-03-30 19:20:50 +00:00
|
|
|
import { isTablet } from '../../utils/deviceInfo';
|
|
|
|
|
|
|
|
const ImageProgress = createImageProgress(FastImage);
|
|
|
|
|
|
|
|
const SIZE = 96;
|
|
|
|
const MARGIN_TOP = isTablet ? 0 : 64;
|
|
|
|
const BORDER_RADIUS = 6;
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
marginBottom: 16,
|
|
|
|
width: '100%',
|
|
|
|
height: SIZE + MARGIN_TOP,
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
image: {
|
|
|
|
width: SIZE,
|
|
|
|
height: SIZE,
|
|
|
|
borderRadius: BORDER_RADIUS
|
|
|
|
},
|
|
|
|
fallback: {
|
|
|
|
width: SIZE,
|
|
|
|
height: SIZE,
|
|
|
|
borderRadius: BORDER_RADIUS,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
initial: {
|
|
|
|
...sharedStyles.textBold,
|
|
|
|
fontSize: 42
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-05 20:48:04 +00:00
|
|
|
const getInitial = (url: string) => url && url.replace(/http(s?):\/\//, '').slice(0, 1);
|
2020-03-30 19:20:50 +00:00
|
|
|
|
2021-10-05 20:48:04 +00:00
|
|
|
interface IFallback {
|
|
|
|
theme: string;
|
|
|
|
initial: string;
|
|
|
|
}
|
|
|
|
const Fallback = ({ theme, initial }: IFallback) => (
|
2020-03-30 19:20:50 +00:00
|
|
|
<View style={[styles.container, styles.fallback, { backgroundColor: themes[theme].dangerColor }]}>
|
|
|
|
<Text style={[styles.initial, { color: themes[theme].buttonText }]}>{initial}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
2021-10-05 20:48:04 +00:00
|
|
|
interface IServerAvatar {
|
|
|
|
theme: string;
|
|
|
|
url: string;
|
|
|
|
image: string;
|
|
|
|
}
|
|
|
|
const ServerAvatar = React.memo(({ theme, url, image }: IServerAvatar) => (
|
2020-03-30 19:20:50 +00:00
|
|
|
<View style={styles.container}>
|
|
|
|
{image && (
|
|
|
|
<ImageProgress
|
|
|
|
style={[styles.image, { borderColor: themes[theme].borderColor }]}
|
2021-09-13 20:41:05 +00:00
|
|
|
source={{ uri: `${url}/${image}` }}
|
2020-03-30 19:20:50 +00:00
|
|
|
resizeMode={FastImage.resizeMode.cover}
|
|
|
|
indicator={Progress.Pie}
|
|
|
|
indicatorProps={{
|
|
|
|
color: themes[theme].actionTintColor
|
|
|
|
}}
|
|
|
|
renderError={() => <Fallback theme={theme} initial={getInitial(url)} />}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
));
|
|
|
|
|
|
|
|
ServerAvatar.displayName = 'ServerAvatar';
|
|
|
|
|
|
|
|
export default ServerAvatar;
|