All files / app/containers Search.tsx

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                        2x                                                   2x   2x 4x 4x 4x 4x 4x 4x 4x       4x                                     4x 4x                     4x   12x                                                 2x                                                                                                 4x 4x 4x          
import React, { useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
 
import { style } from 'app/styles';
import { setSearch } from 'app/actions';
import { SearchInput } from 'app/components/SearchInput';
import { Popup } from 'app/components/Popup';
import { Selectable } from 'app/components/Selectable';
 
import { debounce } from 'app/utilities/debounce';
import { getSearch } from 'app/selectors/stateVars';
 
const styles = {
  container: {
    alignSelf: 'flex-end',
    marginBottom: '@margins.medium+px',
    overflow: 'visible',
    position: 'relative',
    width: 280,
  },
  searchInput: {
    borderRadius: 10,
    marginLeft: 10,
    marginTop: 0,
    width: 'calc(100% - 22px)',
  },
  searchInputPopupOpen: {
    borderBottomRightRadius: 0,
  },
  popup: {
    border: 'solid 2px @colors.selectable',
    borderRightWidth: '1px',
    borderTop: 'none',
    marginRight: 0,
    marginTop: '-2px',
  },
};
 
const suggestedSearchPrefixes = ['author:', 'commit:', 'file:'];
 
export const Search: React.FC = (): React.ReactElement => {
  const [searchHasFocus, setSearchHasFocus] = useState(false);
  const [selectedSuggestion, setSelectedSuggestion] = useState('all');
  const search = useSelector(getSearch);
  const dispatch = useDispatch();
  const debouncedOnBlur = debounce(onSearchBlur, 200);
  const isPopupOpen = searchHasFocus && search && search.trim().length > 0;
  const searchInputStyle = isPopupOpen
    ? [styles.searchInput, styles.searchInputPopupOpen]
    : styles.searchInput;
 
  return (
    <div style={style(styles.container)}>
      <SearchInput
        value={search}
        onChange={onSearch}
        onClear={onClear}
        onFocus={onSearchFocus}
        onBlur={debouncedOnBlur}
        onKeyDown={onKeyboard}
        style={style(searchInputStyle)}
        placeholder="search authors, files or commits"
      />
      <Popup isOpen={isPopupOpen} style={style(styles.popup)} noBackdrop>
        {renderPopupSuggestions()}
      </Popup>
    </div>
  );
 
  function renderPopupSuggestions(): JSX.Element[] {
    const unPrefixedSearch = getUnprefixedSearch();
    const menuItems: JSX.Element[] = [
      <Selectable
        value="all"
        key="searchSuggestion_all"
        onClick={onSuggestClick}
        selected={selectedSuggestion === 'all'}
      >
        <div>Search everywhere for '{unPrefixedSearch}'</div>
      </Selectable>,
    ];
 
    return menuItems.concat(
      suggestedSearchPrefixes.map((prefix, index) => {
        return (
          <Selectable
            key={`searchSuggestion_${index}`}
            value={prefix}
            onClick={onSuggestClick}
            selected={selectedSuggestion === prefix}
          >
            <div style={style('normalText')}>
              {prefix} {unPrefixedSearch}
            </div>
          </Selectable>
        );
      })
    );
  }
 
  function onSearch(value) {
    dispatch(setSearch(value || ''));
  }
 
  function onClear() {
    dispatch(setSearch(''));
  }
 
  function onSearchFocus() {
    setSearchHasFocus(true);
  }
 
  function onSearchBlur() {
    setSearchHasFocus(false);
  }
 
  function onSuggestClick(_evt, value) {
    selectSuggestion(value);
  }
 
  function selectSuggestion(value) {
    const newSearch =
      value === 'all'
        ? getUnprefixedSearch()
        : `${value}${getUnprefixedSearch()}`;
    dispatch(setSearch(newSearch));
    setSelectedSuggestion(value);
  }
 
  function onKeyboard(evt) {
    const currentPrefixIndex =
      selectedSuggestion === 'all'
        ? -1
        : suggestedSearchPrefixes.indexOf(selectedSuggestion);
 
    if (evt.key === 'ArrowDown') {
      evt.preventDefault();
      const newSelection =
        currentPrefixIndex < suggestedSearchPrefixes.length - 1
          ? suggestedSearchPrefixes[currentPrefixIndex + 1]
          : 'all';
      setSelectedSuggestion(newSelection);
    } else if (evt.key === 'ArrowUp') {
      evt.preventDefault();
      const newSelection =
        currentPrefixIndex === 0
          ? 'all'
          : currentPrefixIndex > 0
            ? suggestedSearchPrefixes[currentPrefixIndex - 1]
            : suggestedSearchPrefixes[suggestedSearchPrefixes.length - 1];
      setSelectedSuggestion(newSelection);
    } else if (evt.key === 'Enter') {
      selectSuggestion(selectedSuggestion);
      return;
    }
  }
 
  function getUnprefixedSearch() {
    const matches = search && search.match(/^[^\:]*\:(.*)/);
    Eif (!matches || matches.length < 2) {
      return search;
    }
    return matches[1];
  }
};