2017-12-08 19:13:21 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Text, View, Platform, TouchableOpacity } from 'react-native';
|
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { HeaderBackButton } from 'react-navigation';
|
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
import RocketChat from '../../../lib/rocketchat';
|
2017-12-08 19:13:21 +00:00
|
|
|
import realm from '../../../lib/realm';
|
|
|
|
import Avatar from '../../../containers/Avatar';
|
|
|
|
import { STATUS_COLORS } from '../../../constants/colors';
|
|
|
|
import styles from './styles';
|
2018-01-15 18:44:20 +00:00
|
|
|
import { closeRoom } from '../../../actions/room';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
|
|
|
import log from '../../../utils/log';
|
2018-05-18 16:41:47 +00:00
|
|
|
import RoomTypeIcon from '../../../containers/RoomTypeIcon';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const title = (offline, connecting, authenticating, logged) => {
|
|
|
|
if (offline) {
|
|
|
|
return 'You are offline...';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connecting) {
|
|
|
|
return 'Connecting...';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authenticating) {
|
|
|
|
return 'Authenticating...';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (logged) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Not logged...';
|
|
|
|
};
|
2017-12-08 19:13:21 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
user: state.login.user,
|
2018-04-24 19:34:03 +00:00
|
|
|
activeUsers: state.activeUsers,
|
|
|
|
loading: state.messages.isFetching,
|
|
|
|
connecting: state.meteor.connecting,
|
|
|
|
authenticating: state.login.isFetching,
|
|
|
|
offline: !state.meteor.connected,
|
|
|
|
logged: !!state.login.token
|
2018-01-15 18:44:20 +00:00
|
|
|
}), dispatch => ({
|
|
|
|
close: () => dispatch(closeRoom())
|
2017-12-08 19:13:21 +00:00
|
|
|
}))
|
2018-02-08 14:08:50 +00:00
|
|
|
export default class RoomHeaderView extends React.PureComponent {
|
2017-12-08 19:13:21 +00:00
|
|
|
static propTypes = {
|
2018-01-15 18:44:20 +00:00
|
|
|
close: PropTypes.func.isRequired,
|
2017-12-08 19:13:21 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
|
|
|
user: PropTypes.object.isRequired,
|
|
|
|
activeUsers: PropTypes.object
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-05-07 20:43:26 +00:00
|
|
|
room: props.navigation.state.params.room
|
2017-12-08 19:13:21 +00:00
|
|
|
};
|
2018-05-07 20:43:26 +00:00
|
|
|
this.room = realm.objects('subscriptions').filtered('rid = $0', this.state.room.rid);
|
2017-12-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.updateState();
|
2018-05-07 20:43:26 +00:00
|
|
|
this.room.addListener(this.updateState);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.navigation.state.params.room !== this.props.navigation.state.params.room) {
|
|
|
|
this.room.removeAllListeners();
|
|
|
|
this.room = realm.objects('subscriptions').filtered('rid = $0', nextProps.navigation.state.params.room.rid);
|
|
|
|
this.room.addListener(this.updateState);
|
|
|
|
}
|
2017-12-08 19:13:21 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.room.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserStatus() {
|
2018-05-07 20:43:26 +00:00
|
|
|
const userId = this.state.room.rid.replace(this.props.user.id, '').trim();
|
2018-03-29 17:55:37 +00:00
|
|
|
const userInfo = this.props.activeUsers[userId];
|
|
|
|
return (userInfo && userInfo.status) || 'offline';
|
2017-12-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getUserStatusLabel() {
|
|
|
|
const status = this.getUserStatus();
|
|
|
|
return status.charAt(0).toUpperCase() + status.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateState = () => {
|
2018-05-07 20:43:26 +00:00
|
|
|
if (this.room.length > 0) {
|
|
|
|
this.setState({ room: this.room[0] });
|
|
|
|
}
|
2017-12-08 19:13:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
isDirect = () => this.state.room && this.state.room.t === 'd';
|
|
|
|
|
2018-01-15 18:44:20 +00:00
|
|
|
renderLeft = () => (<HeaderBackButton
|
|
|
|
onPress={() => {
|
|
|
|
this.props.navigation.goBack(null);
|
2018-03-02 21:31:44 +00:00
|
|
|
requestAnimationFrame(() => this.props.close());
|
2018-01-15 18:44:20 +00:00
|
|
|
}}
|
|
|
|
tintColor='#292E35'
|
|
|
|
title='Back'
|
|
|
|
titleStyle={{ display: 'none' }}
|
|
|
|
/>);
|
2017-12-08 19:13:21 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderCenter() {
|
2018-05-07 20:43:26 +00:00
|
|
|
if (!this.state.room.name) {
|
2018-05-18 17:55:08 +00:00
|
|
|
return <View style={styles.titleContainer} />;
|
2017-12-08 19:13:21 +00:00
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
let accessibilityLabel = this.state.room.name;
|
2017-12-11 20:37:33 +00:00
|
|
|
|
|
|
|
if (this.isDirect()) {
|
|
|
|
accessibilityLabel += `, ${ this.getUserStatusLabel() }`;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
const {
|
|
|
|
offline, connecting, authenticating, logged, loading
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
let t = '';
|
|
|
|
if (!title(offline, connecting, authenticating, logged) && loading) {
|
|
|
|
t = 'Loading messages...';
|
|
|
|
} else if (this.isDirect()) {
|
|
|
|
t = this.getUserStatusLabel();
|
|
|
|
} else {
|
|
|
|
t = this.state.room.topic || ' ';
|
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
return (
|
2018-03-29 17:55:37 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.titleContainer}
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
|
|
|
accessibilityTraits='header'
|
2018-05-23 13:39:18 +00:00
|
|
|
onPress={() => this.props.navigation.navigate({ key: 'RoomInfo', routeName: 'RoomInfo', params: { rid: this.state.room.rid } })}
|
|
|
|
testID='room-view-header-title'
|
2018-03-29 17:55:37 +00:00
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
<Avatar
|
2018-05-07 20:43:26 +00:00
|
|
|
text={this.state.room.name}
|
2017-12-08 19:13:21 +00:00
|
|
|
size={24}
|
2018-04-24 19:34:03 +00:00
|
|
|
style={styles.avatar}
|
2017-12-08 19:13:21 +00:00
|
|
|
type={this.state.room.t}
|
2018-04-24 19:34:03 +00:00
|
|
|
>
|
2017-12-08 19:13:21 +00:00
|
|
|
{this.isDirect() ?
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={[styles.status, { backgroundColor: STATUS_COLORS[this.getUserStatus()] }]} />
|
2017-12-08 19:13:21 +00:00
|
|
|
: null
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
</Avatar>
|
|
|
|
<View style={styles.titleTextContainer}>
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={{ flexDirection: 'row' }}>
|
|
|
|
<RoomTypeIcon type={this.state.room.t} size={13} />
|
|
|
|
<Text style={styles.title} allowFontScaling={false} testID='room-view-title'>
|
|
|
|
{this.state.room.name}
|
|
|
|
</Text>
|
|
|
|
</View>
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
{ t && <Text style={styles.userStatus} allowFontScaling={false} numberOfLines={1}>{t}</Text>}
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRight = () => (
|
|
|
|
<View style={styles.right}>
|
2018-04-24 19:34:03 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.headerButton}
|
2018-05-18 17:55:08 +00:00
|
|
|
onPress={() => {
|
|
|
|
try {
|
|
|
|
RocketChat.toggleFavorite(this.state.room.rid, this.state.room.f);
|
|
|
|
} catch (e) {
|
|
|
|
log('toggleFavorite', e);
|
|
|
|
}
|
|
|
|
}}
|
2018-02-19 21:19:39 +00:00
|
|
|
accessibilityLabel='Star room'
|
|
|
|
accessibilityTraits='button'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='room-view-header-star'
|
2018-02-19 21:19:39 +00:00
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
<Icon
|
|
|
|
name={`${ Platform.OS === 'ios' ? 'ios' : 'md' }-star${ this.state.room.f ? '' : '-outline' }`}
|
|
|
|
color='#f6c502'
|
|
|
|
size={24}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
2017-12-08 19:13:21 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.headerButton}
|
2018-04-24 19:34:03 +00:00
|
|
|
onPress={() => this.props.navigation.navigate({ key: 'RoomActions', routeName: 'RoomActions', params: { rid: this.state.room.rid } })}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Room actions'
|
|
|
|
accessibilityTraits='button'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='room-view-header-actions'
|
2017-12-08 19:13:21 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name={Platform.OS === 'ios' ? 'ios-more' : 'md-more'}
|
|
|
|
color='#292E35'
|
|
|
|
size={24}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={styles.header} testID='room-view-header'>
|
2017-12-08 19:13:21 +00:00
|
|
|
{this.renderLeft()}
|
2018-04-24 19:34:03 +00:00
|
|
|
{this.renderCenter()}
|
2017-12-08 19:13:21 +00:00
|
|
|
{this.renderRight()}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|