import React from 'react'; import PropTypes from 'prop-types'; import { View, Text, ScrollView } from 'react-native'; import { connect } from 'react-redux'; import moment from 'moment'; import { SafeAreaView } from 'react-navigation'; 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 RoomTypeIcon from '../../containers/RoomTypeIcon'; import I18n from '../../i18n'; import { CustomHeaderButtons, Item } from '../../containers/HeaderButton'; import StatusBar from '../../containers/StatusBar'; import log from '../../utils/log'; const PERMISSION_EDIT_ROOM = 'edit-room'; const camelize = str => str.replace(/^(.)/, (match, chr) => chr.toUpperCase()); const getRoomTitle = (room, type, name) => (type === 'd' ? {name} : ( {room.prid ? room.fname : room.name} ) ); class RoomInfoView extends React.Component { static navigationOptions = ({ navigation }) => { const showEdit = navigation.getParam('showEdit'); const rid = navigation.getParam('rid'); return { title: I18n.t('Room_Info'), headerRight: showEdit ? ( navigation.navigate('RoomInfoEditView', { rid })} testID='room-info-view-edit-button' /> ) : null }; } static propTypes = { navigation: PropTypes.object, user: PropTypes.shape({ id: PropTypes.string, token: PropTypes.string }), baseUrl: PropTypes.string, Message_TimeFormat: PropTypes.string } constructor(props) { super(props); this.rid = props.navigation.getParam('rid'); this.t = props.navigation.getParam('t'); this.roles = database.objects('roles'); this.sub = { unsubscribe: () => {} }; this.state = { room: {}, roomUser: {} }; } async componentDidMount() { if (this.t === 'd') { const { user } = this.props; const roomUserId = RocketChat.getRoomMemberId(this.rid, user.id); try { const result = await RocketChat.getUserInfo(roomUserId); if (result.success) { this.setState({ roomUser: result.user }); } } catch (e) { log(e); } return; } const rooms = database.objects('subscriptions').filtered('rid = $0', this.rid); let room = {}; if (rooms.length > 0) { this.setState({ room: rooms[0] }); [room] = rooms; } else { try { const result = await RocketChat.getRoomInfo(this.rid); if (result.success) { // eslint-disable-next-line prefer-destructuring room = result.room; this.setState({ room }); } } catch (error) { log('err_get_room_info', error); } } const permissions = RocketChat.hasPermission([PERMISSION_EDIT_ROOM], room.rid); if (permissions[PERMISSION_EDIT_ROOM] && !room.prid) { const { navigation } = this.props; navigation.setParams({ showEdit: true }); } } getRoleDescription = (id) => { const role = database.objectForPrimaryKey('roles', id); if (role) { return role.description; } return null; } isDirect = () => this.t === 'd' updateRoom = () => { if (this.rooms.length > 0) { this.setState({ room: JSON.parse(JSON.stringify(this.rooms[0])) }); } } renderItem = (key, room) => ( {I18n.t(camelize(key))} { room[key] ? room[key] : I18n.t(`No_${ key }_provided`) } ); renderRole = (role) => { const description = this.getRoleDescription(role); if (description) { return ( { this.getRoleDescription(role) } ); } return null; } renderRoles = () => { const { roomUser } = this.state; if (roomUser && roomUser.roles && roomUser.roles.length) { return ( {I18n.t('Roles')} {roomUser.roles.map(role => this.renderRole(role))} ); } return null; } renderTimezone = () => { const { roomUser } = this.state; const { Message_TimeFormat } = this.props; if (roomUser) { const { utcOffset } = roomUser; if (!utcOffset) { return null; } return ( {I18n.t('Timezone')} {moment().utcOffset(utcOffset).format(Message_TimeFormat)} (UTC { utcOffset }) ); } return null; } renderAvatar = (room, roomUser) => { const { baseUrl, user } = this.props; return ( {this.t === 'd' && roomUser._id ? : null} ); } renderBroadcast = () => ( {I18n.t('Broadcast_Channel')} {I18n.t('Broadcast_channel_Description')} ) renderCustomFields = () => { const { roomUser } = this.state; if (roomUser) { const { customFields } = roomUser; if (!roomUser.customFields) { return null; } return ( Object.keys(customFields).map((title) => { if (!customFields[title]) { return; } return ( {title} {customFields[title]} ); }) ); } return null; } renderChannel = () => { const { room } = this.state; return ( {this.renderItem('description', room)} {this.renderItem('topic', room)} {this.renderItem('announcement', room)} {room.broadcast ? this.renderBroadcast() : null} ); } renderDirect = () => { const { roomUser } = this.state; return ( {this.renderRoles()} {this.renderTimezone()} {this.renderCustomFields(roomUser._id)} ); } render() { const { room, roomUser } = this.state; if (!room) { return ; } return ( {this.renderAvatar(room, roomUser)} { getRoomTitle(room, this.t, roomUser && roomUser.name) } {this.isDirect() ? this.renderDirect() : this.renderChannel()} ); } } const mapStateToProps = state => ({ baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', user: { id: state.login.user && state.login.user.id, token: state.login.user && state.login.user.token }, Message_TimeFormat: state.settings.Message_TimeFormat }); export default connect(mapStateToProps)(RoomInfoView);