2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
2017-12-28 17:40:10 +00:00
|
|
|
import { View, Text, TouchableOpacity, 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';
|
|
|
|
import QuoteMark from './QuoteMark';
|
|
|
|
import Avatar from '../Avatar';
|
2017-12-28 17:40:10 +00:00
|
|
|
import openLink from '../../utils/openLink';
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginTop: 2,
|
|
|
|
alignSelf: 'flex-end'
|
|
|
|
},
|
|
|
|
attachmentContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column'
|
|
|
|
},
|
|
|
|
authorContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2017-12-04 15:49:15 +00:00
|
|
|
author: {
|
|
|
|
fontWeight: 'bold',
|
|
|
|
marginHorizontal: 5,
|
|
|
|
flex: 1
|
|
|
|
},
|
2017-12-02 13:19:58 +00:00
|
|
|
time: {
|
|
|
|
fontSize: 10,
|
|
|
|
fontWeight: 'normal',
|
|
|
|
color: '#888',
|
|
|
|
marginLeft: 5
|
|
|
|
},
|
|
|
|
fieldsContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
|
|
|
fieldContainer: {
|
|
|
|
flexDirection: 'column',
|
|
|
|
padding: 10
|
|
|
|
},
|
|
|
|
fieldTitle: {
|
|
|
|
fontWeight: 'bold'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const Reply = ({ attachment, timeFormat }) => {
|
|
|
|
if (!attachment) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderAvatar = () => {
|
|
|
|
if (!attachment.author_icon && !attachment.author_name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Avatar
|
|
|
|
text={attachment.author_name}
|
|
|
|
size={16}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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}>
|
|
|
|
{renderAvatar()}
|
|
|
|
{renderAuthor()}
|
|
|
|
{renderTime()}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const renderText = () => (
|
2018-05-07 20:41:36 +00:00
|
|
|
attachment.text ? <Markdown msg={attachment.text} /> : 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 (
|
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => onPress(attachment)}
|
|
|
|
style={styles.button}
|
|
|
|
>
|
|
|
|
<QuoteMark color={attachment.color} />
|
|
|
|
<View style={styles.attachmentContainer}>
|
2017-12-04 15:49:15 +00:00
|
|
|
{renderTitle()}
|
2017-12-02 13:19:58 +00:00
|
|
|
{renderText()}
|
|
|
|
{renderFields()}
|
2018-06-13 01:33:00 +00:00
|
|
|
{attachment.attachments ?
|
|
|
|
attachment.attachments
|
|
|
|
.map(attach => <Reply key={attach.text} attachment={attach} timeFormat={timeFormat} />)
|
|
|
|
: null
|
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Reply.propTypes = {
|
|
|
|
attachment: PropTypes.object.isRequired,
|
|
|
|
timeFormat: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Reply;
|