import React from 'react'; import PropTypes from 'prop-types'; import { View, Text, ScrollView } from 'react-native'; import { connect } from 'react-redux'; import MaterialIcon from 'react-native-vector-icons/MaterialIcons'; import moment from 'moment'; import LoggedView from '../View'; import Status from '../../containers/status'; import Avatar from '../../containers/Avatar'; import styles from './styles'; import sharedStyles from '../Styles'; import database from '../../lib/realm'; import RocketChat from '../../lib/rocketchat'; import Touch from '../../utils/touch'; import log from '../../utils/log'; import RoomTypeIcon from '../../containers/RoomTypeIcon'; import I18n from '../../i18n'; const PERMISSION_EDIT_ROOM = 'edit-room'; const camelize = str => str.replace(/^(.)/, (match, chr) => chr.toUpperCase()); const getRoomTitle = room => (room.t === 'd' ? {room.fname} : [ , {room.name} ] ); @connect(state => ({ baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', user: state.login.user, permissions: state.permissions, activeUsers: state.activeUsers, Message_TimeFormat: state.settings.Message_TimeFormat, roles: state.roles })) export default class RoomInfoView extends LoggedView { static propTypes = { baseUrl: PropTypes.string, user: PropTypes.object, navigation: PropTypes.object, activeUsers: PropTypes.object, Message_TimeFormat: PropTypes.string, roles: PropTypes.object } static navigationOptions = ({ navigation }) => { const params = navigation.state.params || {}; if (!params.hasEditPermission) { return; } return { headerRight: ( navigation.navigate({ key: 'RoomInfoEdit', routeName: 'RoomInfoEdit', params: { rid: navigation.state.params.rid } })} underlayColor='#ffffff' activeOpacity={0.5} accessibilityLabel={I18n.t('edit')} accessibilityTraits='button' testID='room-info-view-edit-button' > ) }; }; constructor(props) { super('RoomInfoView', props); const { rid } = props.navigation.state.params; this.rooms = database.objects('subscriptions').filtered('rid = $0', rid); this.sub = { unsubscribe: () => {} }; this.state = { room: {}, roomUser: {}, roles: [] }; } async componentDidMount() { await this.updateRoom(); this.rooms.addListener(this.updateRoom); // get user of room if (this.state.room.t === 'd') { try { const roomUser = await RocketChat.getRoomMember(this.state.room.rid, this.props.user.id); this.setState({ roomUser }); const username = this.state.room.name; const activeUser = this.props.activeUsers[roomUser._id]; if (!activeUser || !activeUser.utcOffset) { // get full user data looking for utcOffset // will be catched by .on('users) and saved on activeUsers reducer this.getFullUserData(username); } // get all users roles // needs to be changed by a better method const allUsersRoles = await RocketChat.getUserRoles(); const userRoles = allUsersRoles.find(user => user.username === username); if (userRoles) { this.setState({ roles: userRoles.roles || [] }); } } catch (e) { log('RoomInfoView.componentDidMount', e); } } else { const permissions = RocketChat.hasPermission([PERMISSION_EDIT_ROOM], this.state.room.rid); this.props.navigation.setParams({ hasEditPermission: permissions[PERMISSION_EDIT_ROOM] }); } } componentWillUnmount() { this.rooms.removeAllListeners(); this.sub.unsubscribe(); } getFullUserData = async(username) => { const result = await RocketChat.subscribe('fullUserData', username); this.sub = result; } isDirect = () => this.state.room.t === 'd'; updateRoom = async() => { const [room] = this.rooms; this.setState({ room }); } renderItem = (key, room) => ( {I18n.t(camelize(key))} { room[key] ? room[key] : I18n.t(`No_${ key }_provided`) } ); renderRoles = () => ( this.state.roles.length > 0 && {I18n.t('Roles')} {this.state.roles.map(role => ( { this.props.roles[role] } ))} ) renderTimezone = (userId) => { if (this.props.activeUsers[userId]) { const { utcOffset } = this.props.activeUsers[userId]; if (!utcOffset) { return null; } // TODO: translate return ( {I18n.t('Timezone')} {moment().utcOffset(utcOffset).format(this.props.Message_TimeFormat)} (UTC { utcOffset }) ); } return null; } renderAvatar = (room, roomUser) => ( {room.t === 'd' ? : null} ) renderBroadcast = () => ( {I18n.t('Broadcast_Channel')} {I18n.t('Broadcast_channel_Description')} ) render() { const { room, roomUser } = this.state; if (!room) { return ; } return ( {this.renderAvatar(room, roomUser)} { getRoomTitle(room) } {!this.isDirect() && this.renderItem('description', room)} {!this.isDirect() && this.renderItem('topic', room)} {!this.isDirect() && this.renderItem('announcement', room)} {this.isDirect() && this.renderRoles()} {this.isDirect() && this.renderTimezone(roomUser._id)} {room.broadcast && this.renderBroadcast()} ); } }