<commit_before>
import React from 'react'
import Tabs from './tabs'
import Btns from './btns'
import Modal from './modal'
import {generate} from 'shortid'
import _ from 'lodash'
import {statusMap, settingMap} from '../../common/constants'
import './control.styl'

const {prefix} = window
const e = prefix('control')

const defaultStatus = statusMap.processing
export const newTerm = () => ({
  id: generate(),
  status: defaultStatus,
  title: e('newTerminal')
})

export default class IndexControl extends React.Component {

  state = {
    item: {
      id: ''
    },
    tab: settingMap.bookmarks
  }

  onDup = tab => {
    delete tab.status
    this.props.addTab({
      ...tab,
      status: defaultStatus,
      id: generate()
    })
  }

  onAdd = () => {
    this.props.addTab(newTerm())
  }

  onChange = currentTabId => {
    this.props.modifier({currentTabId})
  }

  onClose = id => {
    this.props.delTab({id})
  }

  onNewSsh = () => {
    this.setState({
      tab: settingMap.bookmarks,
      item: {
        id: ''
      }
    }, this.openModal)
  }

  onSelectHistory = id => {
    let item = _.find(this.props.history, it => it.id === id)
    this.props.addTab({
      ...item,
      status: defaultStatus,
      id: generate()
    })
  }

  onSelectBookmark = id => {
    let item = _.find(this.props.bookmarks, it => it.id === id)
    this.props.addTab({
      ...item,
      status: defaultStatus,
      id: generate()
    })
    item.id = generate()
    if (!_.some(this.props.history, j => {
      let keysj = Object.keys(j)
      let keysi = Object.keys(item)
      return _.isEqual(
        _.pick(item, _.without(keysi, 'id')),
        _.pick(j, _.without(keysj, 'id'))
      )
    })) {
      this.props.addItem(item, settingMap.history)
    }
  }

  openSetting = () => {
    this.setState({
      tab: settingMap.setting,
      item: {
        id: ''
      }
    }, this.openModal)
  }

  openModal = () => {
    this.modal.show()
  }
<commit_msg>fix #263 new history moves on list top (#264)<commit_after>
import React from 'react'
import Tabs from './tabs'
import Btns from './btns'
import Modal from './modal'
import {generate} from 'shortid'
import _ from 'lodash'
import copy from 'json-deep-copy'
import {statusMap, settingMap} from '../../common/constants'
import './control.styl'

const {prefix} = window
const e = prefix('control')

const defaultStatus = statusMap.processing
export const newTerm = () => ({
  id: generate(),
  status: defaultStatus,
  title: e('newTerminal')
})

export default class IndexControl extends React.Component {

  state = {
    item: {
      id: ''
    },
    tab: settingMap.bookmarks
  }

  onDup = tab => {
    delete tab.status
    this.props.addTab({
      ...tab,
      status: defaultStatus,
      id: generate()
    })
  }

  onAdd = () => {
    this.props.addTab(newTerm())
  }

  onChange = currentTabId => {
    this.props.modifier({currentTabId})
  }

  onClose = id => {
    this.props.delTab({id})
  }

  onNewSsh = () => {
    this.setState({
      tab: settingMap.bookmarks,
      item: {
        id: ''
      }
    }, this.openModal)
  }

  onSelectHistory = id => {
    let item = _.find(this.props.history, it => it.id === id)
    this.props.addTab({
      ...item,
      status: defaultStatus,
      id: generate()
    })
  }

  onSelectBookmark = id => {
    let {history, bookmarks} = this.props
    let item = _.find(bookmarks, it => it.id === id)
    this.props.addTab({
      ...item,
      status: defaultStatus,
      id: generate()
    })
    item.id = generate()

    let existItem = _.find(history, j => {
      let keysj = Object.keys(j)
      let keysi = Object.keys(item)
      return _.isEqual(
        _.pick(item, _.without(keysi, 'id')),
        _.pick(j, _.without(keysj, 'id'))
      )
    })
    if (!existItem) {
      this.props.addItem(item, settingMap.history)
    } else {
      let historyNew = copy(history)
      let index = _.findIndex(historyNew, f => f.id === existItem.id)
      historyNew.splice(index, 1)
      historyNew.unshift(existItem)
      this.props.modifier({history: historyNew})
    }
  }

  openSetting = () => {
    this.setState({
      tab: settingMap.setting,
      item: {
        id: ''
      }
    }, this.openModal)
  }

  openModal = () => {
    this.modal.show()
  }

{'fraction_non_alphanumeric': 0.15785813630041726, 'fraction_numerical': 0.0016226240148354196, 'mean_word_length': 2.2938931297709924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '4769245', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1446, 'n_words': 320}