<commit_before>import React from 'react'
import PropTypes from 'prop-types'
import qs from 'qs'
import { Route } from 'react-router-dom'
import { omit } from 'lodash'

import { Chip } from '..'

const RoutedAdviserFilterChips = ({
  qsParamName,
  selectedAdvisers,
  ...props
}) => {
  return (
    <Route>
      {({ location, history }) => {
        const qsParams = qs.parse(location.search.slice(1))
        const filterSearchParams = (
          qsParams,
          queryParam,
          targetValue = null
        ) => {
          const [filteredResult] = Object.entries(qsParams)
            .map(([key]) =>
              key === queryParam
                ? Array.isArray(qsParams[key])
                  ? {
                      ...qsParams,
                      ...{
                        [key]: qsParams[key].filter((x) => x !== targetValue),
                      },
                    }
                  : omit(qsParams, queryParam)
                : null
            )
            .filter(Boolean)
          return filteredResult
        }
        return selectedAdvisers.map(({ advisers: { name, id } }) => (
          <Chip
            key={id}
            value={id}
            onClick={(e) => {
              history.push({
                search: qs.stringify({
                  ...filterSearchParams(
                    qsParams,
                    qsParamName,
                    e.target.getAttribute('data-value')
                  ),
                }),
              })
            }}
            {...props}
          >
            {name}
          </Chip>
        ))
      }}
    </Route>
  )
}

RoutedAdviserFilterChips.propTypes = {
  qsParamName: PropTypes.string.isRequired,
  selectedAdvisers: PropTypes.array.isRequired,
}

export default RoutedAdviserFilterChips
<commit_msg>Abstract function and flatten for clarity<commit_after>import React from 'react'
import PropTypes from 'prop-types'
import qs from 'qs'
import { Route } from 'react-router-dom'
import { omit } from 'lodash'

import { Chip } from '..'

const RoutedAdviserFilterChips = ({
  qsParamName,
  selectedAdvisers,
  ...props
}) => {
  const removeFilterChip = (qsParams, queryParam, targetValue = null) =>
    Object.entries(qsParams)
      .map(([key]) => {
        if (key === queryParam) {
          return Array.isArray(qsParams[key])
            ? {
                ...qsParams,
                [key]: qsParams[key].filter((x) => x !== targetValue),
              }
            : omit(qsParams, queryParam)
        }
      })
      .filter(Boolean)[0]

  return (
    <Route>
      {({ location, history }) => {
        const qsParams = qs.parse(location.search.slice(1))
        return selectedAdvisers.map(({ advisers: { name, id } }) => (
          <Chip
            key={id}
            value={id}
            onClick={(e) => {
              history.push({
                search: qs.stringify({
                  ...removeFilterChip(
                    qsParams,
                    qsParamName,
                    e.target.getAttribute('data-value')
                  ),
                }),
              })
            }}
            {...props}
          >
            {name}
          </Chip>
        ))
      }}
    </Route>
  )
}

RoutedAdviserFilterChips.propTypes = {
  qsParamName: PropTypes.string.isRequired,
  selectedAdvisers: PropTypes.array.isRequired,
}

export default RoutedAdviserFilterChips

{'fraction_non_alphanumeric': 0.12554616953102243, 'fraction_numerical': 0.000873871249635887, 'mean_word_length': 1.4030790762771168, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '5104225', 'n_tokens_mistral': 995, 'n_tokens_neox': 948, 'n_words': 176}