2021-09-13 20:41:05 +00:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import MessageError from './MessageError';
|
|
|
|
import sharedStyles from '../../views/Styles';
|
|
|
|
import messageStyles from './styles';
|
|
|
|
import MessageContext from './Context';
|
|
|
|
import { SYSTEM_MESSAGE_TYPES_WITH_AUTHOR_NAME } from './utils';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
username: {
|
|
|
|
fontSize: 16,
|
|
|
|
lineHeight: 22,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
usernameInfoMessage: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
titleContainer: {
|
|
|
|
flexShrink: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
alias: {
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IMessageUser {
|
2022-01-17 16:10:39 +00:00
|
|
|
isHeader?: boolean;
|
|
|
|
hasError?: boolean;
|
2022-03-02 14:18:01 +00:00
|
|
|
useRealName?: boolean;
|
2022-01-17 16:10:39 +00:00
|
|
|
author?: {
|
2021-09-13 20:41:05 +00:00
|
|
|
_id: string;
|
2022-01-17 16:10:39 +00:00
|
|
|
name?: string;
|
|
|
|
username?: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2022-01-17 16:10:39 +00:00
|
|
|
alias?: string;
|
|
|
|
ts?: Date;
|
|
|
|
timeFormat?: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
theme: string;
|
2022-03-02 14:18:01 +00:00
|
|
|
navToRoomInfo?: Function;
|
2021-09-13 20:41:05 +00:00
|
|
|
type: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const User = React.memo(
|
|
|
|
({ isHeader, useRealName, author, alias, ts, timeFormat, hasError, theme, navToRoomInfo, type, ...props }: IMessageUser) => {
|
|
|
|
if (isHeader || hasError) {
|
|
|
|
const { user } = useContext(MessageContext);
|
2022-03-02 14:18:01 +00:00
|
|
|
const username = (useRealName && author?.name) || author?.username;
|
2021-09-13 20:41:05 +00:00
|
|
|
const aliasUsername = alias ? (
|
|
|
|
<Text style={[styles.alias, { color: themes[theme].auxiliaryText }]}> @{username}</Text>
|
|
|
|
) : null;
|
|
|
|
const time = moment(ts).format(timeFormat);
|
2022-03-02 14:18:01 +00:00
|
|
|
const onUserPress = () => {
|
|
|
|
navToRoomInfo?.({
|
|
|
|
t: 'd',
|
|
|
|
rid: author?._id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const isDisabled = author?._id === user.id;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
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}
|
|
|
|
// @ts-ignore
|
|
|
|
disabled={isDisabled}>
|
|
|
|
{textContent}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<TouchableOpacity style={styles.titleContainer} onPress={onUserPress} disabled={isDisabled}>
|
|
|
|
<Text style={[styles.username, { color: themes[theme].titleText }]} numberOfLines={1}>
|
|
|
|
{textContent}
|
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
2022-03-21 20:44:06 +00:00
|
|
|
<Text style={[messageStyles.time, { color: themes[theme].auxiliaryTintColor }]}>{time}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
{hasError && <MessageError hasError={hasError} theme={theme} {...props} />}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
User.displayName = 'MessageUser';
|
|
|
|
|
|
|
|
export default withTheme(User);
|