<commit_before>import React from 'react';
import CSSModules from 'react-css-modules';
import 'semantic-ui-css/semantic.min.css';
import ReactTooltip from 'react-tooltip';

import '../css/base.css';

import aoun from './aouuuuuuuuun.png'
import search from './search';
import css from './home.css';
import macros from './macros';
import ResultsLoader from './ResultsLoader';


// Home page component
class Home extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      results: [],

      // Value to set the search box to after the search box is rendered.
      // If the user navigates to a page, search for the query.
      searchTerm: this.getSearchQueryFromUrl(),

      // If we a waiting on a user on a slow computer to click enter to search.
      // On desktop, the data is searched every time, but it is only searched after you click enter on mobile.
      waitingOnEnter: false,
    };

    // Timer used to debounce search queries
    this.searchDebounceTimer = null;

    // Used in analytics to ensure you don't log the same query twice
    this.lastSearch = null;

    // Used in search to make sure you discard a result if the search requests did not come back in order
    this.currentQuery = null;

    // Reference to the raw DOM element of the input box.
    // Updated with react refs when the render function runs.
    this.inputElement = null;

    this.onClick = this.onClick.bind(this);
    this.onKeyDown = this.onKeyDown.bind(this);
    this.loadMore = this.loadMore.bind(this);
    this.onPopState = this.onPopState.bind(this);
    this.onSearchDebounced = this.onSearchDebounced.bind(this);

    // Count the number of times the user searched this session. Used for analytics. 
    this.searchCount = 0;

    // Log the initial search or pageview.
    this.logSearch(this.state.searchTerm);
  }

  getSearchQueryFromUrl() {
    return decodeURIComponent(macros.replaceAll(location.pathname.slice(1), '+', ' '))
  }

  // Runs when the user clicks back or forward in their browser.
  onPopState(event) {
    let query = this.getSearchQueryFromUrl()
    this.search(query)
    if (this.inputElement) {
      this.inputElement.value = query
    }
  }

  // Remove the listener when this component goes away. 
  // Even this component will never go away in prod, it can go away in dev due to HMR. 
  componentWillUnmount() {
    window.removeEventListener('onpopstate', this.onPopState);
  }

  logSearch(searchTerm) {
    searchTerm = searchTerm.trim();
    if (searchTerm === this.lastSearch) {
      console.log('Not logging because same as last search', searchTerm);
      return;
    }
    this.lastSearch = searchTerm;

    if (searchTerm) {
      this.searchCount ++;
      window.ga('send', 'pageview', `/?search=${searchTerm}`);
      window.amplitude.logEvent('Search', {'query': searchTerm, sessionCount: this.searchCount});
    } else {
      window.amplitude.logEvent('Homepage visit');
      window.ga('send', 'pageview', '/');
    }
  }<commit_msg>on a pop history event, only search for query if it exists<commit_after>import React from 'react';
import CSSModules from 'react-css-modules';
import 'semantic-ui-css/semantic.min.css';
import ReactTooltip from 'react-tooltip';

import '../css/base.css';

import aoun from './aouuuuuuuuun.png'
import search from './search';
import css from './home.css';
import macros from './macros';
import ResultsLoader from './ResultsLoader';


// Home page component
class Home extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      results: [],

      // Value to set the search box to after the search box is rendered.
      // If the user navigates to a page, search for the query.
      searchTerm: this.getSearchQueryFromUrl(),

      // If we a waiting on a user on a slow computer to click enter to search.
      // On desktop, the data is searched every time, but it is only searched after you click enter on mobile.
      waitingOnEnter: false,
    };

    // Timer used to debounce search queries
    this.searchDebounceTimer = null;

    // Used in analytics to ensure you don't log the same query twice
    this.lastSearch = null;

    // Used in search to make sure you discard a result if the search requests did not come back in order
    this.currentQuery = null;

    // Reference to the raw DOM element of the input box.
    // Updated with react refs when the render function runs.
    this.inputElement = null;

    this.onClick = this.onClick.bind(this);
    this.onKeyDown = this.onKeyDown.bind(this);
    this.loadMore = this.loadMore.bind(this);
    this.onPopState = this.onPopState.bind(this);
    this.onSearchDebounced = this.onSearchDebounced.bind(this);

    // Count the number of times the user searched this session. Used for analytics. 
    this.searchCount = 0;

    // Log the initial search or pageview.
    this.logSearch(this.state.searchTerm);
  }

  getSearchQueryFromUrl() {
    return decodeURIComponent(macros.replaceAll(location.pathname.slice(1), '+', ' '))
  }

  // Runs when the user clicks back or forward in their browser.
  onPopState() {
    let query = this.getSearchQueryFromUrl()

    // Only search if the query is longer than 0
    if (query) {
      this.search(query)
    }
    else {
      this.setState({
        results: [],
        searchTerm: query,
      });
    }

    if (this.inputElement) {
      this.inputElement.value = query
    }
  }

  // Remove the listener when this component goes away. 
  // Even this component will never go away in prod, it can go away in dev due to HMR. 
  componentWillUnmount() {
    window.removeEventListener('onpopstate', this.onPopState);
  }

  logSearch(searchTerm) {
    searchTerm = searchTerm.trim();
    if (searchTerm === this.lastSearch) {
      console.log('Not logging because same as last search', searchTerm);
      return;
    }
    this.lastSearch = searchTerm;

    if (searchTerm) {
      this.searchCount ++;
      window.ga('send', 'pageview', `/?search=${searchTerm}`);
      window.amplitude.logEvent('Search', {'query': searchTerm, sessionCount: this.searchCount});
    } else {
      window.amplitude.logEvent('Homepage visit');
      window.ga('send', 'pageview', '/');
    }
  }
{'fraction_non_alphanumeric': 0.10819881732459645, 'fraction_numerical': 0.000799105002397315, 'mean_word_length': 2.9859872611464966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2459298', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1782, 'n_words': 611}