2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { View, FlatList } from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
import LoggedView from '../View';
|
|
|
|
import RCTextInput from '../../containers/TextInput';
|
|
|
|
import RCActivityIndicator from '../../containers/ActivityIndicator';
|
|
|
|
import styles from './styles';
|
|
|
|
import Markdown from '../../containers/message/Markdown';
|
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import buildMessage from '../../lib/methods/helpers/buildMessage';
|
|
|
|
import Message from '../../containers/message';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
2018-07-10 13:40:32 +00:00
|
|
|
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
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-24 19:34:03 +00:00
|
|
|
export default class SearchMessagesView extends LoggedView {
|
2018-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
|
|
|
topBar: {
|
|
|
|
title: {
|
|
|
|
text: I18n.t('Search')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super('SearchMessagesView', props);
|
|
|
|
this.limit = 0;
|
|
|
|
this.state = {
|
|
|
|
messages: [],
|
|
|
|
searching: false,
|
|
|
|
loadingMore: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.name.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.onChangeSearch.stop();
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
onChangeSearch = debounce((search) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { searching } = this.state;
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
this.searchText = search;
|
|
|
|
this.limit = 0;
|
2018-09-25 19:28:42 +00:00
|
|
|
if (!searching) {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.setState({ searching: true });
|
|
|
|
}
|
|
|
|
this.search();
|
|
|
|
}, 1000)
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
search = async() => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { rid } = this.props;
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (this._cancel) {
|
|
|
|
this._cancel('cancel');
|
|
|
|
}
|
|
|
|
const cancel = new Promise((r, reject) => this._cancel = reject);
|
|
|
|
let messages = [];
|
|
|
|
try {
|
2018-09-25 19:28:42 +00:00
|
|
|
const result = await Promise.race([RocketChat.messageSearch(this.searchText, rid, this.limit), cancel]);
|
2018-05-18 17:55:08 +00:00
|
|
|
messages = result.message.docs.map(message => buildMessage(message));
|
2018-04-24 19:34:03 +00:00
|
|
|
this.setState({ messages, searching: false, loadingMore: false });
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2018-04-24 19:34:03 +00:00
|
|
|
this._cancel = null;
|
2018-05-18 17:55:08 +00:00
|
|
|
if (e !== 'cancel') {
|
2018-04-24 19:34:03 +00:00
|
|
|
return this.setState({ searching: false, loadingMore: false });
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
log('SearchMessagesView.search', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moreData = () => {
|
|
|
|
const { loadingMore, messages } = this.state;
|
|
|
|
if (messages.length < this.limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.searchText && !loadingMore) {
|
|
|
|
this.setState({ loadingMore: true });
|
|
|
|
this.limit += 20;
|
|
|
|
this.search();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { user } = this.props;
|
|
|
|
return (
|
|
|
|
<Message
|
|
|
|
item={item}
|
|
|
|
style={styles.message}
|
|
|
|
reactions={item.reactions}
|
|
|
|
user={user}
|
|
|
|
customTimeFormat='MMMM Do YYYY, h:mm:ss a'
|
|
|
|
onReactionPress={async(emoji) => {
|
|
|
|
try {
|
|
|
|
await RocketChat.setReaction(emoji, item._id);
|
|
|
|
this.search();
|
|
|
|
this.forceUpdate();
|
|
|
|
} catch (e) {
|
|
|
|
log('SearchMessagesView.onReactionPress', e);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { searching, loadingMore, messages } = this.state;
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='search-messages-view' forceInset={{ bottom: 'never' }}>
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.searchContainer}>
|
|
|
|
<RCTextInput
|
|
|
|
inputRef={(e) => { this.name = e; }}
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Search')}
|
2018-04-24 19:34:03 +00:00
|
|
|
onChangeText={this.onChangeSearch}
|
2018-06-01 17:38:13 +00:00
|
|
|
placeholder={I18n.t('Search_Messages')}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='search-message-view-input'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2018-09-11 16:32:52 +00:00
|
|
|
<Markdown msg={I18n.t('You_can_search_using_RegExp_eg')} username='' baseUrl='' customEmojis={{}} />
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.divider} />
|
|
|
|
</View>
|
|
|
|
<FlatList
|
2018-09-25 19:28:42 +00:00
|
|
|
data={messages}
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.moreData}
|
2018-06-13 01:33:00 +00:00
|
|
|
ListHeaderComponent={searching ? <RCActivityIndicator /> : null}
|
|
|
|
ListFooterComponent={loadingMore ? <RCActivityIndicator /> : null}
|
2018-04-24 19:34:03 +00:00
|
|
|
{...scrollPersistTaps}
|
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|