2019-04-18 20:57:35 +00:00
|
|
|
import React from 'react';
|
|
|
|
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',
|
|
|
|
'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-02-21 16:13:05 +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, theme
|
2019-10-29 13:53:58 +00:00
|
|
|
}) => {
|
|
|
|
const date = formatDate(_updatedAt);
|
|
|
|
|
|
|
|
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}
|
|
|
|
id={id}
|
|
|
|
prid={prid}
|
|
|
|
status={status}
|
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,
|
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,
|
|
|
|
status: 'offline'
|
2019-10-29 13:53:58 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
2019-12-04 16:39:53 +00:00
|
|
|
status:
|
|
|
|
state.meteor.connected && ownProps.type === 'd'
|
|
|
|
? state.activeUsers[ownProps.id]
|
|
|
|
: 'offline'
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RoomItem);
|