2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-12-12 15:15:10 +00:00
|
|
|
import { View, FlatList, Text } from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
import RCTextInput from '../../containers/TextInput';
|
2019-12-11 23:01:12 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2018-04-24 19:34:03 +00:00
|
|
|
import styles from './styles';
|
2019-09-03 19:27:57 +00:00
|
|
|
import Markdown from '../../containers/markdown';
|
2018-04-24 19:34:03 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-05-25 20:54:27 +00:00
|
|
|
import Message from '../../containers/message';
|
2018-04-24 19:34:03 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-05-28 16:18:46 +00:00
|
|
|
import log from '../../utils/log';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2020-02-19 19:43:47 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-07-06 19:01:35 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import { CloseModalButton } from '../../containers/HeaderButton';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
class SearchMessagesView extends React.Component {
|
2020-07-06 19:01:35 +00:00
|
|
|
static navigationOptions = ({ navigation, route }) => {
|
|
|
|
const options = {
|
|
|
|
title: I18n.t('Search')
|
|
|
|
};
|
|
|
|
const showCloseModal = route.params?.showCloseModal;
|
|
|
|
if (showCloseModal) {
|
|
|
|
options.headerLeft = () => <CloseModalButton navigation={navigation} />;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-07-06 19:01:35 +00:00
|
|
|
route: PropTypes.object,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.object,
|
2019-10-07 20:56:30 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-12-11 23:01:12 +00:00
|
|
|
customEmojis: PropTypes.object,
|
|
|
|
theme: PropTypes.string
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.state = {
|
2018-12-12 15:15:10 +00:00
|
|
|
loading: false,
|
2018-04-24 19:34:03 +00:00
|
|
|
messages: [],
|
2018-12-12 15:15:10 +00:00
|
|
|
searchText: ''
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2020-07-06 19:01:35 +00:00
|
|
|
this.rid = props.route.params?.rid;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { loading, searchText, messages } = this.state;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.searchText !== searchText) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.messages, messages)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
componentWillUnmount() {
|
2018-12-12 15:15:10 +00:00
|
|
|
this.search.stop();
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
search = debounce(async(searchText) => {
|
|
|
|
this.setState({ searchText, loading: true, messages: [] });
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-04-08 12:35:28 +00:00
|
|
|
const result = await RocketChat.searchMessages(this.rid, searchText);
|
2018-12-12 15:15:10 +00:00
|
|
|
if (result.success) {
|
|
|
|
this.setState({
|
|
|
|
messages: result.messages || [],
|
|
|
|
loading: false
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2019-09-03 19:27:57 +00:00
|
|
|
} catch (e) {
|
2018-12-12 15:15:10 +00:00
|
|
|
this.setState({ loading: false });
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-12-12 15:15:10 +00:00
|
|
|
}, 1000)
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
getCustomEmoji = (name) => {
|
|
|
|
const { customEmojis } = this.props;
|
|
|
|
const emoji = customEmojis[name];
|
|
|
|
if (emoji) {
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-19 19:43:47 +00:00
|
|
|
navToRoomInfo = (navParam) => {
|
|
|
|
const { navigation, user } = this.props;
|
|
|
|
if (navParam.rid === user.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
navigation.navigate('RoomInfoView', navParam);
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
renderEmpty = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={[styles.listEmptyContainer, { backgroundColor: themes[theme].backgroundColor }]}>
|
|
|
|
<Text style={[styles.noDataFound, { color: themes[theme].titleText }]}>{I18n.t('No_results_found')}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
2019-12-11 23:01:12 +00:00
|
|
|
const { user, baseUrl, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<Message
|
2020-05-25 20:54:27 +00:00
|
|
|
item={item}
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-09-25 19:28:42 +00:00
|
|
|
user={user}
|
2018-12-12 15:15:10 +00:00
|
|
|
timeFormat='MMM Do YYYY, h:mm:ss a'
|
2019-05-20 20:43:50 +00:00
|
|
|
isHeader
|
2020-01-29 20:20:36 +00:00
|
|
|
showAttachment={() => {}}
|
2019-10-07 20:56:30 +00:00
|
|
|
getCustomEmoji={this.getCustomEmoji}
|
2020-02-19 19:43:47 +00:00
|
|
|
navToRoomInfo={this.navToRoomInfo}
|
2019-12-11 23:01:12 +00:00
|
|
|
theme={theme}
|
2018-12-12 15:15:10 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderList = () => {
|
|
|
|
const { messages, loading, searchText } = this.state;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { theme } = this.props;
|
2018-12-12 15:15:10 +00:00
|
|
|
|
|
|
|
if (!loading && messages.length === 0 && searchText.length) {
|
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
2019-12-11 23:01:12 +00:00
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
2018-12-12 15:15:10 +00:00
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.load}
|
2019-12-11 23:01:12 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
2018-12-12 15:15:10 +00:00
|
|
|
{...scrollPersistTaps}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
render() {
|
2019-12-11 23:01:12 +00:00
|
|
|
const { theme } = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2020-07-06 19:01:35 +00:00
|
|
|
<SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='search-messages-view' theme={theme}>
|
2019-12-11 23:01:12 +00:00
|
|
|
<StatusBar theme={theme} />
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.searchContainer}>
|
|
|
|
<RCTextInput
|
2019-08-20 19:24:22 +00:00
|
|
|
autoFocus
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Search')}
|
2018-12-12 15:15:10 +00:00
|
|
|
onChangeText={this.search}
|
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'
|
2019-12-11 23:01:12 +00:00
|
|
|
theme={theme}
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2019-12-11 23:01:12 +00:00
|
|
|
<Markdown msg={I18n.t('You_can_search_using_RegExp_eg')} username='' baseUrl='' theme={theme} />
|
|
|
|
<View style={[styles.divider, { backgroundColor: themes[theme].separatorColor }]} />
|
2018-04-24 19:34:03 +00:00
|
|
|
</View>
|
2018-12-12 15:15:10 +00:00
|
|
|
{this.renderList()}
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 19:24:22 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-19 19:43:47 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2019-10-07 20:56:30 +00:00
|
|
|
customEmojis: state.customEmojis
|
2019-08-20 19:24:22 +00:00
|
|
|
});
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(SearchMessagesView));
|