2020-03-03 21:10:39 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2019-04-18 20:57:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-10-29 13:53:58 +00:00
|
|
|
import { View, Text } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import I18n from '../../i18n';
|
2019-10-29 13:53:58 +00:00
|
|
|
import styles, { ROW_HEIGHT } from './styles';
|
2019-04-18 20:57:35 +00:00
|
|
|
import UnreadBadge from './UnreadBadge';
|
|
|
|
import TypeIcon from './TypeIcon';
|
|
|
|
import LastMessage from './LastMessage';
|
2019-07-16 14:30:29 +00:00
|
|
|
import { capitalize, formatDate } from '../../utils/room';
|
2019-10-29 13:53:58 +00:00
|
|
|
import Touchable from './Touchable';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
export { ROW_HEIGHT };
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const attrs = [
|
|
|
|
'name',
|
|
|
|
'unread',
|
|
|
|
'userMentions',
|
|
|
|
'showLastMessage',
|
2020-02-21 16:13:05 +00:00
|
|
|
'useRealName',
|
2019-09-16 20:26:32 +00:00
|
|
|
'alert',
|
|
|
|
'type',
|
|
|
|
'width',
|
|
|
|
'isRead',
|
|
|
|
'favorite',
|
2019-12-04 16:39:53 +00:00
|
|
|
'status',
|
2020-04-03 18:03:53 +00:00
|
|
|
'connected',
|
2019-12-04 16:39:53 +00:00
|
|
|
'theme'
|
2019-09-16 20:26:32 +00:00
|
|
|
];
|
2019-07-15 16:54:28 +00:00
|
|
|
|
2019-10-29 13:53:58 +00:00
|
|
|
const arePropsEqual = (oldProps, newProps) => {
|
|
|
|
const { _updatedAt: _updatedAtOld } = oldProps;
|
|
|
|
const { _updatedAt: _updatedAtNew } = newProps;
|
|
|
|
if (_updatedAtOld && _updatedAtNew && _updatedAtOld.toISOString() !== _updatedAtNew.toISOString()) {
|
|
|
|
return false;
|
2019-09-26 18:35:33 +00:00
|
|
|
}
|
2019-10-29 13:53:58 +00:00
|
|
|
return attrs.every(key => oldProps[key] === newProps[key]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RoomItem = React.memo(({
|
2020-04-03 18:03:53 +00:00
|
|
|
onPress, width, favorite, toggleFav, isRead, rid, toggleRead, hideChannel, testID, unread, userMentions, name, _updatedAt, alert, type, avatarSize, baseUrl, userId, username, token, id, prid, showLastMessage, hideUnreadStatus, lastMessage, status, avatar, useRealName, getUserPresence, isGroupChat, connected, theme
|
2019-10-29 13:53:58 +00:00
|
|
|
}) => {
|
2020-03-03 21:10:39 +00:00
|
|
|
useEffect(() => {
|
2020-04-03 18:03:53 +00:00
|
|
|
if (connected && type === 'd' && id) {
|
2020-04-01 12:28:54 +00:00
|
|
|
getUserPresence(id);
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
2020-04-03 18:03:53 +00:00
|
|
|
}, [connected]);
|
2020-03-03 21:10:39 +00:00
|
|
|
|
2020-04-06 20:23:13 +00:00
|
|
|
const date = lastMessage && formatDate(lastMessage.ts);
|
2019-10-29 13:53:58 +00:00
|
|
|
|
|
|
|
let accessibilityLabel = name;
|
|
|
|
if (unread === 1) {
|
|
|
|
accessibilityLabel += `, ${ unread } ${ I18n.t('alert') }`;
|
|
|
|
} else if (unread > 1) {
|
|
|
|
accessibilityLabel += `, ${ unread } ${ I18n.t('alerts') }`;
|
2019-05-22 20:15:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:53:58 +00:00
|
|
|
if (userMentions > 0) {
|
|
|
|
accessibilityLabel += `, ${ I18n.t('you_were_mentioned') }`;
|
2019-04-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:53:58 +00:00
|
|
|
if (date) {
|
|
|
|
accessibilityLabel += `, ${ I18n.t('last_message') } ${ date }`;
|
2019-07-01 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:53:58 +00:00
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
onPress={onPress}
|
|
|
|
width={width}
|
|
|
|
favorite={favorite}
|
|
|
|
toggleFav={toggleFav}
|
|
|
|
isRead={isRead}
|
|
|
|
rid={rid}
|
|
|
|
toggleRead={toggleRead}
|
|
|
|
hideChannel={hideChannel}
|
|
|
|
testID={testID}
|
|
|
|
type={type}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-10-29 13:53:58 +00:00
|
|
|
>
|
|
|
|
<View
|
|
|
|
style={styles.container}
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
2019-04-18 20:57:35 +00:00
|
|
|
>
|
2019-10-29 13:53:58 +00:00
|
|
|
<Avatar
|
|
|
|
text={avatar}
|
|
|
|
size={avatarSize}
|
|
|
|
type={type}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
style={styles.avatar}
|
|
|
|
userId={userId}
|
|
|
|
token={token}
|
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.centerContainer,
|
|
|
|
{
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
>
|
2019-10-29 13:53:58 +00:00
|
|
|
<View style={styles.titleContainer}>
|
|
|
|
<TypeIcon
|
|
|
|
type={type}
|
|
|
|
prid={prid}
|
|
|
|
status={status}
|
2020-04-01 12:28:54 +00:00
|
|
|
isGroupChat={isGroupChat}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-10-29 13:53:58 +00:00
|
|
|
/>
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.title,
|
2019-12-04 16:39:53 +00:00
|
|
|
alert && !hideUnreadStatus && styles.alert,
|
|
|
|
{ color: themes[theme].titleText }
|
2019-10-29 13:53:58 +00:00
|
|
|
]}
|
|
|
|
ellipsizeMode='tail'
|
|
|
|
numberOfLines={1}
|
2019-07-01 14:20:38 +00:00
|
|
|
>
|
2019-10-29 13:53:58 +00:00
|
|
|
{name}
|
|
|
|
</Text>
|
|
|
|
{_updatedAt ? (
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.date,
|
2019-12-04 16:39:53 +00:00
|
|
|
{
|
|
|
|
color:
|
|
|
|
themes[theme]
|
|
|
|
.auxiliaryText
|
|
|
|
},
|
|
|
|
alert && !hideUnreadStatus && [
|
|
|
|
styles.updateAlert,
|
|
|
|
{
|
|
|
|
color:
|
|
|
|
themes[theme]
|
|
|
|
.tintColor
|
|
|
|
}
|
|
|
|
]
|
2019-10-29 13:53:58 +00:00
|
|
|
]}
|
|
|
|
ellipsizeMode='tail'
|
|
|
|
numberOfLines={1}
|
2019-07-01 14:20:38 +00:00
|
|
|
>
|
2019-10-29 13:53:58 +00:00
|
|
|
{capitalize(date)}
|
|
|
|
</Text>
|
|
|
|
) : null}
|
|
|
|
</View>
|
|
|
|
<View style={styles.row}>
|
|
|
|
<LastMessage
|
|
|
|
lastMessage={lastMessage}
|
|
|
|
type={type}
|
|
|
|
showLastMessage={showLastMessage}
|
|
|
|
username={username}
|
|
|
|
alert={alert && !hideUnreadStatus}
|
2020-02-21 16:13:05 +00:00
|
|
|
useRealName={useRealName}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-10-29 13:53:58 +00:00
|
|
|
/>
|
|
|
|
<UnreadBadge
|
|
|
|
unread={unread}
|
|
|
|
userMentions={userMentions}
|
|
|
|
type={type}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-10-29 13:53:58 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
}, arePropsEqual);
|
|
|
|
|
|
|
|
RoomItem.propTypes = {
|
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
|
|
|
showLastMessage: PropTypes.bool,
|
|
|
|
_updatedAt: PropTypes.string,
|
|
|
|
lastMessage: PropTypes.object,
|
|
|
|
alert: PropTypes.bool,
|
|
|
|
unread: PropTypes.number,
|
|
|
|
userMentions: PropTypes.number,
|
|
|
|
id: PropTypes.string,
|
|
|
|
prid: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
|
|
|
userId: PropTypes.string,
|
|
|
|
username: PropTypes.string,
|
|
|
|
token: PropTypes.string,
|
|
|
|
avatarSize: PropTypes.number,
|
|
|
|
testID: PropTypes.string,
|
|
|
|
width: PropTypes.number,
|
|
|
|
favorite: PropTypes.bool,
|
|
|
|
isRead: PropTypes.bool,
|
|
|
|
rid: PropTypes.string,
|
|
|
|
status: PropTypes.string,
|
|
|
|
toggleFav: PropTypes.func,
|
|
|
|
toggleRead: PropTypes.func,
|
|
|
|
hideChannel: PropTypes.func,
|
|
|
|
avatar: PropTypes.bool,
|
2019-12-04 16:39:53 +00:00
|
|
|
hideUnreadStatus: PropTypes.bool,
|
2020-02-21 16:13:05 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2020-03-03 21:10:39 +00:00
|
|
|
getUserPresence: PropTypes.func,
|
2020-04-03 18:03:53 +00:00
|
|
|
connected: PropTypes.bool,
|
2020-04-01 12:28:54 +00:00
|
|
|
isGroupChat: PropTypes.bool,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string
|
2019-10-29 13:53:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RoomItem.defaultProps = {
|
2019-10-29 18:13:58 +00:00
|
|
|
avatarSize: 48,
|
2020-03-03 21:10:39 +00:00
|
|
|
status: 'offline',
|
|
|
|
getUserPresence: () => {}
|
2019-10-29 13:53:58 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
let status = 'offline';
|
|
|
|
const { id, type, visitor = {} } = ownProps;
|
|
|
|
if (state.meteor.connected) {
|
|
|
|
if (type === 'd') {
|
|
|
|
status = state.activeUsers[id]?.status || 'offline';
|
|
|
|
} else if (type === 'l' && visitor?.status) {
|
|
|
|
({ status } = visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
connected: state.meteor.connected,
|
|
|
|
status
|
|
|
|
};
|
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RoomItem);
|