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';
|
2018-09-19 14:18:32 +00:00
|
|
|
import { RectButton } from 'react-native-gesture-handler';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
import Markdown from './Markdown';
|
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',
|
2018-09-11 16:32:52 +00:00
|
|
|
marginTop: 15,
|
2018-09-19 14:18:32 +00:00
|
|
|
alignSelf: 'flex-end',
|
|
|
|
backgroundColor: '#f3f4f5'
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
attachmentContainer: {
|
|
|
|
flex: 1,
|
2018-09-11 16:32:52 +00:00
|
|
|
borderRadius: 4,
|
|
|
|
flexDirection: 'column',
|
|
|
|
padding: 15
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
authorContainer: {
|
2019-02-08 16:34:50 +00:00
|
|
|
flex: 1,
|
2017-12-02 13:19:58 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2017-12-04 15:49:15 +00:00
|
|
|
author: {
|
2019-02-08 16:34:50 +00:00
|
|
|
flex: 1,
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
const onPress = (attachment, baseUrl, user) => {
|
|
|
|
let url = attachment.title_link || attachment.author_link;
|
2017-12-02 13:19:58 +00:00
|
|
|
if (!url) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-19 14:18:32 +00:00
|
|
|
if (attachment.type === 'file') {
|
|
|
|
url = `${ baseUrl }${ url }?rc_uid=${ user.id }&rc_token=${ user.token }`;
|
|
|
|
}
|
|
|
|
openLink(url);
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
const renderText = () => {
|
|
|
|
const text = attachment.text || attachment.title;
|
|
|
|
if (text) {
|
|
|
|
return (
|
|
|
|
<Markdown
|
|
|
|
msg={text}
|
|
|
|
customEmojis={customEmojis}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
username={user.username}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
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-19 14:18:32 +00:00
|
|
|
<RectButton
|
|
|
|
onPress={() => onPress(attachment, baseUrl, user)}
|
2018-09-11 16:32:52 +00:00
|
|
|
style={[styles.button, index > 0 && styles.marginTop]}
|
2018-09-19 14:18:32 +00:00
|
|
|
activeOpacity={0.5}
|
|
|
|
underlayColor='#fff'
|
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-19 14:18:32 +00:00
|
|
|
</RectButton>
|
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;
|