Rocket.Chat.ReactNative/app/containers/message/Urls.js

154 lines
3.9 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import FastImage from 'react-native-fast-image';
2019-04-08 12:35:28 +00:00
import Touchable from 'react-native-platform-touchable';
2019-05-03 13:29:56 +00:00
import isEqual from 'lodash/isEqual';
import openLink from '../../utils/openLink';
2019-03-29 19:36:07 +00:00
import sharedStyles from '../../views/Styles';
2019-12-04 16:39:53 +00:00
import { themes } from '../../constants/colors';
import { withTheme } from '../../theme';
2019-11-25 20:01:17 +00:00
import { withSplit } from '../../split';
const styles = StyleSheet.create({
button: {
marginTop: 6
},
container: {
flex: 1,
flexDirection: 'column',
borderRadius: 4,
borderWidth: 1
},
textContainer: {
flex: 1,
flexDirection: 'column',
padding: 15,
justifyContent: 'flex-start',
alignItems: 'flex-start'
},
title: {
2019-03-29 19:36:07 +00:00
fontSize: 16,
...sharedStyles.textMedium
},
description: {
fontSize: 16,
2019-03-29 19:36:07 +00:00
...sharedStyles.textRegular
},
marginTop: {
marginTop: 4
},
image: {
width: '100%',
height: 150,
borderTopLeftRadius: 4,
borderTopRightRadius: 4
}
});
2019-05-03 13:29:56 +00:00
const UrlImage = React.memo(({ image, user, baseUrl }) => {
if (!image) {
return null;
}
image = image.includes('http') ? image : `${ baseUrl }/${ image }?rc_uid=${ user.id }&rc_token=${ user.token }`;
return <FastImage source={{ uri: image }} style={styles.image} resizeMode={FastImage.resizeMode.cover} />;
}, (prevProps, nextProps) => prevProps.image === nextProps.image);
2019-05-03 13:29:56 +00:00
2019-12-04 16:39:53 +00:00
const UrlContent = React.memo(({ title, description, theme }) => (
2019-05-03 13:29:56 +00:00
<View style={styles.textContainer}>
2019-12-04 16:39:53 +00:00
{title ? <Text style={[styles.title, { color: themes[theme].tintColor }]} numberOfLines={2}>{title}</Text> : null}
{description ? <Text style={[styles.description, { color: themes[theme].auxiliaryText }]} numberOfLines={2}>{description}</Text> : null}
2019-05-03 13:29:56 +00:00
</View>
), (prevProps, nextProps) => {
if (prevProps.title !== nextProps.title) {
return false;
}
if (prevProps.description !== nextProps.description) {
return false;
}
2019-12-04 16:39:53 +00:00
if (prevProps.theme !== nextProps.theme) {
return false;
}
return true;
});
2019-05-03 13:29:56 +00:00
const Url = React.memo(({
2019-12-04 16:39:53 +00:00
url, index, user, baseUrl, split, theme
2019-05-03 13:29:56 +00:00
}) => {
if (!url) {
return null;
}
2019-05-03 13:29:56 +00:00
2019-12-04 16:39:53 +00:00
const onPress = () => openLink(url.url, theme);
2019-05-03 13:29:56 +00:00
return (
2019-04-08 12:35:28 +00:00
<Touchable
2019-05-03 13:29:56 +00:00
onPress={onPress}
2019-12-04 16:39:53 +00:00
style={[
styles.button,
index > 0 && styles.marginTop,
styles.container,
{
backgroundColor: themes[theme].chatComponentBackground,
borderColor: themes[theme].borderColor
},
split && sharedStyles.tabletContent
]}
background={Touchable.Ripple(themes[theme].bannerBackground)}
>
<>
2019-05-03 13:29:56 +00:00
<UrlImage image={url.image} user={user} baseUrl={baseUrl} />
2019-12-04 16:39:53 +00:00
<UrlContent title={url.title} description={url.description} theme={theme} />
</>
2019-04-08 12:35:28 +00:00
</Touchable>
);
2019-12-04 16:39:53 +00:00
}, (oldProps, newProps) => isEqual(oldProps.url, newProps.url) && oldProps.split === newProps.split && oldProps.theme === newProps.theme);
2019-05-03 13:29:56 +00:00
2019-11-25 20:01:17 +00:00
const Urls = React.memo(({
2019-12-04 16:39:53 +00:00
urls, user, baseUrl, split, theme
2019-11-25 20:01:17 +00:00
}) => {
if (!urls || urls.length === 0) {
return null;
}
return urls.map((url, index) => (
2019-12-04 16:39:53 +00:00
<Url url={url} key={url.url} index={index} user={user} baseUrl={baseUrl} split={split} theme={theme} />
));
2019-12-04 16:39:53 +00:00
}, (oldProps, newProps) => isEqual(oldProps.urls, newProps.urls) && oldProps.split === newProps.split && oldProps.theme === newProps.theme);
2019-05-03 13:29:56 +00:00
UrlImage.propTypes = {
image: PropTypes.string,
user: PropTypes.object,
baseUrl: PropTypes.string
};
UrlImage.displayName = 'MessageUrlImage';
2019-05-03 13:29:56 +00:00
UrlContent.propTypes = {
title: PropTypes.string,
2019-12-04 16:39:53 +00:00
description: PropTypes.string,
theme: PropTypes.string
};
UrlContent.displayName = 'MessageUrlContent';
Url.propTypes = {
url: PropTypes.object.isRequired,
2019-05-03 13:29:56 +00:00
index: PropTypes.number,
user: PropTypes.object,
2019-11-25 20:01:17 +00:00
baseUrl: PropTypes.string,
2019-12-04 16:39:53 +00:00
theme: PropTypes.string,
2019-11-25 20:01:17 +00:00
split: PropTypes.bool
};
Url.displayName = 'MessageUrl';
Urls.propTypes = {
urls: PropTypes.array,
user: PropTypes.object,
2019-11-25 20:01:17 +00:00
baseUrl: PropTypes.string,
2019-12-04 16:39:53 +00:00
theme: PropTypes.string,
2019-11-25 20:01:17 +00:00
split: PropTypes.bool
};
Urls.displayName = 'MessageUrls';
2019-12-04 16:39:53 +00:00
export default withTheme(withSplit(Urls));