vn-verdnaturachat/app/views/RoomView/index.js

404 lines
10 KiB
JavaScript
Raw Normal View History

2017-08-04 00:34:37 +00:00
import React from 'react';
2017-08-05 18:16:32 +00:00
import PropTypes from 'prop-types';
import {
2019-01-29 19:52:56 +00:00
Text, View, LayoutAnimation, ActivityIndicator
} from 'react-native';
import { connect } from 'react-redux';
import { RectButton } from 'react-native-gesture-handler';
import SafeAreaView from 'react-native-safe-area-view';
2019-02-07 15:48:10 +00:00
import equal from 'deep-equal';
2017-08-13 01:35:09 +00:00
import Navigation from '../../lib/Navigation';
import { openRoom as openRoomAction, closeRoom as closeRoomAction, setLastOpen as setLastOpenAction } from '../../actions/room';
import { toggleReactionPicker as toggleReactionPickerAction, actionsShow as actionsShowAction } from '../../actions/messages';
import LoggedView from '../View';
import { List } from './ListView';
import database from '../../lib/realm';
import RocketChat from '../../lib/rocketchat';
import Message from '../../containers/message';
import MessageActions from '../../containers/MessageActions';
import MessageErrorActions from '../../containers/MessageErrorActions';
import MessageBox from '../../containers/MessageBox';
import ReactionPicker from './ReactionPicker';
import UploadProgress from './UploadProgress';
import styles from './styles';
import log from '../../utils/log';
2019-01-29 19:52:56 +00:00
import { isIOS } from '../../utils/deviceInfo';
2018-06-01 17:38:13 +00:00
import I18n from '../../i18n';
import Icons from '../../lib/Icons';
2018-10-02 12:33:21 +00:00
import ConnectionBadge from '../../containers/ConnectionBadge';
@connect(state => ({
user: {
id: state.login.user && state.login.user.id,
username: state.login.user && state.login.user.username,
token: state.login.user && state.login.user.token
},
actionMessage: state.messages.actionMessage,
showActions: state.messages.showActions,
2018-12-05 20:52:08 +00:00
showErrorActions: state.messages.showErrorActions,
appState: state.app.ready && state.app.foreground ? 'foreground' : 'background'
}), dispatch => ({
openRoom: room => dispatch(openRoomAction(room)),
setLastOpen: date => dispatch(setLastOpenAction(date)),
toggleReactionPicker: message => dispatch(toggleReactionPickerAction(message)),
actionsShow: actionMessage => dispatch(actionsShowAction(actionMessage)),
closeRoom: () => dispatch(closeRoomAction())
}))
/** @extends React.Component */
export default class RoomView extends LoggedView {
static options() {
return {
topBar: {
2018-10-31 18:40:08 +00:00
title: {
component: {
name: 'RoomHeaderView',
alignment: 'left'
2018-10-31 18:40:08 +00:00
}
},
rightButtons: [{
id: 'more',
testID: 'room-view-header-actions',
icon: Icons.getSource('more')
}, {
id: 'star',
testID: 'room-view-header-star',
icon: Icons.getSource('starOutline')
}]
},
blurOnUnmount: true
};
}
2017-08-05 18:16:32 +00:00
static propTypes = {
componentId: PropTypes.string,
2017-11-20 22:18:00 +00:00
openRoom: PropTypes.func.isRequired,
setLastOpen: PropTypes.func.isRequired,
user: PropTypes.shape({
id: PropTypes.string.isRequired,
username: PropTypes.string.isRequired,
token: PropTypes.string.isRequired
}),
2017-08-12 20:52:55 +00:00
rid: PropTypes.string,
name: PropTypes.string,
t: PropTypes.string,
showActions: PropTypes.bool,
showErrorActions: PropTypes.bool,
actionMessage: PropTypes.object,
2018-12-05 20:52:08 +00:00
appState: PropTypes.string,
toggleReactionPicker: PropTypes.func.isRequired,
actionsShow: PropTypes.func,
closeRoom: PropTypes.func
};
2017-08-05 18:16:32 +00:00
2017-08-04 00:34:37 +00:00
constructor(props) {
super('RoomView', props);
this.rid = props.rid;
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
2017-08-07 18:42:02 +00:00
this.state = {
loaded: false,
joined: this.rooms.length > 0,
room: {}
2017-08-07 18:42:02 +00:00
};
this.onReactionPress = this.onReactionPress.bind(this);
Navigation.events().bindComponent(this);
2017-08-07 00:34:35 +00:00
}
2017-08-04 00:34:37 +00:00
componentDidMount() {
2018-12-05 20:52:08 +00:00
if (this.rooms.length === 0 && this.rid) {
const { rid, name, t } = this.props;
this.setState(
{ room: { rid, name, t } },
() => this.updateRoom()
);
2018-12-05 20:52:08 +00:00
}
this.rooms.addListener(this.updateRoom);
this.internalSetState({ loaded: true });
}
shouldComponentUpdate(nextProps, nextState) {
const {
room, loaded, joined
} = this.state;
2018-12-05 20:52:08 +00:00
const { showActions, showErrorActions, appState } = this.props;
if (room.ro !== nextState.room.ro) {
return true;
} else if (room.f !== nextState.room.f) {
return true;
} else if (room.blocked !== nextState.room.blocked) {
return true;
} else if (room.blocker !== nextState.room.blocker) {
return true;
} else if (room.archived !== nextState.room.archived) {
return true;
} else if (loaded !== nextState.loaded) {
return true;
} else if (joined !== nextState.joined) {
return true;
} else if (showActions !== nextProps.showActions) {
return true;
} else if (showErrorActions !== nextProps.showErrorActions) {
return true;
2018-12-05 20:52:08 +00:00
} else if (appState !== nextProps.appState) {
return true;
2019-02-07 15:48:10 +00:00
} else if (!equal(room.muted, nextState.room.muted)) {
return true;
}
return false;
}
componentDidUpdate(prevProps, prevState) {
const { room } = this.state;
2018-12-05 20:52:08 +00:00
const { componentId, appState } = this.props;
if (prevState.room.f !== room.f) {
const rightButtons = [{
id: 'star',
testID: 'room-view-header-star',
icon: room.f ? Icons.getSource('star') : Icons.getSource('starOutline')
}];
if (room.t !== 'l') {
rightButtons.unshift({
id: 'more',
testID: 'room-view-header-actions',
icon: Icons.getSource('more')
});
}
Navigation.mergeOptions(componentId, {
topBar: {
rightButtons
}
});
2018-12-05 20:52:08 +00:00
} else if (appState === 'foreground' && appState !== prevProps.appState) {
RocketChat.loadMissedMessages(room).catch(e => console.log(e));
RocketChat.readMessages(room.rid).catch(e => console.log(e));
}
}
2017-08-07 00:34:35 +00:00
componentWillUnmount() {
const { closeRoom } = this.props;
closeRoom();
this.rooms.removeAllListeners();
2017-08-04 00:34:37 +00:00
}
onMessageLongPress = (message) => {
const { actionsShow } = this.props;
actionsShow(message);
}
onReactionPress = (shortname, messageId) => {
const { actionMessage, toggleReactionPicker } = this.props;
try {
if (!messageId) {
RocketChat.setReaction(shortname, actionMessage._id);
return toggleReactionPicker();
}
RocketChat.setReaction(shortname, messageId);
} catch (e) {
log('RoomView.onReactionPress', e);
}
};
2017-08-04 00:34:37 +00:00
internalSetState = (...args) => {
2019-01-29 19:52:56 +00:00
if (isIOS) {
LayoutAnimation.easeInEaseOut();
}
this.setState(...args);
}
navigationButtonPressed = ({ buttonId }) => {
const { room } = this.state;
const { rid, f } = room;
const { componentId } = this.props;
if (buttonId === 'more') {
Navigation.push(componentId, {
component: {
id: 'RoomActionsView',
name: 'RoomActionsView',
passProps: {
rid
}
}
});
} else if (buttonId === 'star') {
try {
2018-12-05 20:52:08 +00:00
RocketChat.toggleFavorite(rid, !f);
} catch (e) {
log('toggleFavorite', e);
}
}
}
// eslint-disable-next-line react/sort-comp
updateRoom = () => {
2018-10-31 18:40:08 +00:00
const { openRoom, setLastOpen } = this.props;
if (this.rooms.length > 0) {
const { room: prevRoom } = this.state;
2018-10-16 20:30:04 +00:00
const room = JSON.parse(JSON.stringify(this.rooms[0] || {}));
this.internalSetState({ room });
if (!prevRoom._id) {
openRoom({
...room
});
if (room.alert || room.unread || room.userMentions) {
setLastOpen(room.ls);
} else {
setLastOpen(null);
}
}
} else {
2018-12-05 20:52:08 +00:00
const { room } = this.state;
if (room.rid) {
openRoom(room);
this.internalSetState({ joined: false });
}
}
}
sendMessage = (message) => {
const { setLastOpen } = this.props;
LayoutAnimation.easeInEaseOut();
RocketChat.sendMessage(this.rid, message).then(() => {
setLastOpen(null);
});
};
2017-08-09 01:40:55 +00:00
joinRoom = async() => {
const { rid } = this.props;
try {
2018-12-05 20:52:08 +00:00
const result = await RocketChat.joinRoom(rid);
if (result.success) {
this.internalSetState({
joined: true
});
}
} catch (e) {
log('joinRoom', e);
}
2017-08-10 16:16:32 +00:00
};
isOwner = () => {
const { room } = this.state;
return room && room.roles && Array.from(Object.keys(room.roles), i => room.roles[i].value).includes('owner');
}
isMuted = () => {
const { room } = this.state;
const { user } = this.props;
2019-02-07 15:48:10 +00:00
return room && room.muted && !!Array.from(Object.keys(room.muted), i => room.muted[i].value).includes(user.username);
}
isReadOnly = () => {
const { room } = this.state;
2019-02-07 15:48:10 +00:00
return (room.ro && !room.broadcast) || this.isMuted() || room.archived;
}
isBlocked = () => {
const { room } = this.state;
if (room) {
const { t, blocked, blocker } = room;
if (t === 'd' && (blocked || blocker)) {
return true;
}
}
return false;
}
renderItem = (item, previousItem) => {
const { room } = this.state;
const { user } = this.props;
return (
<Message
key={item._id}
item={item}
status={item.status}
reactions={JSON.parse(JSON.stringify(item.reactions))}
user={user}
archived={room.archived}
broadcast={room.broadcast}
previousItem={previousItem}
_updatedAt={item._updatedAt}
onReactionPress={this.onReactionPress}
onLongPress={this.onMessageLongPress}
/>
);
}
2017-08-07 00:34:35 +00:00
2017-08-10 16:16:32 +00:00
renderFooter = () => {
2019-02-07 15:48:10 +00:00
const { joined } = this.state;
if (!joined) {
return (
<View style={styles.joinRoomContainer} key='room-view-join' testID='room-view-join'>
<Text style={styles.previewMode}>{I18n.t('You_are_in_preview_mode')}</Text>
<RectButton
onPress={this.joinRoom}
style={styles.joinRoomButton}
activeOpacity={0.5}
underlayColor='#fff'
>
<Text style={styles.joinRoomText} testID='room-view-join-button'>{I18n.t('Join')}</Text>
</RectButton>
</View>
);
}
2019-02-07 15:48:10 +00:00
if (this.isReadOnly()) {
return (
<View style={styles.readOnly} key='room-view-read-only'>
2018-06-01 17:38:13 +00:00
<Text>{I18n.t('This_room_is_read_only')}</Text>
</View>
);
}
if (this.isBlocked()) {
return (
<View style={styles.readOnly} key='room-view-block'>
2018-06-01 17:38:13 +00:00
<Text>{I18n.t('This_room_is_blocked')}</Text>
</View>
);
}
return <MessageBox key='room-view-messagebox' onSubmit={this.sendMessage} rid={this.rid} />;
};
2017-08-09 20:08:50 +00:00
renderList = () => {
const { loaded, room } = this.state;
if (!loaded || !room.rid) {
return <ActivityIndicator style={styles.loading} />;
}
return (
[
<List
key='room-view-messages'
room={room}
renderRow={this.renderItem}
/>,
this.renderFooter()
]
);
}
2017-08-04 00:34:37 +00:00
render() {
const { room } = this.state;
const { user, showActions, showErrorActions } = this.props;
2017-08-04 00:34:37 +00:00
return (
<SafeAreaView style={styles.container} testID='room-view' forceInset={{ bottom: 'never' }}>
{this.renderList()}
{room._id && showActions
? <MessageActions room={room} user={user} />
: null
}
{showErrorActions ? <MessageErrorActions /> : null}
<ReactionPicker onEmojiSelected={this.onReactionPress} />
<UploadProgress rid={this.rid} />
2018-10-02 12:33:21 +00:00
<ConnectionBadge />
</SafeAreaView>
2017-08-04 00:34:37 +00:00
);
}
}