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'; import RocketChat from '../../../lib/rocketchat'; import realm from '../../../lib/realm'; import Avatar from '../../../containers/Avatar'; import { STATUS_COLORS } from '../../../constants/colors'; import styles from './styles'; import { closeRoom } from '../../../actions/room'; import log from '../../../utils/log'; import RoomTypeIcon from '../../../containers/RoomTypeIcon'; import I18n from '../../../i18n'; const title = (offline, connecting, authenticating, logged) => { if (offline) { return `${ I18n.t('You_are_offline') }...`; } if (connecting) { return `${ I18n.t('Connecting') }...`; } if (authenticating) { return `${ I18n.t('Authenticating') }...`; } if (logged) { return null; } return `${ I18n.t('Not_logged') }...`; }; @connect(state => ({ user: state.login.user, activeUsers: state.activeUsers, loading: state.messages.isFetching, connecting: state.meteor.connecting, authenticating: state.login.isFetching, offline: !state.meteor.connected, logged: !!state.login.token }), dispatch => ({ close: () => dispatch(closeRoom()) })) export default class RoomHeaderView extends React.PureComponent { static propTypes = { close: PropTypes.func.isRequired, navigation: PropTypes.object.isRequired, user: PropTypes.object.isRequired, activeUsers: PropTypes.object } constructor(props) { super(props); this.state = { room: props.navigation.state.params.room }; this.room = realm.objects('subscriptions').filtered('rid = $0', this.state.room.rid); } componentDidMount() { this.updateState(); 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); } } componentWillUnmount() { this.room.removeAllListeners(); } getUserStatus() { const userId = this.state.room.rid.replace(this.props.user.id, '').trim(); const userInfo = this.props.activeUsers[userId]; return (userInfo && userInfo.status) || 'offline'; } getUserStatusLabel() { const status = this.getUserStatus(); return I18n.t(status.charAt(0).toUpperCase() + status.slice(1)); } updateState = () => { if (this.room.length > 0) { this.setState({ room: this.room[0] }); } }; isDirect = () => this.state.room && this.state.room.t === 'd'; renderLeft = () => ( { this.props.navigation.goBack(null); requestAnimationFrame(() => this.props.close()); }} tintColor='#292E35' title={I18n.t('Back')} titleStyle={{ display: 'none' }} />); renderCenter() { if (!this.state.room.name) { return ; } let accessibilityLabel = this.state.room.name; if (this.isDirect()) { accessibilityLabel += `, ${ this.getUserStatusLabel() }`; } const { offline, connecting, authenticating, logged, loading } = this.props; let t = ''; if (!title(offline, connecting, authenticating, logged) && loading) { t = I18n.t('Loading_messages_ellipsis'); } else if (this.isDirect()) { t = this.getUserStatusLabel(); } else { t = this.state.room.topic || ' '; } return ( this.props.navigation.navigate({ key: 'RoomInfo', routeName: 'RoomInfo', params: { rid: this.state.room.rid } })} testID='room-view-header-title' > {this.isDirect() ? : null } {this.state.room.name} { t && {t}} ); } renderRight = () => ( { try { RocketChat.toggleFavorite(this.state.room.rid, this.state.room.f); } catch (e) { log('toggleFavorite', e); } }} accessibilityLabel={I18n.t('Star_room')} accessibilityTraits='button' testID='room-view-header-star' > this.props.navigation.navigate({ key: 'RoomActions', routeName: 'RoomActions', params: { rid: this.state.room.rid } })} accessibilityLabel={I18n.t('Room_actions')} accessibilityTraits='button' testID='room-view-header-actions' > ); render() { return ( {this.renderLeft()} {this.renderCenter()} {this.renderRight()} ); } }