<commit_before>import React, { useCallback, useState } from 'react';
import { withRouter } from 'react-router';
import { KEY_RETURN, KEY_ESCAPE } from 'keycode-js';
import { useSelector } from 'react-redux';

export default function SearchForm() {
  const query = useSelector((state) => {
    const { pathname, query } = state.routing.locationBeforeTransitions;
    return (pathname === '/search' && query.qs) || '';
  });
  return <SearchInput query={query} key={query} />;
}

const SearchInput = withRouter(function SearchInput({ router, query: initialQuery }) {
  const [query, setQuery] = useState(initialQuery);

  const performSearch = useCallback(
    () => query.trim() !== '' && router.push(`/search?qs=${encodeURIComponent(query.trim())}`),
    [query, router],
  );

  const onChange = useCallback(({ target }) => setQuery(target.value), []);

  const onKeyDown = useCallback(
    ({ target, keyCode }) => {
      if (keyCode === KEY_ESCAPE) {
        target.blur();
        setQuery(initialQuery);
      }
      if (keyCode === KEY_RETURN) {
        target.blur();
        performSearch();
      }
    },
    [performSearch, initialQuery],
  );

  return (
    <div className="search-form">
      <input
        value={query}
        onChange={onChange}
        onKeyDown={onKeyDown}
        placeholder="Search request"
        className="search-input"
      />
      <button type="button" className="search-button" onClick={performSearch}>
        Search
      </button>
    </div>
  );
});
<commit_msg>Set default search query on top search input field<commit_after>import React, { useCallback, useState, useMemo, useEffect } from 'react';
import { withRouter } from 'react-router';
import { KEY_RETURN, KEY_ESCAPE } from 'keycode-js';
import { useSearchQuery } from './hooks/search-query';

export default withRouter(function SearchForm({ router }) {
  const urlQuery = useSearchQuery();
  const preQuery = useMemo(() => {
    const route = router.routes[router.routes.length - 1];
    switch (route.name) {
      case 'saves':
        return `in-my:saves `;
      case 'discussions':
        return `in-my:discussions `;
      case 'direct':
        return `in-my:directs `;
      case 'userLikes':
        return `liked-by:${router.params.userName} `;
      case 'userComments':
        return `commented-by:${router.params.userName} `;
      case 'userFeed':
        return `in:${router.params.userName} `;
      default:
        return '';
    }
  }, [router.routes, router.params]);

  const initialQuery = urlQuery || preQuery;

  const [query, setQuery] = useState(initialQuery);

  // Handle navigation
  useEffect(() => setQuery(initialQuery), [initialQuery]);

  const performSearch = useCallback(
    () => query.trim() !== '' && router.push(`/search?qs=${encodeURIComponent(query.trim())}`),
    [query, router],
  );

  const onChange = useCallback(({ target }) => setQuery(target.value), []);

  const onKeyDown = useCallback(
    ({ target, keyCode }) => {
      if (keyCode === KEY_ESCAPE) {
        target.blur();
        setQuery(initialQuery);
      }
      if (keyCode === KEY_RETURN) {
        target.blur();
        performSearch();
      }
    },
    [performSearch, initialQuery],
  );

  return (
    <div className="search-form">
      <input
        value={query}
        onChange={onChange}
        onKeyDown={onKeyDown}
        placeholder="Search request"
        className="search-input"
      />
      <button type="button" className="search-button" onClick={performSearch}>
        Search
      </button>
    </div>
  );
});

{'fraction_non_alphanumeric': 0.15556799104644656, 'fraction_numerical': 0.0002797985450475658, 'mean_word_length': 2.989955357142857, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 23, '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': '6176946', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1064, 'n_words': 228}