2023-11-08 18:03:53 +00:00
|
|
|
import moment from 'moment';
|
2021-09-13 20:41:05 +00:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
|
|
|
2023-11-08 18:03:53 +00:00
|
|
|
import { MessageType, MessageTypesValues, SubscriptionType } from '../../definitions';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2023-11-08 18:03:53 +00:00
|
|
|
import { IRoomInfoParam } from '../../views/SearchMessagesView';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2023-11-08 18:03:53 +00:00
|
|
|
import RightIcons from './Components/RightIcons';
|
2021-09-13 20:41:05 +00:00
|
|
|
import MessageContext from './Context';
|
2023-11-08 18:03:53 +00:00
|
|
|
import messageStyles from './styles';
|
2022-10-21 16:35:26 +00:00
|
|
|
import { messageHaveAuthorName } from './utils';
|
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,
|
2022-08-23 19:32:51 +00:00
|
|
|
...sharedStyles.textSemibold
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
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);
|
2023-11-08 18:03:53 +00:00
|
|
|
const { colors } = useTheme();
|
2022-04-11 18:01:43 +00:00
|
|
|
|
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;
|
2023-11-08 18:03:53 +00:00
|
|
|
const aliasUsername = alias ? <Text style={[styles.alias, { color: colors.auxiliaryText }]}> @{username}</Text> : null;
|
2021-09-13 20:41:05 +00:00
|
|
|
const time = moment(ts).format(timeFormat);
|
2023-11-08 18:03:53 +00:00
|
|
|
const itsMe = author?._id === user.id;
|
|
|
|
|
2022-03-02 14:18:01 +00:00
|
|
|
const onUserPress = () => {
|
|
|
|
navToRoomInfo?.({
|
2022-04-01 21:52:38 +00:00
|
|
|
t: SubscriptionType.DIRECT,
|
2023-11-08 18:03:53 +00:00
|
|
|
rid: author?._id || '',
|
|
|
|
itsMe
|
2022-03-02 14:18:01 +00:00
|
|
|
});
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const textContent = (
|
|
|
|
<>
|
|
|
|
{alias || username}
|
|
|
|
{aliasUsername}
|
|
|
|
</>
|
|
|
|
);
|
2023-11-08 18:03:53 +00:00
|
|
|
|
2022-10-21 16:35:26 +00:00
|
|
|
if (messageHaveAuthorName(type as MessageTypesValues)) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return (
|
2023-11-08 18:03:53 +00:00
|
|
|
<Text style={[styles.usernameInfoMessage, { color: colors.titleText }]} onPress={onUserPress}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{textContent}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
2024-02-15 21:07:48 +00:00
|
|
|
<TouchableOpacity testID={`username-header-${username}`} style={styles.titleContainer} onPress={onUserPress}>
|
2023-11-08 18:03:53 +00:00
|
|
|
<Text style={[styles.username, { color: colors.titleText }]} numberOfLines={1}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{textContent}
|
|
|
|
</Text>
|
2023-11-08 18:03:53 +00:00
|
|
|
<Text style={[messageStyles.time, { color: colors.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}
|
2022-10-18 18:25:15 +00:00
|
|
|
isReadReceiptEnabled={props.isReadReceiptEnabled}
|
|
|
|
unread={props.unread}
|
2022-05-27 17:27:43 +00:00
|
|
|
/>
|
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;
|