2020-10-30 13:12:02 +00:00
|
|
|
import React from 'react';
|
2019-04-18 20:57:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
import I18n from '../../i18n';
|
2020-07-31 17:06:22 +00:00
|
|
|
import { ROW_HEIGHT } from './styles';
|
|
|
|
import { formatDate } from '../../utils/room';
|
2020-10-30 13:12:02 +00:00
|
|
|
import database from '../../lib/database';
|
2020-07-31 17:06:22 +00:00
|
|
|
import RoomItem from './RoomItem';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
export { ROW_HEIGHT };
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const attrs = [
|
|
|
|
'width',
|
2019-12-04 16:39:53 +00:00
|
|
|
'status',
|
2020-04-03 18:03:53 +00:00
|
|
|
'connected',
|
2020-06-15 14:00:46 +00:00
|
|
|
'theme',
|
2020-07-20 16:44:54 +00:00
|
|
|
'isFocused',
|
|
|
|
'forceUpdate',
|
|
|
|
'showLastMessage'
|
2019-09-16 20:26:32 +00:00
|
|
|
];
|
2019-07-15 16:54:28 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
class RoomItemContainer extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
item: PropTypes.object.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
|
|
|
showLastMessage: PropTypes.bool,
|
|
|
|
id: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
|
|
|
userId: PropTypes.string,
|
|
|
|
username: PropTypes.string,
|
|
|
|
token: PropTypes.string,
|
|
|
|
avatarSize: PropTypes.number,
|
|
|
|
testID: PropTypes.string,
|
|
|
|
width: PropTypes.number,
|
|
|
|
status: PropTypes.string,
|
|
|
|
toggleFav: PropTypes.func,
|
|
|
|
toggleRead: PropTypes.func,
|
|
|
|
hideChannel: PropTypes.func,
|
|
|
|
useRealName: PropTypes.bool,
|
|
|
|
getUserPresence: PropTypes.func,
|
|
|
|
connected: PropTypes.bool,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
isFocused: PropTypes.bool,
|
|
|
|
getRoomTitle: PropTypes.func,
|
|
|
|
getRoomAvatar: PropTypes.func,
|
|
|
|
getIsGroupChat: PropTypes.func,
|
|
|
|
getIsRead: PropTypes.func,
|
|
|
|
swipeEnabled: PropTypes.bool
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
avatarSize: 48,
|
|
|
|
status: 'offline',
|
|
|
|
getUserPresence: () => {},
|
|
|
|
getRoomTitle: () => 'title',
|
|
|
|
getRoomAvatar: () => '',
|
|
|
|
getIsGroupChat: () => false,
|
|
|
|
getIsRead: () => false,
|
|
|
|
swipeEnabled: true
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.mounted = false;
|
|
|
|
this.state = { avatarETag: '' };
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.mounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { avatarETag } = this.state;
|
|
|
|
if (nextState.avatarETag !== avatarETag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const { props } = this;
|
|
|
|
return !attrs.every(key => props[key] === nextProps[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { connected, getUserPresence, id } = this.props;
|
|
|
|
if (prevProps.connected !== connected && connected && this.isDirect) {
|
2020-04-01 12:28:54 +00:00
|
|
|
getUserPresence(id);
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
2020-03-03 21:10:39 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.userSubscription?.unsubscribe) {
|
|
|
|
this.userSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
if (this.roomSubscription?.unsubscribe) {
|
|
|
|
this.roomSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isGroupChat() {
|
|
|
|
const { item, getIsGroupChat } = this.props;
|
|
|
|
return getIsGroupChat(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
get isDirect() {
|
|
|
|
const { item: { t }, id } = this.props;
|
|
|
|
return t === 'd' && id && !this.isGroupChat;
|
|
|
|
}
|
|
|
|
|
|
|
|
init = async() => {
|
|
|
|
const { item } = this.props;
|
2020-07-20 16:44:54 +00:00
|
|
|
if (item?.observe) {
|
|
|
|
const observable = item.observe();
|
2020-10-30 13:12:02 +00:00
|
|
|
this.roomSubscription = observable?.subscribe?.(() => {
|
|
|
|
this.forceUpdate();
|
2020-07-20 16:44:54 +00:00
|
|
|
});
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
2020-07-20 16:44:54 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
if (this.isDirect) {
|
|
|
|
const { id } = this.props;
|
|
|
|
const db = database.active;
|
|
|
|
const usersCollection = db.collections.get('users');
|
|
|
|
try {
|
|
|
|
const user = await usersCollection.find(id);
|
|
|
|
const observable = user.observe();
|
|
|
|
this.userSubscription = observable.subscribe((u) => {
|
|
|
|
const { avatarETag } = u;
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ avatarETag });
|
|
|
|
} else {
|
|
|
|
this.state.avatarETag = avatarETag;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// User not found
|
|
|
|
}
|
2020-07-20 16:44:54 +00:00
|
|
|
}
|
2019-05-22 20:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
onPress = () => {
|
|
|
|
const { item, onPress } = this.props;
|
|
|
|
return onPress(item);
|
2019-04-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
render() {
|
|
|
|
const { avatarETag } = this.state;
|
|
|
|
const {
|
|
|
|
item,
|
|
|
|
getRoomTitle,
|
|
|
|
getRoomAvatar,
|
|
|
|
getIsRead,
|
|
|
|
width,
|
|
|
|
toggleFav,
|
|
|
|
toggleRead,
|
|
|
|
hideChannel,
|
|
|
|
testID,
|
|
|
|
theme,
|
|
|
|
isFocused,
|
|
|
|
avatarSize,
|
|
|
|
baseUrl,
|
|
|
|
userId,
|
|
|
|
token,
|
|
|
|
status,
|
|
|
|
showLastMessage,
|
|
|
|
username,
|
|
|
|
useRealName,
|
|
|
|
swipeEnabled
|
|
|
|
} = this.props;
|
|
|
|
const name = getRoomTitle(item);
|
|
|
|
const avatar = getRoomAvatar(item);
|
|
|
|
const isRead = getIsRead(item);
|
|
|
|
const date = item.lastMessage?.ts && formatDate(item.lastMessage.ts);
|
|
|
|
|
|
|
|
let accessibilityLabel = name;
|
|
|
|
if (item.unread === 1) {
|
|
|
|
accessibilityLabel += `, ${ item.unread } ${ I18n.t('alert') }`;
|
|
|
|
} else if (item.unread > 1) {
|
|
|
|
accessibilityLabel += `, ${ item.unread } ${ I18n.t('alerts') }`;
|
|
|
|
}
|
2019-07-01 14:20:38 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
if (item.userMentions > 0) {
|
|
|
|
accessibilityLabel += `, ${ I18n.t('you_were_mentioned') }`;
|
|
|
|
}
|
2019-10-29 13:53:58 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
if (date) {
|
|
|
|
accessibilityLabel += `, ${ I18n.t('last_message') } ${ date }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RoomItem
|
|
|
|
name={name}
|
|
|
|
avatar={avatar}
|
|
|
|
isGroupChat={this.isGroupChat}
|
|
|
|
isRead={isRead}
|
|
|
|
onPress={this.onPress}
|
|
|
|
date={date}
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
|
|
|
userMentions={item.userMentions}
|
|
|
|
width={width}
|
|
|
|
favorite={item.f}
|
|
|
|
toggleFav={toggleFav}
|
|
|
|
rid={item.rid}
|
|
|
|
toggleRead={toggleRead}
|
|
|
|
hideChannel={hideChannel}
|
|
|
|
testID={testID}
|
|
|
|
type={item.t}
|
|
|
|
theme={theme}
|
|
|
|
isFocused={isFocused}
|
|
|
|
size={avatarSize}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
userId={userId}
|
|
|
|
token={token}
|
|
|
|
prid={item.prid}
|
|
|
|
status={status}
|
|
|
|
hideUnreadStatus={item.hideUnreadStatus}
|
|
|
|
alert={item.alert}
|
|
|
|
roomUpdatedAt={item.roomUpdatedAt}
|
|
|
|
lastMessage={item.lastMessage}
|
|
|
|
showLastMessage={showLastMessage}
|
|
|
|
username={username}
|
|
|
|
useRealName={useRealName}
|
|
|
|
unread={item.unread}
|
|
|
|
groupMentions={item.groupMentions}
|
|
|
|
avatarETag={avatarETag}
|
|
|
|
swipeEnabled={swipeEnabled}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2020-07-31 17:06:22 +00:00
|
|
|
export default connect(mapStateToProps)(RoomItemContainer);
|