<commit_before>const React = require("react");
const { DOM: dom, PropTypes, createFactory } = React;
const { connect } = require("react-redux");
const { bindActionCreators } = require("redux");
const actions = require("../actions");
const {
  getSources,
  getSelectedSource,
  getFileSearchState
} = require("../selectors");
const { endTruncateStr } = require("../utils/utils");
const { parse: parseURL } = require("url");
const { isPretty } = require("../utils/source");

require("./SourceSearch.css");

const Autocomplete = createFactory(require("./shared/Autocomplete"));

function searchResults(sources) {
  function getSourcePath(source) {
    const { path, href } = parseURL(source.get("url"));
    // for URLs like "about:home" the path is null so we pass the full href
    return (path || href);
  }

  return sources.valueSeq()
    .filter(source => !isPretty(source.toJS()) && source.get("url"))
    .map(source => ({
      value: getSourcePath(source),
      title: getSourcePath(source).split("/").pop(),
      subtitle: endTruncateStr(getSourcePath(source), 100),
      id: source.get("id")
    }))
    .toJS();
}

const Search = React.createClass({
  propTypes: {
    sources: PropTypes.object,
    selectSource: PropTypes.func,
    selectedSource: PropTypes.object,
    toggleFileSearch: PropTypes.func,
    searchOn: PropTypes.bool
  },

  contextTypes: {
    shortcuts: PropTypes.object
  },

  displayName: "Search",

  getInitialState() {
    return {
      inputValue: ""
    };
  },

  componentWillUnmount() {
    const shortcuts = this.context.shortcuts;
    shortcuts.off(
      `CmdOrCtrl+${L10N.getStr("sources.search.key")}`,
      this.toggle
    );
    shortcuts.off(
      `CmdOrCtrl+${L10N.getStr("sources.searchAlt.key")}`,
      this.toggle
    );
    shortcuts.off("Escape", this.onEscape);
  },

  componentDidMount() {
    const shortcuts = this.context.shortcuts;
    shortcuts.on(
      `CmdOrCtrl+${L10N.getStr("sources.search.key")}`,
      this.toggle
    );
    shortcuts.on(
      `CmdOrCtrl+${L10N.getStr("sources.searchAlt.key")}`,
      this.toggle
    );
    shortcuts.on("Escape", this.onEscape);
  },

  toggle(key, e) {
    e.preventDefault();
    this.props.toggleFileSearch(!this.props.searchOn);
  },

  onEscape(shortcut, e) {
    if (this.props.searchOn) {
      e.preventDefault();
      this.setState({ inputValue: "" });
      this.props.toggleFileSearch(false);
    }
  },

  close(inputValue = "") {
    this.setState({ inputValue });
    this.props.toggleFileSearch(false);
  },

  render() {
    return this.props.searchOn ?
      dom.div({ className: "search-container" },
      Autocomplete({
        selectItem: result => {<commit_msg>Refactored code<commit_after>const React = require("react");
const { DOM: dom, PropTypes, createFactory } = React;
const { connect } = require("react-redux");
const { bindActionCreators } = require("redux");
const actions = require("../actions");
const {
  getSources,
  getSelectedSource,
  getFileSearchState
} = require("../selectors");
const { endTruncateStr } = require("../utils/utils");
const { parse: parseURL } = require("url");
const { isPretty } = require("../utils/source");

require("./SourceSearch.css");

const Autocomplete = createFactory(require("./shared/Autocomplete"));

function searchResults(sources) {
  function getSourcePath(source) {
    const { path, href } = parseURL(source.get("url"));
    // for URLs like "about:home" the path is null so we pass the full href
    return (path || href);
  }

  return sources.valueSeq()
    .filter(source => !isPretty(source.toJS()) && source.get("url"))
    .map(source => ({
      value: getSourcePath(source),
      title: getSourcePath(source).split("/").pop(),
      subtitle: endTruncateStr(getSourcePath(source), 100),
      id: source.get("id")
    }))
    .toJS();
}

const Search = React.createClass({
  propTypes: {
    sources: PropTypes.object,
    selectSource: PropTypes.func,
    selectedSource: PropTypes.object,
    toggleFileSearch: PropTypes.func,
    searchOn: PropTypes.bool
  },

  contextTypes: {
    shortcuts: PropTypes.object
  },

  displayName: "Search",

  getInitialState() {
    return {
      inputValue: ""
    };
  },

  componentWillUnmount() {
    const shortcuts = this.context.shortcuts;
    const searchKeys = [
      L10N.getStr("sources.searchAlt.key"), 
      L10N.getStr("sources.search.key")
    ];
    searchKeys.forEach(key => shortcuts.off(`CmdOrCtrl+${key}`, this.toggle));
    shortcuts.off("Escape", this.onEscape);
  },

  componentDidMount() {
    const shortcuts = this.context.shortcuts;
    const searchKeys = [
      L10N.getStr("sources.searchAlt.key"), 
      L10N.getStr("sources.search.key")
    ];
    searchKeys.forEach(key => shortcuts.on(`CmdOrCtrl+${key}`, this.toggle));
    shortcuts.on("Escape", this.onEscape);
  },

  toggle(key, e) {
    e.preventDefault();
    this.props.toggleFileSearch(!this.props.searchOn);
  },

  onEscape(shortcut, e) {
    if (this.props.searchOn) {
      e.preventDefault();
      this.setState({ inputValue: "" });
      this.props.toggleFileSearch(false);
    }
  },

  close(inputValue = "") {
    this.setState({ inputValue });
    this.props.toggleFileSearch(false);
  },

  render() {
    return this.props.searchOn ?
      dom.div({ className: "search-container" },
      Autocomplete({
        selectItem: result => {
{'fraction_non_alphanumeric': 0.1632350213318494, 'fraction_numerical': 0.004080875533296235, 'mean_word_length': 3.3980424143556283, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7533349', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1709, 'n_words': 301}