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';
|
2018-01-25 14:04:20 +00:00
|
|
|
import { Text, View, Button, SafeAreaView, Platform } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
2018-01-09 17:12:55 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-01-25 14:04:20 +00:00
|
|
|
import KeyboardSpacer from 'react-native-keyboard-spacer';
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2018-01-09 17:12:55 +00:00
|
|
|
import { ListView } from './ListView';
|
2017-12-08 19:13:21 +00:00
|
|
|
import * as actions from '../../actions';
|
2018-01-16 20:27:57 +00:00
|
|
|
import { openRoom, setLastOpen } from '../../actions/room';
|
2017-12-08 19:13:21 +00:00
|
|
|
import { editCancel } from '../../actions/messages';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import Message from '../../containers/message';
|
|
|
|
import MessageActions from '../../containers/MessageActions';
|
2017-12-13 15:00:26 +00:00
|
|
|
import MessageErrorActions from '../../containers/MessageErrorActions';
|
2017-12-08 19:13:21 +00:00
|
|
|
import MessageBox from '../../containers/MessageBox';
|
|
|
|
import Typing from '../../containers/Typing';
|
|
|
|
import Header from '../../containers/Header';
|
|
|
|
import RoomsHeader from './Header';
|
2018-01-09 17:12:55 +00:00
|
|
|
import Banner from './banner';
|
2017-12-08 19:13:21 +00:00
|
|
|
import styles from './styles';
|
2017-08-14 14:15:37 +00:00
|
|
|
|
2018-01-09 17:12:55 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1._id !== r2._id });
|
2017-12-08 19:13:21 +00:00
|
|
|
|
2017-11-24 17:21:21 +00:00
|
|
|
const typing = () => <Typing />;
|
2017-09-25 13:15:28 +00:00
|
|
|
@connect(
|
|
|
|
state => ({
|
2017-12-27 15:22:06 +00:00
|
|
|
Site_Url: state.settings.Site_Url || state.server ? state.server.server : '',
|
2017-09-25 13:15:28 +00:00
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
2017-12-02 13:19:58 +00:00
|
|
|
loading: state.messages.isFetching,
|
2018-01-16 20:27:57 +00:00
|
|
|
user: state.login.user
|
2017-09-25 13:15:28 +00:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
|
|
|
actions: bindActionCreators(actions, dispatch),
|
2017-11-24 20:44:52 +00:00
|
|
|
openRoom: room => dispatch(openRoom(room)),
|
2018-01-16 20:27:57 +00:00
|
|
|
editCancel: () => dispatch(editCancel()),
|
|
|
|
setLastOpen: date => dispatch(setLastOpen(date))
|
2017-09-25 13:15:28 +00:00
|
|
|
})
|
|
|
|
)
|
2017-08-05 18:16:32 +00:00
|
|
|
export default class RoomView extends React.Component {
|
|
|
|
static propTypes = {
|
2017-09-21 17:08:00 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2017-11-20 22:18:00 +00:00
|
|
|
openRoom: PropTypes.func.isRequired,
|
2018-01-16 20:27:57 +00:00
|
|
|
setLastOpen: PropTypes.func.isRequired,
|
2017-12-02 13:19:58 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: PropTypes.func,
|
2017-08-12 20:52:55 +00:00
|
|
|
rid: PropTypes.string,
|
2017-08-13 01:35:09 +00:00
|
|
|
name: PropTypes.string,
|
2017-08-14 00:03:33 +00:00
|
|
|
Site_Url: PropTypes.string,
|
2018-01-09 17:12:55 +00:00
|
|
|
Message_TimeFormat: PropTypes.string
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-05 18:16:32 +00:00
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
header: <Header subview={<RoomsHeader navigation={navigation} />} />
|
|
|
|
});
|
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-25 13:15:28 +00:00
|
|
|
this.rid =
|
|
|
|
props.rid ||
|
2017-11-28 17:47:56 +00:00
|
|
|
props.navigation.state.params.room.rid;
|
2017-12-01 15:06:56 +00:00
|
|
|
this.name = this.props.name ||
|
|
|
|
this.props.navigation.state.params.name ||
|
|
|
|
this.props.navigation.state.params.room.name;
|
2018-01-09 17:12:55 +00:00
|
|
|
this.opened = new Date();
|
|
|
|
this.data = database
|
|
|
|
.objects('messages')
|
2017-12-27 15:22:06 +00:00
|
|
|
.filtered('rid = $0', this.rid)
|
2017-09-25 13:15:28 +00:00
|
|
|
.sorted('ts', true);
|
2018-01-09 17:12:55 +00:00
|
|
|
const rowIds = this.data.map((row, index) => index);
|
2018-01-17 16:42:30 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
|
2017-08-07 18:42:02 +00:00
|
|
|
this.state = {
|
2018-01-09 17:12:55 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data, rowIds),
|
2017-08-10 16:49:15 +00:00
|
|
|
loaded: true,
|
2018-01-17 16:42:30 +00:00
|
|
|
joined: typeof props.rid === 'undefined',
|
|
|
|
readOnly: false
|
2017-08-07 18:42:02 +00:00
|
|
|
};
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2017-08-07 18:42:02 +00:00
|
|
|
componentWillMount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.props.navigation.setParams({
|
2017-12-01 15:06:56 +00:00
|
|
|
title: this.name
|
2017-09-21 17:08:00 +00:00
|
|
|
});
|
2018-01-17 16:42:30 +00:00
|
|
|
this.updateRoom();
|
2018-01-16 20:27:57 +00:00
|
|
|
this.props.openRoom({ rid: this.rid, name: this.name, ls: this.room.ls });
|
|
|
|
if (this.room.alert || this.room.unread || this.room.userMentions) {
|
|
|
|
this.props.setLastOpen(this.room.ls);
|
|
|
|
} else {
|
|
|
|
this.props.setLastOpen(null);
|
|
|
|
}
|
2017-08-17 06:28:41 +00:00
|
|
|
this.data.addListener(this.updateState);
|
2018-01-17 16:42:30 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
2018-01-09 17:12:55 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
return !(equal(this.props, nextProps) && equal(this.state, nextState));
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
2017-08-07 00:34:35 +00:00
|
|
|
componentWillUnmount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.data.removeAllListeners();
|
2017-11-24 20:44:52 +00:00
|
|
|
this.props.editCancel();
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
onEndReached = () => {
|
2017-09-25 13:15:28 +00:00
|
|
|
if (
|
2018-01-09 17:12:55 +00:00
|
|
|
// rowCount &&
|
2017-09-25 13:15:28 +00:00
|
|
|
this.state.loaded &&
|
|
|
|
this.state.loadingMore !== true &&
|
|
|
|
this.state.end !== true
|
|
|
|
) {
|
2017-08-10 23:21:46 +00:00
|
|
|
this.setState({
|
|
|
|
loadingMore: true
|
|
|
|
});
|
2018-01-09 17:12:55 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const lastRowData = this.data[this.data.length - 1];
|
|
|
|
if (!lastRowData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RocketChat.loadMessagesForRoom(this.rid, lastRowData.ts, ({ end }) => {
|
|
|
|
this.setState({
|
|
|
|
loadingMore: false,
|
|
|
|
end
|
|
|
|
});
|
2017-08-10 23:21:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-11-21 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 17:12:55 +00:00
|
|
|
updateState = debounce(() => {
|
|
|
|
const rowIds = this.data.map((row, index) => index);
|
2017-08-09 01:40:55 +00:00
|
|
|
this.setState({
|
2018-01-09 17:12:55 +00:00
|
|
|
dataSource: this.state.dataSource.cloneWithRows(this.data, rowIds)
|
2017-08-09 01:40:55 +00:00
|
|
|
});
|
2018-01-09 17:12:55 +00:00
|
|
|
}, 50);
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2018-01-17 16:42:30 +00:00
|
|
|
updateRoom = () => {
|
|
|
|
[this.room] = this.rooms;
|
|
|
|
this.setState({ readOnly: this.room.ro });
|
|
|
|
}
|
|
|
|
|
2018-01-16 20:27:57 +00:00
|
|
|
sendMessage = message => RocketChat.sendMessage(this.rid, message).then(() => {
|
|
|
|
this.props.setLastOpen(null);
|
|
|
|
});
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
joinRoom = async() => {
|
|
|
|
await RocketChat.joinRoom(this.props.rid);
|
|
|
|
this.setState({
|
|
|
|
joined: true
|
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
|
|
|
|
2018-01-09 17:12:55 +00:00
|
|
|
renderItem = item => (
|
2017-08-09 01:40:55 +00:00
|
|
|
<Message
|
2017-12-13 15:00:26 +00:00
|
|
|
key={item._id}
|
2017-08-09 01:40:55 +00:00
|
|
|
item={item}
|
2018-01-09 17:12:55 +00:00
|
|
|
animate={this.opened.toISOString() < item.ts.toISOString()}
|
2017-08-13 23:45:47 +00:00
|
|
|
baseUrl={this.props.Site_Url}
|
2017-08-14 00:03:33 +00:00
|
|
|
Message_TimeFormat={this.props.Message_TimeFormat}
|
2017-12-02 13:19:58 +00:00
|
|
|
user={this.props.user}
|
2017-08-09 01:40:55 +00:00
|
|
|
/>
|
|
|
|
);
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
2017-08-10 16:16:32 +00:00
|
|
|
|
|
|
|
renderFooter = () => {
|
|
|
|
if (!this.state.joined) {
|
2017-08-09 20:08:50 +00:00
|
|
|
return (
|
2017-08-10 16:16:32 +00:00
|
|
|
<View>
|
|
|
|
<Text>You are in preview mode.</Text>
|
|
|
|
<Button title='Join' onPress={this.joinRoom} />
|
2017-08-09 20:08:50 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-01-17 16:42:30 +00:00
|
|
|
if (this.state.readOnly) {
|
|
|
|
return (
|
|
|
|
<View style={styles.readOnly}>
|
|
|
|
<Text>This room is read only</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
return <MessageBox ref={box => (this.box = box)} onSubmit={this.sendMessage} rid={this.rid} />;
|
|
|
|
};
|
2017-08-09 20:08:50 +00:00
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
if (this.state.loadingMore) {
|
2017-11-19 02:29:46 +00:00
|
|
|
return <Text style={styles.loadingMore}>Loading more messages...</Text>;
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.end) {
|
2017-11-19 02:29:46 +00:00
|
|
|
return <Text style={styles.loadingMore}>Start of conversation</Text>;
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
2017-11-21 16:55:32 +00:00
|
|
|
}
|
2017-08-04 00:34:37 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-01-25 14:04:20 +00:00
|
|
|
<View style={styles.container}>
|
2018-01-09 17:12:55 +00:00
|
|
|
<Banner />
|
2017-11-14 17:01:16 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView}>
|
|
|
|
<ListView
|
|
|
|
enableEmptySections
|
|
|
|
style={styles.list}
|
2018-01-09 17:12:55 +00:00
|
|
|
onEndReachedThreshold={500}
|
2017-11-14 17:01:16 +00:00
|
|
|
renderFooter={this.renderHeader}
|
2017-11-24 17:21:21 +00:00
|
|
|
renderHeader={typing}
|
2017-11-14 17:01:16 +00:00
|
|
|
onEndReached={this.onEndReached}
|
|
|
|
dataSource={this.state.dataSource}
|
2018-01-09 17:12:55 +00:00
|
|
|
renderRow={item => this.renderItem(item)}
|
2017-11-14 17:01:16 +00:00
|
|
|
initialListSize={10}
|
2018-01-25 14:04:20 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
keyboardDismissMode='on-drag'
|
2017-11-14 17:01:16 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
2017-08-10 16:16:32 +00:00
|
|
|
{this.renderFooter()}
|
2017-11-24 20:44:52 +00:00
|
|
|
<MessageActions room={this.room} />
|
2017-12-13 15:00:26 +00:00
|
|
|
<MessageErrorActions />
|
2018-01-25 14:04:20 +00:00
|
|
|
{Platform.OS === 'ios' ? <KeyboardSpacer /> : null}
|
|
|
|
</View>
|
2017-08-04 00:34:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|