<commit_before>/* @flow */
import React, { PureComponent } from 'react';
import { ActivityIndicator, StyleSheet, View } from 'react-native';
import throttle from 'lodash.throttle';
import { ActionSheetProvider } from '@expo/react-native-action-sheet';

import type { Actions, Auth, Message } from '../types';
import { SearchEmptyState } from '../common';
import { BRAND_COLOR } from '../styles';
import { searchNarrow } from '../utils/narrow';
import MessageList from '../message/MessageList';
import { getMessages } from '../api';
import renderMessages from '../message/renderMessages';

const styles = StyleSheet.create({
  results: {
    flex: 1,
  },
  activity: {
    padding: 8,
  },
});

type Props = {
  actions: Actions,
  query: string,
  auth: Auth,
};

type State = {
  messages: Message[],
  isFetching: boolean,
  query: string,
};

export default class SearchMessagesCard extends PureComponent<Props, State> {
  props: Props;
  state: State;

  state = {
    messages: [],
    isFetching: false,
    query: '',
  };

  handleQueryChange = async (query: string) => {
    const { auth } = this.props;

    throttle(async () => {
      this.setState({ isFetching: true });
      const messages = await getMessages(
        auth,
        searchNarrow(query),
        Number.MAX_SAFE_INTEGER,
        20,
        0,
        false,
      );
      this.setState({
        messages,
        isFetching: false,
      });
    }, 500).call(this);
  };

  componentWillReceiveProps(nextProps: Props) {
    if (nextProps.query !== this.props.query) {
      this.handleQueryChange(nextProps.query);
    }
  }

  render() {
    const { isFetching, messages, query } = this.state;
    const { actions } = this.props;
    const noResults = !!query && !isFetching && !messages.length;

    if (noResults) {
      return <SearchEmptyState text="No results" />;
    }

    const renderedMessages = renderMessages(messages, []);

    return (
      <View style={styles.results}>
        {isFetching && (
          <ActivityIndicator style={styles.activity} color={BRAND_COLOR} size="large" />
        )}
        <ActionSheetProvider>
          <MessageList
            actions={actions}
            renderedMessages={renderedMessages}
            fetchingOlder={false}
            fetchingNewer={isFetching}
            caughtUpOlder
            caughtUpNewer
            singleFetchProgress
          />
        </ActionSheetProvider>
      </View>
    );
  }
}<commit_msg>Show Zulip logo with revolving border as loading indicator in SearchMessageScreen. (#1437)<commit_after>/* @flow */
import React, { PureComponent } from 'react';
import { StyleSheet, View } from 'react-native';
import throttle from 'lodash.throttle';
import { ActionSheetProvider } from '@expo/react-native-action-sheet';

import type { Actions, Auth, Message } from '../types';
import { SearchEmptyState } from '../common';
import { searchNarrow } from '../utils/narrow';
import MessageList from '../message/MessageList';
import { getMessages } from '../api';
import renderMessages from '../message/renderMessages';

const styles = StyleSheet.create({
  results: {
    flex: 1,
  },
});

type Props = {
  actions: Actions,
  query: string,
  auth: Auth,
};

type State = {
  messages: Message[],
  isFetching: boolean,
  query: string,
};

export default class SearchMessagesCard extends PureComponent<Props, State> {
  props: Props;
  state: State;

  state = {
    messages: [],
    isFetching: false,
    query: '',
  };

  handleQueryChange = async (query: string) => {
    const { auth } = this.props;

    throttle(async () => {
      this.setState({ isFetching: true });
      const messages = await getMessages(
        auth,
        searchNarrow(query),
        Number.MAX_SAFE_INTEGER,
        20,
        0,
        false,
      );
      this.setState({
        messages,
        isFetching: false,
      });
    }, 500).call(this);
  };

  componentWillReceiveProps(nextProps: Props) {
    if (nextProps.query !== this.props.query) {
      this.handleQueryChange(nextProps.query);
    }
  }

  render() {
    const { isFetching, messages, query } = this.state;
    const { actions } = this.props;
    const noResults = !!query && !isFetching && !messages.length;

    if (noResults) {
      return <SearchEmptyState text="No results" />;
    }

    const renderedMessages = renderMessages(messages, []);

    return (
      <View style={styles.results}>
        <ActionSheetProvider>
          <MessageList
            actions={actions}
            renderedMessages={renderedMessages}
            fetchingOlder={isFetching}
            fetchingNewer={isFetching}
            caughtUpOlder
            caughtUpNewer
            singleFetchProgress
          />
        </ActionSheetProvider>
      </View>
    );
  }
}
{'fraction_non_alphanumeric': 0.13088786994581075, 'fraction_numerical': 0.003959983326385994, 'mean_word_length': 2.616428033157498, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6793612', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1403, 'n_words': 332}