<commit_before>/*
 * Copyright 2018 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React from 'react';
import { compose, lifecycle } from 'recompose';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import ReactDOM from 'react-dom';
import { Input, Button, Header, Icon, Modal, Menu } from 'semantic-ui-react';

import Mousetrap from 'mousetrap';

import { actionCreators as recentItemsActionCreators } from './redux';
import openDocRef from './openDocRef';

const {
  recentItemsClosed,
  recentItemsSelectionUp,
  recentItemsSelectionDown,
} = recentItemsActionCreators;

const enhance = compose(
  withRouter,
  connect(
    (state, props) => ({
      isOpen: state.recentItems.isOpen,
      openItemStack: state.recentItems.openItemStack,
      topItem: state.recentItems.openItemStack,
      selectedItem: state.recentItems.selectedItem,
      selectedDocRef: state.recentItems.selectedDocRef,
    }),
    {
      recentItemsClosed,
      openDocRef,
      recentItemsSelectionUp,
      recentItemsSelectionDown,
    },
  ),
  lifecycle({
    componentDidMount() {
      const {
        recentItemsSelectionUp,
        recentItemsSelectionDown,
        openDocRef,
        recentItemsClosed,
        history,
        openItemStack,
      } = this.props;

      Mousetrap.bind(['k', 'up'], () => recentItemsSelectionUp());
      Mousetrap.bind(['j', 'down'], () => recentItemsSelectionDown());
      Mousetrap.bind('enter', () => {
        if (this.props.selectedDocRef !== undefined) {
          openDocRef(history, this.props.selectedDocRef);
          recentItemsClosed();
        } else if (openItemStack.length > 0) {
          openDocRef(history, openItemStack[0]);
          recentItemsClosed();
        }
      });
    },
    componentWillUnmount() {
      Mousetrap.unbind(['k', 'up']);
      Mousetrap.unbind(['j', 'down']);
      Mousetrap.unbind('enter');
    },<commit_msg>Add key binding<commit_after>/*
 * Copyright 2018 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React from 'react';
import { compose, lifecycle } from 'recompose';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import ReactDOM from 'react-dom';
import { Input, Button, Header, Icon, Modal, Menu } from 'semantic-ui-react';

import Mousetrap from 'mousetrap';

import { actionCreators as recentItemsActionCreators } from './redux';
import openDocRef from './openDocRef';

const {
  recentItemsClosed,
  recentItemsSelectionUp,
  recentItemsSelectionDown,
} = recentItemsActionCreators;

const upKeys = ['k', 'ctrl+k', 'up'];
const downKeys = ['j', 'ctrl+j', 'down'];
const openKeys = ['enter'];

const enhance = compose(
  withRouter,
  connect(
    (state, props) => ({
      isOpen: state.recentItems.isOpen,
      openItemStack: state.recentItems.openItemStack,
      topItem: state.recentItems.openItemStack,
      selectedItem: state.recentItems.selectedItem,
      selectedDocRef: state.recentItems.selectedDocRef,
    }),
    {
      recentItemsClosed,
      openDocRef,
      recentItemsSelectionUp,
      recentItemsSelectionDown,
    },
  ),
  lifecycle({
    componentDidMount() {
      const {
        recentItemsSelectionUp,
        recentItemsSelectionDown,
        openDocRef,
        recentItemsClosed,
        history,
        openItemStack,
      } = this.props;

      Mousetrap.bind(upKeys, () => recentItemsSelectionUp());
      Mousetrap.bind(downKeys, () => recentItemsSelectionDown());
      Mousetrap.bind(openKeys, () => {
        if (this.props.selectedDocRef !== undefined) {
          openDocRef(history, this.props.selectedDocRef);
          recentItemsClosed();
        } else if (openItemStack.length > 0) {
          openDocRef(history, openItemStack[0]);
          recentItemsClosed();
        }
      });
    },
    componentWillUnmount() {
      Mousetrap.unbind(upKeys);
      Mousetrap.unbind(downKeys);
      Mousetrap.unbind(openKeys);
    },
{'fraction_non_alphanumeric': 0.11144278606965174, 'fraction_numerical': 0.003980099502487562, 'mean_word_length': 3.28839590443686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3832230', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1417, 'n_words': 389}