2020-04-30 20:05:59 +00:00
|
|
|
import React, { useContext } from 'react';
|
2021-07-22 20:09:06 +00:00
|
|
|
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
|
2017-09-01 19:17:53 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
|
2019-07-17 14:06:39 +00:00
|
|
|
import MessageError from './MessageError';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2019-04-08 12:35:28 +00:00
|
|
|
import messageStyles from './styles';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2020-12-01 19:26:03 +00:00
|
|
|
import { SYSTEM_MESSAGE_TYPES_WITH_AUTHOR_NAME } from './utils';
|
2019-03-29 19:36:07 +00:00
|
|
|
|
2017-09-01 19:17:53 +00:00
|
|
|
const styles = StyleSheet.create({
|
2019-03-01 16:49:11 +00:00
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
2021-04-19 14:33:07 +00:00
|
|
|
justifyContent: 'space-between',
|
2019-03-29 19:36:07 +00:00
|
|
|
alignItems: 'center'
|
2019-03-01 16:49:11 +00:00
|
|
|
},
|
2017-09-01 19:17:53 +00:00
|
|
|
username: {
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 16,
|
2019-03-29 19:36:07 +00:00
|
|
|
lineHeight: 22,
|
|
|
|
...sharedStyles.textMedium
|
2017-09-01 19:17:53 +00:00
|
|
|
},
|
2020-12-01 19:26:03 +00:00
|
|
|
usernameInfoMessage: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
2019-03-01 16:49:11 +00:00
|
|
|
titleContainer: {
|
2021-04-19 14:33:07 +00:00
|
|
|
flexShrink: 1,
|
2017-09-01 19:17:53 +00:00
|
|
|
flexDirection: 'row',
|
2019-03-01 16:49:11 +00:00
|
|
|
alignItems: 'center'
|
2017-09-01 19:17:53 +00:00
|
|
|
},
|
|
|
|
alias: {
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 14,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textRegular
|
2017-09-01 19:17:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-22 20:09:06 +00:00
|
|
|
interface IMessageUser {
|
|
|
|
isHeader: boolean;
|
|
|
|
hasError: boolean;
|
|
|
|
useRealName: boolean;
|
|
|
|
author: {
|
|
|
|
_id: string;
|
|
|
|
name: string;
|
|
|
|
username: string;
|
|
|
|
};
|
|
|
|
alias: string;
|
|
|
|
ts: Date;
|
|
|
|
timeFormat: string;
|
|
|
|
theme: string;
|
|
|
|
navToRoomInfo: Function;
|
|
|
|
type: string;
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
const User = React.memo(({
|
2020-12-01 19:26:03 +00:00
|
|
|
isHeader, useRealName, author, alias, ts, timeFormat, hasError, theme, navToRoomInfo, type, ...props
|
2021-07-22 20:09:06 +00:00
|
|
|
}: IMessageUser) => {
|
2019-07-17 14:06:39 +00:00
|
|
|
if (isHeader || hasError) {
|
2020-03-17 16:14:54 +00:00
|
|
|
const navParam = {
|
|
|
|
t: 'd',
|
|
|
|
rid: author._id
|
|
|
|
};
|
2020-04-30 20:05:59 +00:00
|
|
|
const { user } = useContext(MessageContext);
|
2019-05-20 20:43:50 +00:00
|
|
|
const username = (useRealName && author.name) || author.username;
|
2019-12-04 16:39:53 +00:00
|
|
|
const aliasUsername = alias ? (<Text style={[styles.alias, { color: themes[theme].auxiliaryText }]}> @{username}</Text>) : null;
|
2018-09-25 19:28:42 +00:00
|
|
|
const time = moment(ts).format(timeFormat);
|
2020-12-01 19:26:03 +00:00
|
|
|
const onUserPress = () => navToRoomInfo(navParam);
|
|
|
|
const isDisabled = author._id === user.id;
|
|
|
|
|
|
|
|
const textContent = (
|
|
|
|
<>
|
|
|
|
{alias || username}
|
|
|
|
{aliasUsername}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (SYSTEM_MESSAGE_TYPES_WITH_AUTHOR_NAME.includes(type)) {
|
|
|
|
return (
|
|
|
|
<Text
|
|
|
|
style={[styles.usernameInfoMessage, { color: themes[theme].titleText }]}
|
|
|
|
onPress={onUserPress}
|
2021-07-22 20:09:06 +00:00
|
|
|
// @ts-ignore
|
2020-12-01 19:26:03 +00:00
|
|
|
disabled={isDisabled}
|
|
|
|
>
|
|
|
|
{textContent}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
2017-09-01 19:17:53 +00:00
|
|
|
|
|
|
|
return (
|
2019-03-01 16:49:11 +00:00
|
|
|
<View style={styles.container}>
|
2020-03-17 16:14:54 +00:00
|
|
|
<TouchableOpacity
|
2020-04-30 20:05:59 +00:00
|
|
|
style={styles.titleContainer}
|
2020-12-01 19:26:03 +00:00
|
|
|
onPress={onUserPress}
|
|
|
|
disabled={isDisabled}
|
2020-03-17 16:14:54 +00:00
|
|
|
>
|
2020-04-30 20:05:59 +00:00
|
|
|
<Text style={[styles.username, { color: themes[theme].titleText }]} numberOfLines={1}>
|
2020-12-01 19:26:03 +00:00
|
|
|
{textContent}
|
2020-04-30 20:05:59 +00:00
|
|
|
</Text>
|
2020-03-17 16:14:54 +00:00
|
|
|
</TouchableOpacity>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[messageStyles.time, { color: themes[theme].auxiliaryText }]}>{time}</Text>
|
|
|
|
{ hasError && <MessageError hasError={hasError} theme={theme} {...props} /> }
|
2017-09-01 19:17:53 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2019-05-20 20:43:50 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
User.displayName = 'MessageUser';
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default withTheme(User);
|