<commit_before>import React from 'react'
import PropTypes from 'prop-types'
import { DeckShape } from './__commonShapes'
import styles from '../styles/components/DeckSelector.styl'

/**
 * Should be a simple pop-up of all the decks available to choose from,
 * optionally excluding (or greying out) a particular deck from the list.
 *
 * Input: decks to choose from, deck to exclude from selection, number of decks
 *   to allow for selection via [min, max]
 * Output: deck(s) selected from the list
 */
class DeckSelector extends React.Component {
  static propTypes = {
    decks: PropTypes.arrayOf(DeckShape.isRequired).isRequired,
    // excludedDeckIds: React.PropTypes.array,
    // maxSelected: React.PropTypes.number.isRequired,
    // minSelected: React.PropTypes.number.isRequired,
    push: PropTypes.func.isRequired,
    handleOnSelected: PropTypes.func
  }

  static defaultProps = {
    // maxSelected: 1,
    // minSelected: 1,
    handleOnSelected: () => {}
  }

  constructor(props) {
    super(props)

    this.handleKeyDown = this.handleKeyDown.bind(this)
    this.moveDown = this.moveDown.bind(this)
    this.moveUp = this.moveUp.bind(this)

    // Grab the currently viewed deck (if applicable) and pre-select it
    let index = null
    if (
      window &&
      window.location.pathname &&
      window.location.pathname.includes('/view/')
    ) {
      const deckId = window.location.pathname.split('/')[2]
      index = this.props.decks.findIndex(x => x.id === deckId)
    }

    this.state = {
      selectedIndex: index || 0
    }
  }

  navigateToDeck(deckId) {
    this.props.push(`/view/${deckId}`)
  }

  handleKeyDown(e) {
    if (e.keyCode === 38) {
      // Up arrow
      this.moveUp()
    } else if (e.keyCode === 40) {
      // Down arrow
      this.moveDown()
    } else if (e.keyCode === 13) {
      // Enter key
      this.navigateToDeck(this.props.decks[this.state.selectedIndex].id)
      this.props.handleOnSelected()
    }
  }

  moveDown() {
    this.setState({
      selectedIndex: Math.min(<commit_msg>fix: disallow the enter key from navigating to a deck that doesn't exist. this happened when the user had no decks but hit enter<commit_after>import React from 'react'
import PropTypes from 'prop-types'
import { DeckShape } from './__commonShapes'
import styles from '../styles/components/DeckSelector.styl'

/**
 * Should be a simple pop-up of all the decks available to choose from,
 * optionally excluding (or greying out) a particular deck from the list.
 *
 * Input: decks to choose from, deck to exclude from selection, number of decks
 *   to allow for selection via [min, max]
 * Output: deck(s) selected from the list
 */
class DeckSelector extends React.Component {
  static propTypes = {
    decks: PropTypes.arrayOf(DeckShape.isRequired).isRequired,
    // excludedDeckIds: React.PropTypes.array,
    // maxSelected: React.PropTypes.number.isRequired,
    // minSelected: React.PropTypes.number.isRequired,
    push: PropTypes.func.isRequired,
    handleOnSelected: PropTypes.func
  }

  static defaultProps = {
    // maxSelected: 1,
    // minSelected: 1,
    handleOnSelected: () => {}
  }

  constructor(props) {
    super(props)

    this.handleKeyDown = this.handleKeyDown.bind(this)
    this.moveDown = this.moveDown.bind(this)
    this.moveUp = this.moveUp.bind(this)

    // Grab the currently viewed deck (if applicable) and pre-select it
    let index = null
    if (
      window &&
      window.location.pathname &&
      window.location.pathname.includes('/view/')
    ) {
      const deckId = window.location.pathname.split('/')[2]
      index = this.props.decks.findIndex(x => x.id === deckId)
    }

    this.state = {
      selectedIndex: index || 0
    }
  }

  navigateToDeck(deckId) {
    this.props.push(`/view/${deckId}`)
  }

  handleKeyDown(e) {
    if (e.keyCode === 38) {
      // Up arrow
      this.moveUp()
    } else if (e.keyCode === 40) {
      // Down arrow
      this.moveDown()
    } else if (e.keyCode === 13) {
      // Enter key
      if (this.props.decks.length > 0) {
        // Only navigate if there are decks available
        this.navigateToDeck(this.props.decks[this.state.selectedIndex].id)
        this.props.handleOnSelected()
      }
    }
  }

  moveDown() {
    this.setState({
      selectedIndex: Math.min(
{'fraction_non_alphanumeric': 0.12332253586302638, 'fraction_numerical': 0.004858861638130495, 'mean_word_length': 2.792105263157895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '102850', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1343, 'n_words': 342}