import React from 'react'; import PropTypes from 'prop-types'; import { View, FlatList, Text } from 'react-native'; import { connect } from 'react-redux'; import { SafeAreaView } from 'react-navigation'; import equal from 'deep-equal'; import RCTextInput from '../../containers/TextInput'; import ActivityIndicator from '../../containers/ActivityIndicator'; import styles from './styles'; import Markdown from '../../containers/markdown'; import debounce from '../../utils/debounce'; import RocketChat from '../../lib/rocketchat'; import Message from '../../containers/message/Message'; import scrollPersistTaps from '../../utils/scrollPersistTaps'; import I18n from '../../i18n'; import StatusBar from '../../containers/StatusBar'; import log from '../../utils/log'; import { themes } from '../../constants/colors'; import { withTheme } from '../../theme'; import { themedHeader } from '../../utils/navigation'; class SearchMessagesView extends React.Component { static navigationOptions = ({ screenProps }) => ({ title: I18n.t('Search'), ...themedHeader(screenProps.theme) }) static propTypes = { navigation: PropTypes.object, user: PropTypes.object, baseUrl: PropTypes.string, customEmojis: PropTypes.object, theme: PropTypes.string } constructor(props) { super(props); this.state = { loading: false, messages: [], searchText: '' }; this.rid = props.navigation.getParam('rid'); } shouldComponentUpdate(nextProps, nextState) { const { loading, searchText, messages } = this.state; const { theme } = this.props; if (nextProps.theme !== theme) { return true; } if (nextState.loading !== loading) { return true; } if (nextState.searchText !== searchText) { return true; } if (!equal(nextState.messages, messages)) { return true; } return false; } componentWillUnmount() { this.search.stop(); } // eslint-disable-next-line react/sort-comp search = debounce(async(searchText) => { this.setState({ searchText, loading: true, messages: [] }); try { const result = await RocketChat.searchMessages(this.rid, searchText); if (result.success) { this.setState({ messages: result.messages || [], loading: false }); } } catch (e) { this.setState({ loading: false }); log(e); } }, 1000) getCustomEmoji = (name) => { const { customEmojis } = this.props; const emoji = customEmojis[name]; if (emoji) { return emoji; } return null; } renderEmpty = () => { const { theme } = this.props; return ( {I18n.t('No_results_found')} ); } renderItem = ({ item }) => { const { user, baseUrl, theme } = this.props; return ( {}} getCustomEmoji={this.getCustomEmoji} theme={theme} /> ); } renderList = () => { const { messages, loading, searchText } = this.state; const { theme } = this.props; if (!loading && messages.length === 0 && searchText.length) { return this.renderEmpty(); } return ( item._id} onEndReached={this.load} ListFooterComponent={loading ? : null} {...scrollPersistTaps} /> ); } render() { const { theme } = this.props; return ( {this.renderList()} ); } } const mapStateToProps = state => ({ baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', 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 }, customEmojis: state.customEmojis }); export default connect(mapStateToProps)(withTheme(SearchMessagesView));