2019-04-18 20:57:35 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-07-04 16:15:30 +00:00
|
|
|
import { View, Text, Animated } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
import {
|
|
|
|
RectButton,
|
|
|
|
PanGestureHandler,
|
|
|
|
State
|
|
|
|
} from 'react-native-gesture-handler';
|
|
|
|
import { connect } from 'react-redux';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import I18n from '../../i18n';
|
2019-07-15 16:54:28 +00:00
|
|
|
import styles, {
|
2019-09-16 20:26:32 +00:00
|
|
|
ROW_HEIGHT,
|
|
|
|
ACTION_WIDTH,
|
|
|
|
SMALL_SWIPE,
|
|
|
|
LONG_SWIPE
|
2019-07-15 16:54:28 +00:00
|
|
|
} 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-07-15 16:54:28 +00:00
|
|
|
import { LeftActions, RightActions } from './Actions';
|
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',
|
|
|
|
'alert',
|
|
|
|
'type',
|
|
|
|
'width',
|
|
|
|
'isRead',
|
|
|
|
'favorite',
|
|
|
|
'status'
|
|
|
|
];
|
2019-07-15 16:54:28 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
class RoomItem extends React.Component {
|
2019-04-18 20:57:35 +00:00
|
|
|
static 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,
|
2019-07-04 16:15:30 +00:00
|
|
|
width: PropTypes.number,
|
2019-07-01 14:20:38 +00:00
|
|
|
favorite: PropTypes.bool,
|
|
|
|
isRead: PropTypes.bool,
|
|
|
|
rid: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
status: PropTypes.string,
|
2019-07-01 14:20:38 +00:00
|
|
|
toggleFav: PropTypes.func,
|
|
|
|
toggleRead: PropTypes.func,
|
2019-09-26 18:35:33 +00:00
|
|
|
hideChannel: PropTypes.func,
|
|
|
|
avatar: PropTypes.bool
|
|
|
|
}
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
avatarSize: 48
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-04-18 20:57:35 +00:00
|
|
|
|
2019-05-22 20:15:35 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-07-15 16:54:28 +00:00
|
|
|
this.dragX = new Animated.Value(0);
|
|
|
|
this.rowOffSet = new Animated.Value(0);
|
|
|
|
this.transX = Animated.add(
|
|
|
|
this.rowOffSet,
|
|
|
|
this.dragX
|
2019-07-01 14:20:38 +00:00
|
|
|
);
|
|
|
|
this.state = {
|
2019-07-04 16:15:30 +00:00
|
|
|
rowState: 0 // 0: closed, 1: right opened, -1: left opened
|
2019-07-01 14:20:38 +00:00
|
|
|
};
|
|
|
|
this._onGestureEvent = Animated.event(
|
2019-07-15 16:54:28 +00:00
|
|
|
[{ nativeEvent: { translationX: this.dragX } }]
|
2019-07-01 14:20:38 +00:00
|
|
|
);
|
|
|
|
this._value = 0;
|
2019-05-22 20:15:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
shouldComponentUpdate(nextProps) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { _updatedAt } = this.props;
|
2019-07-15 16:54:28 +00:00
|
|
|
if (_updatedAt && nextProps._updatedAt && nextProps._updatedAt.toISOString() !== _updatedAt.toISOString()) {
|
2019-07-01 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-18 20:57:35 +00:00
|
|
|
// eslint-disable-next-line react/destructuring-assignment
|
|
|
|
return attrs.some(key => nextProps[key] !== this.props[key]);
|
|
|
|
}
|
|
|
|
|
2019-07-01 14:20:38 +00:00
|
|
|
_onHandlerStateChange = ({ nativeEvent }) => {
|
|
|
|
if (nativeEvent.oldState === State.ACTIVE) {
|
|
|
|
this._handleRelease(nativeEvent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleRelease = (nativeEvent) => {
|
|
|
|
const { translationX } = nativeEvent;
|
|
|
|
const { rowState } = this.state;
|
2019-07-15 16:54:28 +00:00
|
|
|
this._value = this._value + translationX;
|
|
|
|
|
2019-07-01 14:20:38 +00:00
|
|
|
let toValue = 0;
|
|
|
|
if (rowState === 0) { // if no option is opened
|
2019-07-15 16:54:28 +00:00
|
|
|
if (translationX > 0 && translationX < LONG_SWIPE) {
|
|
|
|
toValue = ACTION_WIDTH; // open left option if he swipe right but not enough to trigger action
|
2019-07-01 14:20:38 +00:00
|
|
|
this.setState({ rowState: -1 });
|
2019-07-15 16:54:28 +00:00
|
|
|
} else if (translationX >= LONG_SWIPE) {
|
2019-07-01 14:20:38 +00:00
|
|
|
toValue = 0;
|
|
|
|
this.toggleRead();
|
2019-07-15 16:54:28 +00:00
|
|
|
} else if (translationX < 0 && translationX > -LONG_SWIPE) {
|
|
|
|
toValue = -2 * ACTION_WIDTH; // open right option if he swipe left
|
2019-07-01 14:20:38 +00:00
|
|
|
this.setState({ rowState: 1 });
|
2019-07-15 16:54:28 +00:00
|
|
|
} else if (translationX <= -LONG_SWIPE) {
|
|
|
|
toValue = 0;
|
|
|
|
this.setState({ rowState: 0 });
|
2019-07-01 14:20:38 +00:00
|
|
|
this.hideChannel();
|
|
|
|
} else {
|
|
|
|
toValue = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowState === -1) { // if left option is opened
|
|
|
|
if (this._value < SMALL_SWIPE) {
|
|
|
|
toValue = 0;
|
|
|
|
this.setState({ rowState: 0 });
|
2019-07-15 16:54:28 +00:00
|
|
|
} else if (this._value > LONG_SWIPE) {
|
2019-07-01 14:20:38 +00:00
|
|
|
toValue = 0;
|
|
|
|
this.setState({ rowState: 0 });
|
|
|
|
this.toggleRead();
|
|
|
|
} else {
|
2019-07-15 16:54:28 +00:00
|
|
|
toValue = ACTION_WIDTH;
|
2019-07-01 14:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowState === 1) { // if right option is opened
|
|
|
|
if (this._value > -2 * SMALL_SWIPE) {
|
|
|
|
toValue = 0;
|
|
|
|
this.setState({ rowState: 0 });
|
2019-07-15 16:54:28 +00:00
|
|
|
} else if (this._value < -LONG_SWIPE) {
|
2019-07-01 14:20:38 +00:00
|
|
|
toValue = 0;
|
|
|
|
this.setState({ rowState: 0 });
|
|
|
|
this.hideChannel();
|
|
|
|
} else {
|
2019-07-15 16:54:28 +00:00
|
|
|
toValue = -2 * ACTION_WIDTH;
|
2019-07-01 14:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._animateRow(toValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
_animateRow = (toValue) => {
|
2019-07-15 16:54:28 +00:00
|
|
|
this.rowOffSet.setValue(this._value);
|
|
|
|
this._value = toValue;
|
|
|
|
this.dragX.setValue(0);
|
|
|
|
Animated.spring(this.rowOffSet, {
|
2019-07-01 14:20:38 +00:00
|
|
|
toValue,
|
2019-07-15 16:54:28 +00:00
|
|
|
bounciness: 0,
|
|
|
|
useNativeDriver: true
|
2019-07-01 14:20:38 +00:00
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
close = () => {
|
2019-07-04 16:15:30 +00:00
|
|
|
this.setState({ rowState: 0 });
|
2019-07-01 14:20:38 +00:00
|
|
|
this._animateRow(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleFav = () => {
|
|
|
|
const { toggleFav, rid, favorite } = this.props;
|
|
|
|
if (toggleFav) {
|
|
|
|
toggleFav(rid, favorite);
|
|
|
|
}
|
|
|
|
this.close();
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-07-01 14:20:38 +00:00
|
|
|
|
|
|
|
toggleRead = () => {
|
|
|
|
const { toggleRead, rid, isRead } = this.props;
|
|
|
|
if (toggleRead) {
|
|
|
|
toggleRead(rid, isRead);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-07-01 14:20:38 +00:00
|
|
|
|
|
|
|
hideChannel = () => {
|
|
|
|
const { hideChannel, rid, type } = this.props;
|
|
|
|
if (hideChannel) {
|
|
|
|
hideChannel(rid, type);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-07-01 14:20:38 +00:00
|
|
|
|
2019-07-15 16:54:28 +00:00
|
|
|
onToggleReadPress = () => {
|
|
|
|
this.toggleRead();
|
|
|
|
this.close();
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-07-15 16:54:28 +00:00
|
|
|
|
|
|
|
onHidePress = () => {
|
|
|
|
this.hideChannel();
|
|
|
|
this.close();
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-07-15 16:54:28 +00:00
|
|
|
|
2019-07-04 16:15:30 +00:00
|
|
|
onPress = () => {
|
|
|
|
const { rowState } = this.state;
|
|
|
|
if (rowState !== 0) {
|
|
|
|
this.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { onPress } = this.props;
|
|
|
|
if (onPress) {
|
|
|
|
onPress();
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2019-07-04 16:15:30 +00:00
|
|
|
|
2019-04-18 20:57:35 +00:00
|
|
|
render() {
|
|
|
|
const {
|
2019-09-26 18:35:33 +00:00
|
|
|
unread, userMentions, name, _updatedAt, alert, testID, type, avatarSize, baseUrl, userId, username, token, id, prid, showLastMessage, lastMessage, isRead, width, favorite, status, avatar
|
2019-04-18 20:57:35 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2019-07-16 14:30:29 +00:00
|
|
|
const date = formatDate(_updatedAt);
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
let accessibilityLabel = name;
|
|
|
|
if (unread === 1) {
|
|
|
|
accessibilityLabel += `, ${ unread } ${ I18n.t('alert') }`;
|
|
|
|
} else if (unread > 1) {
|
|
|
|
accessibilityLabel += `, ${ unread } ${ I18n.t('alerts') }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userMentions > 0) {
|
|
|
|
accessibilityLabel += `, ${ I18n.t('you_were_mentioned') }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
accessibilityLabel += `, ${ I18n.t('last_message') } ${ date }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-07-01 14:20:38 +00:00
|
|
|
<PanGestureHandler
|
2019-07-15 16:54:28 +00:00
|
|
|
minDeltaX={20}
|
2019-07-01 14:20:38 +00:00
|
|
|
onGestureEvent={this._onGestureEvent}
|
|
|
|
onHandlerStateChange={this._onHandlerStateChange}
|
2019-04-18 20:57:35 +00:00
|
|
|
>
|
2019-07-15 16:54:28 +00:00
|
|
|
<Animated.View>
|
|
|
|
<LeftActions
|
|
|
|
transX={this.transX}
|
|
|
|
isRead={isRead}
|
|
|
|
width={width}
|
|
|
|
onToggleReadPress={this.onToggleReadPress}
|
|
|
|
/>
|
|
|
|
<RightActions
|
|
|
|
transX={this.transX}
|
|
|
|
favorite={favorite}
|
|
|
|
width={width}
|
|
|
|
toggleFav={this.toggleFav}
|
|
|
|
onHidePress={this.onHidePress}
|
|
|
|
/>
|
2019-07-01 14:20:38 +00:00
|
|
|
<Animated.View
|
2019-09-16 20:26:32 +00:00
|
|
|
style={{
|
|
|
|
transform: [{ translateX: this.transX }]
|
|
|
|
}}
|
2019-07-01 14:20:38 +00:00
|
|
|
>
|
|
|
|
<RectButton
|
2019-07-04 16:15:30 +00:00
|
|
|
onPress={this.onPress}
|
2019-07-01 14:20:38 +00:00
|
|
|
activeOpacity={0.8}
|
|
|
|
underlayColor='#e1e5e8'
|
|
|
|
testID={testID}
|
2019-07-04 16:15:30 +00:00
|
|
|
style={styles.button}
|
2019-07-01 14:20:38 +00:00
|
|
|
>
|
|
|
|
<View
|
2019-07-15 16:54:28 +00:00
|
|
|
style={styles.container}
|
2019-07-01 14:20:38 +00:00
|
|
|
accessibilityLabel={accessibilityLabel}
|
|
|
|
>
|
2019-09-16 20:26:32 +00:00
|
|
|
<Avatar
|
2019-09-26 18:35:33 +00:00
|
|
|
text={avatar}
|
2019-09-16 20:26:32 +00:00
|
|
|
size={avatarSize}
|
|
|
|
type={type}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
style={styles.avatar}
|
|
|
|
userId={userId}
|
|
|
|
token={token}
|
|
|
|
/>
|
2019-07-01 14:20:38 +00:00
|
|
|
<View style={styles.centerContainer}>
|
|
|
|
<View style={styles.titleContainer}>
|
2019-09-16 20:26:32 +00:00
|
|
|
<TypeIcon
|
|
|
|
type={type}
|
|
|
|
id={id}
|
|
|
|
prid={prid}
|
|
|
|
status={status}
|
|
|
|
/>
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.title,
|
|
|
|
alert && styles.alert
|
|
|
|
]}
|
|
|
|
ellipsizeMode='tail'
|
|
|
|
numberOfLines={1}
|
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</Text>
|
|
|
|
{_updatedAt ? (
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.date,
|
|
|
|
alert && styles.updateAlert
|
|
|
|
]}
|
|
|
|
ellipsizeMode='tail'
|
|
|
|
numberOfLines={1}
|
|
|
|
>
|
|
|
|
{capitalize(date)}
|
|
|
|
</Text>
|
|
|
|
) : null}
|
2019-07-01 14:20:38 +00:00
|
|
|
</View>
|
|
|
|
<View style={styles.row}>
|
2019-09-16 20:26:32 +00:00
|
|
|
<LastMessage
|
|
|
|
lastMessage={lastMessage}
|
|
|
|
type={type}
|
|
|
|
showLastMessage={showLastMessage}
|
|
|
|
username={username}
|
|
|
|
alert={alert}
|
|
|
|
/>
|
|
|
|
<UnreadBadge
|
|
|
|
unread={unread}
|
|
|
|
userMentions={userMentions}
|
|
|
|
type={type}
|
|
|
|
/>
|
2019-07-01 14:20:38 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</RectButton>
|
|
|
|
</Animated.View>
|
2019-07-15 16:54:28 +00:00
|
|
|
</Animated.View>
|
2019-07-01 14:20:38 +00:00
|
|
|
</PanGestureHandler>
|
2019-04-18 20:57:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
|
|
|
status: state.meteor.connected && ownProps.type === 'd' ? state.activeUsers[ownProps.id] : 'offline'
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RoomItem);
|