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';
|
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2021-09-13 20:41:05 +00:00
|
|
|
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';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { SubscriptionType } from '../../definitions';
|
|
|
|
import { IRoomInfoParam } from '../../views/SearchMessagesView';
|
2022-05-10 17:40:08 +00:00
|
|
|
import Edited from './Edited';
|
|
|
|
import Encrypted from './Encrypted';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2022-05-10 17:40:08 +00:00
|
|
|
actionIcons: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2021-09-13 20:41:05 +00:00
|
|
|
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;
|
2022-05-10 17:40:08 +00:00
|
|
|
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;
|
2022-04-01 21:52:38 +00:00
|
|
|
navToRoomInfo?: (navParam: IRoomInfoParam) => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
type: string;
|
2022-05-10 17:40:08 +00:00
|
|
|
isEdited: boolean;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const User = React.memo(
|
2022-05-10 17:40:08 +00:00
|
|
|
({ isHeader, useRealName, author, alias, ts, timeFormat, hasError, navToRoomInfo, type, isEdited, ...props }: IMessageUser) => {
|
2022-04-11 18:01:43 +00:00
|
|
|
const { user } = useContext(MessageContext);
|
|
|
|
const { theme } = useTheme();
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (isHeader || hasError) {
|
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?.({
|
2022-04-01 21:52:38 +00:00
|
|
|
t: SubscriptionType.DIRECT,
|
|
|
|
rid: author?._id || ''
|
2022-03-02 14:18:01 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
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}
|
2022-04-01 21:52:38 +00:00
|
|
|
// @ts-ignore // TODO - check this prop
|
2021-09-13 20:41:05 +00:00
|
|
|
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>
|
2022-05-15 18:29:07 +00:00
|
|
|
<Text style={[messageStyles.time, { color: themes[theme].auxiliaryTintColor, lineHeight: 22 }]}>{time}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
</TouchableOpacity>
|
2022-05-10 17:40:08 +00:00
|
|
|
<View style={styles.actionIcons}>
|
|
|
|
<Encrypted type={type} />
|
|
|
|
<Edited isEdited={isEdited} />
|
|
|
|
<MessageError hasError={hasError} {...props} />
|
|
|
|
</View>
|
2021-09-13 20:41:05 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
User.displayName = 'MessageUser';
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
export default User;
|