<commit_before>import { useMemo } from 'react'
import PropTypes from 'prop-types'

import { addActions, actions } from '../actions'
import { defaultState } from './useTableState'
import {
  mergeProps,
  applyPropHooks,
  getFirstDefined,
  defaultOrderByFn,
  defaultSortByFn
} from '../utils'

defaultState.sortBy = []

addActions({
  sortByChange: '__sortByChange__'
})

const propTypes = {
  // General
  columns: PropTypes.arrayOf(
    PropTypes.shape({
      sortByFn: PropTypes.func,
      efaultSortDesc: PropTypes.bool
    })
  ),
  sortByFn: PropTypes.func,
  manualSorting: PropTypes.bool,
  disableSorting: PropTypes.bool,
  defaultSortDesc: PropTypes.bool,
  disableMultiSort: PropTypes.bool
}

export const useSortBy = props => {
  PropTypes.checkPropTypes(propTypes, props, 'property', 'useSortBy')

  const {
    debug,
    rows,
    columns,
    orderByFn = defaultOrderByFn,
    sortByFn = defaultSortByFn,
    manualSorting,
    disableSorting,
    defaultSortDesc,
    hooks,
    state: [{ sortBy }, setState]
  } = props

  columns.forEach(column => {
    const { accessor, canSortBy } = column
    column.canSortBy = accessor
      ? getFirstDefined(
        canSortBy,
        disableSorting === true ? false : undefined,
        true
      )
      : false
  })

  // Updates sorting based on a columnID, desc flag and multi flag
  const toggleSortByID = (columnID, desc, multi) => {
    return setState(old => {
      const { sortBy } = old

      // Find the column for this columnID
      const column = columns.find(d => d.id === columnID)
      const resolvedDefaultSortDesc = getFirstDefined(
        column.defaultSortDesc,
        defaultSortDesc
      )

      // Find any existing sortBy for this column
      const existingSortBy = sortBy.find(d => d.id === columnID)
      const hasDescDefined = typeof desc !== 'undefined' && desc !== null

      let newSortBy = []

      // What should we do with this filter?
      let action

      if (!multi) {
        if (sortBy.length <= 1 && existingSortBy) {
          if (existingSortBy.desc) {
            action = 'remove'
          } else {
            action = 'toggle'
          }
        } else {
          action = 'replace'
        }
      } else {
        if (!existingSortBy) {
          action = 'add'
        } else {
          if (hasDescDefined) {
            action = 'set'
          } else {
            action = 'toggle'
          }
        }
      }

      if (action === 'replace') {
        newSortBy = [
          {<commit_msg>Fix defaultSortDesc blocking toggle action (#1341)<commit_after>import { useMemo } from 'react'
import PropTypes from 'prop-types'

import { addActions, actions } from '../actions'
import { defaultState } from './useTableState'
import {
  mergeProps,
  applyPropHooks,
  getFirstDefined,
  defaultOrderByFn,
  defaultSortByFn
} from '../utils'

defaultState.sortBy = []

addActions({
  sortByChange: '__sortByChange__'
})

const propTypes = {
  // General
  columns: PropTypes.arrayOf(
    PropTypes.shape({
      sortByFn: PropTypes.func,
      efaultSortDesc: PropTypes.bool
    })
  ),
  sortByFn: PropTypes.func,
  manualSorting: PropTypes.bool,
  disableSorting: PropTypes.bool,
  defaultSortDesc: PropTypes.bool,
  disableMultiSort: PropTypes.bool
}

export const useSortBy = props => {
  PropTypes.checkPropTypes(propTypes, props, 'property', 'useSortBy')

  const {
    debug,
    rows,
    columns,
    orderByFn = defaultOrderByFn,
    sortByFn = defaultSortByFn,
    manualSorting,
    disableSorting,
    defaultSortDesc,
    hooks,
    state: [{ sortBy }, setState]
  } = props

  columns.forEach(column => {
    const { accessor, canSortBy } = column
    column.canSortBy = accessor
      ? getFirstDefined(
        canSortBy,
        disableSorting === true ? false : undefined,
        true
      )
      : false
  })

  // Updates sorting based on a columnID, desc flag and multi flag
  const toggleSortByID = (columnID, desc, multi) => {
    return setState(old => {
      const { sortBy } = old

      // Find the column for this columnID
      const column = columns.find(d => d.id === columnID)
      const resolvedDefaultSortDesc = getFirstDefined(
        column.defaultSortDesc,
        defaultSortDesc
      )

      // Find any existing sortBy for this column
      const existingSortBy = sortBy.find(d => d.id === columnID)
      const hasDescDefined = typeof desc !== 'undefined' && desc !== null

      let newSortBy = []

      // What should we do with this filter?
      let action

      if (!multi) {
        if (sortBy.length <= 1 && existingSortBy) {
          if ((existingSortBy.desc && !resolvedDefaultSortDesc) ||
            (!existingSortBy.desc && resolvedDefaultSortDesc)) {
            action = 'remove'
          } else {
            action = 'toggle'
          }
        } else {
          action = 'replace'
        }
      } else {
        if (!existingSortBy) {
          action = 'add'
        } else {
          if (hasDescDefined) {
            action = 'set'
          } else {
            action = 'toggle'
          }
        }
      }

      if (action === 'replace') {
        newSortBy = [
          {
{'fraction_non_alphanumeric': 0.10678488820354665, 'fraction_numerical': 0.001156515034695451, 'mean_word_length': 2.2130030959752323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '4702371', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1538, 'n_words': 384}