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

213 lines
5.2 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import moment from 'moment';
2019-04-08 12:35:28 +00:00
import Touchable from 'react-native-platform-touchable';
import isEqual from 'deep-equal';
import Markdown from '../markdown';
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';
2019-11-25 20:01:17 +00:00
import { withSplit } from '../../split';
const styles = StyleSheet.create({
button: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
2019-03-29 19:36:07 +00:00
marginTop: 6,
2019-11-25 20:01:17 +00:00
alignSelf: 'flex-start',
2019-03-29 19:36:07 +00:00
borderWidth: 1,
borderRadius: 4
},
attachmentContainer: {
flex: 1,
borderRadius: 4,
flexDirection: 'column',
padding: 15
},
authorContainer: {
flex: 1,
flexDirection: 'row',
alignItems: 'center'
},
author: {
flex: 1,
fontSize: 16,
2019-03-29 19:36:07 +00:00
...sharedStyles.textMedium
},
time: {
fontSize: 12,
2019-03-29 19:36:07 +00:00
marginLeft: 10,
...sharedStyles.textRegular,
fontWeight: '300'
},
fieldsContainer: {
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row'
},
fieldContainer: {
flexDirection: 'column',
padding: 10
},
fieldTitle: {
2019-03-29 19:36:07 +00:00
fontSize: 14,
...sharedStyles.textSemibold
},
fieldValue: {
fontSize: 14,
...sharedStyles.textRegular
},
marginTop: {
marginTop: 4
}
});
2019-12-04 16:39:53 +00:00
const Title = React.memo(({ attachment, timeFormat, theme }) => {
if (!attachment.author_name) {
return null;
}
const time = attachment.ts ? moment(attachment.ts).format(timeFormat) : null;
return (
<View style={styles.authorContainer}>
2019-12-17 14:08:06 +00:00
{attachment.author_name ? <Text style={[styles.author, { color: themes[theme].bodyText }]}>{attachment.author_name}</Text> : null}
2019-12-04 16:39:53 +00:00
{time ? <Text style={[styles.time, { color: themes[theme].auxiliaryText }]}>{ time }</Text> : null}
</View>
);
2019-12-04 16:39:53 +00:00
});
const Description = React.memo(({
2019-12-04 16:39:53 +00:00
attachment, baseUrl, user, getCustomEmoji, useMarkdown, theme
}) => {
const text = attachment.text || attachment.title;
if (!text) {
return null;
}
return (
<Markdown
msg={text}
baseUrl={baseUrl}
username={user.username}
getCustomEmoji={getCustomEmoji}
useMarkdown={useMarkdown}
2019-12-04 16:39:53 +00:00
theme={theme}
/>
);
}, (prevProps, nextProps) => {
if (prevProps.attachment.text !== nextProps.attachment.text) {
return false;
}
if (prevProps.attachment.title !== nextProps.attachment.title) {
return false;
}
2019-12-04 16:39:53 +00:00
if (prevProps.theme !== nextProps.theme) {
return false;
}
return true;
});
2019-12-04 16:39:53 +00:00
const Fields = React.memo(({ attachment, theme }) => {
if (!attachment.fields) {
return null;
}
return (
<View style={styles.fieldsContainer}>
{attachment.fields.map(field => (
<View key={field.title} style={[styles.fieldContainer, { width: field.short ? '50%' : '100%' }]}>
2019-12-17 14:08:06 +00:00
<Text style={[styles.fieldTitle, { color: themes[theme].bodyText }]}>{field.title}</Text>
<Text style={[styles.fieldValue, { color: themes[theme].bodyText }]}>{field.value}</Text>
</View>
))}
</View>
);
2019-12-04 16:39:53 +00:00
}, (prevProps, nextProps) => isEqual(prevProps.attachment.fields, nextProps.attachment.fields) && prevProps.theme === nextProps.theme);
const Reply = React.memo(({
2019-12-04 16:39:53 +00:00
attachment, timeFormat, baseUrl, user, index, getCustomEmoji, useMarkdown, split, theme
}) => {
if (!attachment) {
return null;
}
const onPress = () => {
let url = attachment.title_link || attachment.author_link;
if (!url) {
return;
}
if (attachment.type === 'file') {
url = `${ baseUrl }${ url }?rc_uid=${ user.id }&rc_token=${ user.token }`;
}
2019-12-04 16:39:53 +00:00
openLink(url, theme);
};
return (
2019-04-08 12:35:28 +00:00
<Touchable
onPress={onPress}
2019-12-04 16:39:53 +00:00
style={[
styles.button,
index > 0 && styles.marginTop,
{
backgroundColor: themes[theme].chatComponentBackground,
borderColor: themes[theme].borderColor
},
split && sharedStyles.tabletContent
]}
background={Touchable.Ripple(themes[theme].bannerBackground)}
>
<View style={styles.attachmentContainer}>
2019-12-04 16:39:53 +00:00
<Title attachment={attachment} timeFormat={timeFormat} theme={theme} />
<Description
attachment={attachment}
timeFormat={timeFormat}
baseUrl={baseUrl}
user={user}
getCustomEmoji={getCustomEmoji}
useMarkdown={useMarkdown}
2019-12-04 16:39:53 +00:00
theme={theme}
/>
2019-12-04 16:39:53 +00:00
<Fields attachment={attachment} theme={theme} />
</View>
2019-04-08 12:35:28 +00:00
</Touchable>
);
2019-12-04 16:39:53 +00:00
}, (prevProps, nextProps) => isEqual(prevProps.attachment, nextProps.attachment) && prevProps.split === nextProps.split && prevProps.theme === nextProps.theme);
Reply.propTypes = {
attachment: PropTypes.object,
timeFormat: PropTypes.string,
baseUrl: PropTypes.string,
user: PropTypes.object,
index: PropTypes.number,
useMarkdown: PropTypes.bool,
2019-12-04 16:39:53 +00:00
theme: PropTypes.string,
2019-11-25 20:01:17 +00:00
getCustomEmoji: PropTypes.func,
split: PropTypes.bool
};
Reply.displayName = 'MessageReply';
Title.propTypes = {
attachment: PropTypes.object,
2019-12-04 16:39:53 +00:00
timeFormat: PropTypes.string,
theme: PropTypes.string
};
Title.displayName = 'MessageReplyTitle';
Description.propTypes = {
attachment: PropTypes.object,
baseUrl: PropTypes.string,
user: PropTypes.object,
useMarkdown: PropTypes.bool,
2019-12-04 16:39:53 +00:00
getCustomEmoji: PropTypes.func,
theme: PropTypes.string
};
Description.displayName = 'MessageReplyDescription';
Fields.propTypes = {
2019-12-04 16:39:53 +00:00
attachment: PropTypes.object,
theme: PropTypes.string
};
Fields.displayName = 'MessageReplyFields';
2019-11-25 20:01:17 +00:00
export default withSplit(Reply);