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 sharedStyles from '../../views/Styles';
|
|
|
|
import messageStyles from './styles';
|
|
|
|
import MessageContext from './Context';
|
|
|
|
import { SYSTEM_MESSAGE_TYPES_WITH_AUTHOR_NAME } from './utils';
|
2022-05-27 17:27:43 +00:00
|
|
|
import { MessageType, SubscriptionType } from '../../definitions';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { IRoomInfoParam } from '../../views/SearchMessagesView';
|
2022-05-27 17:27:43 +00:00
|
|
|
import RightIcons from './Components/RightIcons';
|
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: {
|
2022-05-20 17:56:07 +00:00
|
|
|
flexShrink: 1,
|
2021-09-13 20:41:05 +00:00
|
|
|
fontSize: 16,
|
|
|
|
lineHeight: 22,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
usernameInfoMessage: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
titleContainer: {
|
2022-05-27 17:27:43 +00:00
|
|
|
flexShrink: 1,
|
2021-09-13 20:41:05 +00:00
|
|
|
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;
|
2022-05-27 17:27:43 +00:00
|
|
|
type: MessageType;
|
2022-05-10 17:40:08 +00:00
|
|
|
isEdited: boolean;
|
2022-05-27 17:27:43 +00:00
|
|
|
isReadReceiptEnabled?: boolean;
|
|
|
|
unread?: 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();
|
|
|
|
|
2022-05-27 17:27:43 +00:00
|
|
|
if (isHeader) {
|
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
|
2022-08-08 21:02:08 +00:00
|
|
|
disabled={isDisabled}
|
|
|
|
>
|
2021-09-13 20:41:05 +00:00
|
|
|
{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-20 17:56:07 +00:00
|
|
|
<Text style={[messageStyles.time, { color: themes[theme].auxiliaryText }]}>{time}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
</TouchableOpacity>
|
2022-05-27 17:27:43 +00:00
|
|
|
<RightIcons
|
|
|
|
type={type}
|
|
|
|
isEdited={isEdited}
|
|
|
|
hasError={hasError}
|
|
|
|
isReadReceiptEnabled={props.isReadReceiptEnabled || false}
|
|
|
|
unread={props.unread || false}
|
|
|
|
/>
|
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;
|