2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
2018-09-11 16:32:52 +00:00
|
|
|
import { View, Text, StyleSheet } from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import Markdown from './Markdown';
|
2017-12-28 17:40:10 +00:00
|
|
|
import openLink from '../../utils/openLink';
|
2018-09-11 16:32:52 +00:00
|
|
|
import Touch from '../../utils/touch';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
2018-09-11 16:32:52 +00:00
|
|
|
marginTop: 15,
|
2017-12-02 13:19:58 +00:00
|
|
|
alignSelf: 'flex-end'
|
|
|
|
},
|
|
|
|
attachmentContainer: {
|
|
|
|
flex: 1,
|
2018-09-11 16:32:52 +00:00
|
|
|
borderRadius: 4,
|
|
|
|
flexDirection: 'column',
|
|
|
|
backgroundColor: '#f3f4f5',
|
|
|
|
padding: 15
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
authorContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2017-12-04 15:49:15 +00:00
|
|
|
author: {
|
2018-09-11 16:32:52 +00:00
|
|
|
color: '#1d74f5',
|
|
|
|
fontSize: 18,
|
|
|
|
fontWeight: '500',
|
|
|
|
marginRight: 10
|
2017-12-04 15:49:15 +00:00
|
|
|
},
|
2017-12-02 13:19:58 +00:00
|
|
|
time: {
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 14,
|
2017-12-02 13:19:58 +00:00
|
|
|
fontWeight: 'normal',
|
2018-09-11 16:32:52 +00:00
|
|
|
color: '#9ea2a8',
|
2017-12-02 13:19:58 +00:00
|
|
|
marginLeft: 5
|
|
|
|
},
|
|
|
|
fieldsContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
|
|
|
fieldContainer: {
|
|
|
|
flexDirection: 'column',
|
|
|
|
padding: 10
|
|
|
|
},
|
|
|
|
fieldTitle: {
|
|
|
|
fontWeight: 'bold'
|
2018-09-11 16:32:52 +00:00
|
|
|
},
|
|
|
|
marginTop: {
|
|
|
|
marginTop: 4
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const onPress = (attachment) => {
|
|
|
|
const url = attachment.title_link || attachment.author_link;
|
|
|
|
if (!url) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-28 17:40:10 +00:00
|
|
|
openLink(attachment.title_link || attachment.author_link);
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
2017-12-04 15:49:15 +00:00
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
const Reply = ({
|
|
|
|
attachment, timeFormat, baseUrl, customEmojis, user, index
|
|
|
|
}) => {
|
2017-12-02 13:19:58 +00:00
|
|
|
if (!attachment) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderAuthor = () => (
|
2017-12-04 15:49:15 +00:00
|
|
|
attachment.author_name ? <Text style={styles.author}>{attachment.author_name}</Text> : null
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const renderTime = () => {
|
|
|
|
const time = attachment.ts ? moment(attachment.ts).format(timeFormat) : null;
|
|
|
|
return time ? <Text style={styles.time}>{ time }</Text> : null;
|
|
|
|
};
|
|
|
|
|
2017-12-04 15:49:15 +00:00
|
|
|
const renderTitle = () => {
|
|
|
|
if (!(attachment.author_icon || attachment.author_name || attachment.ts)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.authorContainer}>
|
|
|
|
{renderAuthor()}
|
|
|
|
{renderTime()}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const renderText = () => (
|
2018-09-11 16:32:52 +00:00
|
|
|
attachment.text ? <Markdown msg={attachment.text} customEmojis={customEmojis} baseUrl={baseUrl} username={user.username} /> : null
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const renderFields = () => {
|
|
|
|
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%' }]}>
|
|
|
|
<Text style={styles.fieldTitle}>{field.title}</Text>
|
|
|
|
<Text>{field.value}</Text>
|
|
|
|
</View>
|
|
|
|
))}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2018-09-11 16:32:52 +00:00
|
|
|
<Touch
|
2017-12-02 13:19:58 +00:00
|
|
|
onPress={() => onPress(attachment)}
|
2018-09-11 16:32:52 +00:00
|
|
|
style={[styles.button, index > 0 && styles.marginTop]}
|
2017-12-02 13:19:58 +00:00
|
|
|
>
|
|
|
|
<View style={styles.attachmentContainer}>
|
2017-12-04 15:49:15 +00:00
|
|
|
{renderTitle()}
|
2017-12-02 13:19:58 +00:00
|
|
|
{renderText()}
|
|
|
|
{renderFields()}
|
|
|
|
</View>
|
2018-09-11 16:32:52 +00:00
|
|
|
</Touch>
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Reply.propTypes = {
|
|
|
|
attachment: PropTypes.object.isRequired,
|
2018-09-11 16:32:52 +00:00
|
|
|
timeFormat: PropTypes.string.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
|
|
|
customEmojis: PropTypes.object.isRequired,
|
|
|
|
user: PropTypes.object.isRequired,
|
|
|
|
index: PropTypes.number
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Reply;
|