2020-04-30 20:05:59 +00:00
|
|
|
import React, { useContext } from 'react';
|
2020-02-14 13:39:42 +00:00
|
|
|
import {
|
|
|
|
View, Text, StyleSheet, Clipboard
|
|
|
|
} from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-07-17 17:39:06 +00:00
|
|
|
import FastImage from '@rocket.chat/react-native-fast-image';
|
2019-05-03 13:29:56 +00:00
|
|
|
import isEqual from 'lodash/isEqual';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2020-04-30 20:05:59 +00:00
|
|
|
import Touchable from './Touchable';
|
2017-12-28 17:40:10 +00:00
|
|
|
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';
|
2020-02-14 13:39:42 +00:00
|
|
|
import { LISTENER } from '../Toast';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import I18n from '../../i18n';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
2019-03-01 16:49:11 +00:00
|
|
|
marginTop: 6
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
2018-09-11 16:32:52 +00:00
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
borderRadius: 4,
|
2019-03-01 16:49:11 +00:00
|
|
|
borderWidth: 1
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
textContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
2018-09-11 16:32:52 +00:00
|
|
|
padding: 15,
|
2017-12-02 13:19:58 +00:00
|
|
|
justifyContent: 'flex-start',
|
|
|
|
alignItems: 'flex-start'
|
|
|
|
},
|
|
|
|
title: {
|
2019-03-29 19:36:07 +00:00
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
description: {
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 16,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textRegular
|
2018-09-11 16:32:52 +00:00
|
|
|
},
|
|
|
|
marginTop: {
|
|
|
|
marginTop: 4
|
|
|
|
},
|
|
|
|
image: {
|
|
|
|
width: '100%',
|
|
|
|
height: 150,
|
|
|
|
borderTopLeftRadius: 4,
|
|
|
|
borderTopRightRadius: 4
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-30 20:05:59 +00:00
|
|
|
const UrlImage = React.memo(({ image }) => {
|
2019-05-03 13:29:56 +00:00
|
|
|
if (!image) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-04-30 20:05:59 +00:00
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
2019-05-03 13:29:56 +00:00
|
|
|
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} />;
|
2019-05-20 20:43:50 +00:00
|
|
|
}, (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>
|
2019-05-20 20:43:50 +00:00
|
|
|
), (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;
|
|
|
|
}
|
2019-05-20 20:43:50 +00:00
|
|
|
return true;
|
|
|
|
});
|
2019-05-03 13:29:56 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
const Url = React.memo(({ url, index, theme }) => {
|
2017-12-02 13:19:58 +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
|
|
|
|
2020-02-14 13:39:42 +00:00
|
|
|
const onLongPress = () => {
|
|
|
|
Clipboard.setString(url.url);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
|
|
|
|
};
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
return (
|
2019-04-08 12:35:28 +00:00
|
|
|
<Touchable
|
2019-05-03 13:29:56 +00:00
|
|
|
onPress={onPress}
|
2020-02-14 13:39:42 +00:00
|
|
|
onLongPress={onLongPress}
|
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
|
2020-06-15 14:00:46 +00:00
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
]}
|
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
2018-09-19 14:18:32 +00:00
|
|
|
>
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2020-04-30 20:05:59 +00:00
|
|
|
<UrlImage image={url.image} />
|
2019-12-04 16:39:53 +00:00
|
|
|
<UrlContent title={url.title} description={url.description} theme={theme} />
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2019-04-08 12:35:28 +00:00
|
|
|
</Touchable>
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
2020-06-15 14:00:46 +00:00
|
|
|
}, (oldProps, newProps) => isEqual(oldProps.url, newProps.url) && oldProps.theme === newProps.theme);
|
2019-05-03 13:29:56 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
const Urls = React.memo(({ urls, theme }) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
if (!urls || urls.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls.map((url, index) => (
|
2020-06-15 14:00:46 +00:00
|
|
|
<Url url={url} key={url.url} index={index} theme={theme} />
|
2019-05-20 20:43:50 +00:00
|
|
|
));
|
2020-06-15 14:00:46 +00:00
|
|
|
}, (oldProps, newProps) => isEqual(oldProps.urls, newProps.urls) && oldProps.theme === newProps.theme);
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2019-05-03 13:29:56 +00:00
|
|
|
UrlImage.propTypes = {
|
2020-04-30 20:05:59 +00:00
|
|
|
image: PropTypes.string
|
2019-05-03 13:29:56 +00:00
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
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
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
UrlContent.displayName = 'MessageUrlContent';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
Url.propTypes = {
|
2018-09-11 16:32:52 +00:00
|
|
|
url: PropTypes.object.isRequired,
|
2019-05-03 13:29:56 +00:00
|
|
|
index: PropTypes.number,
|
2020-06-15 14:00:46 +00:00
|
|
|
theme: PropTypes.string
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
Url.displayName = 'MessageUrl';
|
|
|
|
|
|
|
|
Urls.propTypes = {
|
|
|
|
urls: PropTypes.array,
|
2020-06-15 14:00:46 +00:00
|
|
|
theme: PropTypes.string
|
2019-05-20 20:43:50 +00:00
|
|
|
};
|
|
|
|
Urls.displayName = 'MessageUrls';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
export default withTheme(Urls);
|