<commit_before>import {
  FETCH_SUCCESS, COMMIT_SUCCESS,
  FAILURE, REQUEST,
  SELECT_NAMESPACE
} from '../actions/namespaces.jsx'

import {
  ADD_NAMESPACE, UPDATE_NAMESPACE, REMOVE_NAMESPACE,
  ADD_BUCKET, UPDATE_BUCKET, REMOVE_BUCKET
} from '../actions/mutable.jsx'

import { INITIAL_HISTORY } from './history.jsx'

import Immutable from 'seamless-immutable'

// These are special buckets that exist on the top-level
// namespace object and need to be special-cased
const BUCKET_KEY_MAP = {
  '___DEFAULT_BUCKET___': 'default_bucket',
  '___DYNAMIC_BUCKET_TPL___': 'dynamic_bucket_template'
}

function addNamespace(state, action) {
  return Object.assign({}, state, {
    change: {
      type: ADD_NAMESPACE,
      key: action.namespace
    },
    items: state.items.set(action.namespace, {
      buckets: {},
      name: action.namespace
    })
  })
}

function updateNamespace(state, action) {
  return Object.assign({}, state, {
    change: {
      type: UPDATE_NAMESPACE,
      key: `${action.namespace}.${action.key}`,
      value: action.value
    },
    items: state.items.setIn(
      [action.namespace, action.key],
      action.value
    )
  })
}

function removeNamespace(state, action) {
  return Object.assign({}, state, {
    change: {
      type: REMOVE_NAMESPACE,
      key: action.namespace
    },
    items: state.items.without(action.namespace)
  })
}

function addBucket(state, action) {
  let bucketPath = ['buckets', action.bucket]
  const bucketKey = BUCKET_KEY_MAP[action.bucket]

  if (bucketKey) {
    action.bucket = bucketKey
    bucketPath = [bucketKey]
  }

  return Object.assign({}, state, {
    change: {
      type: ADD_BUCKET,
      key: `${action.namespace}.${action.bucket}`
    },
    items: state.items.setIn(
      [action.namespace, ...bucketPath],
      {
        name: action.bucket,
        namespace: action.namespace
      }
    )
  })
}

function updateBucket(state, action) {
  let bucketPath = ['buckets', action.bucket]
  const bucketKey = BUCKET_KEY_MAP[action.bucket]

  if (bucketKey) {
    action.bucket = bucketKey
    bucketPath = [bucketKey]
  }

  return Object.assign({}, state, {
    change: {
      type: UPDATE_BUCKET,
      key: `${action.namespace}.${action.bucket}.${action.key}`,
      value: action.value
    },
    items: state.items.setIn(
      [action.namespace, ...bucketPath, action.key],
      action.value
    )
  })
}

function removeBucket(state, action) {
  let bucketPath = ['buckets']
  const bucketKey = BUCKET_KEY_MAP[action.bucket]

  if (bucketKey) {
    action.bucket = bucketKey
    bucketPath = []
  }

  return Object.assign({}, state, {
    change: {
      type: REMOVE_BUCKET,
      key: `${action.namespace}.${action.bucket}`
    },
    items: state.items.updateIn(
      [action.namespace, ...bucketPath],
      (buckets) => buckets.without(action.bucket)
    )
  })
}

export function namespaces(state, action) {
  switch (action.type) {
    case ADD_NAMESPACE:
      return addNamespace(state, action)
    case UPDATE_NAMESPACE:
      return updateNamespace(state, action)
    case REMOVE_NAMESPACE:
      return removeNamespace(state, action)
    case ADD_BUCKET:
      return addBucket(state, action)
    case UPDATE_BUCKET:
      return updateBucket(state, action)
    case REMOVE_BUCKET:
      return removeBucket(state, action)
    case REQUEST:
    case COMMIT_SUCCESS:
    case FETCH_SUCCESS:
    case FAILURE:
      return handleRequest(state, action)
    default:
      return state
  }
}

function handleRequest(state, action) {
  if (action.error) {
    return Object.assign({}, state, {
      inRequest: false,
      error: action.payload
    })
  }

  switch (action.type) {
    case REQUEST:
      return Object.assign({}, state, {
        inRequest: true,
        error: null
      })
    case COMMIT_SUCCESS:
      return INITIAL_HISTORY
    case FETCH_SUCCESS:
      return Object.assign({}, state, {
        inRequest: false,
        items: Immutable.from(action.payload.namespaces || {})
      })
  }
}

export function selectedNamespace(state = null, action) {
  switch (action.type) {
    case SELECT_NAMESPACE:
      return action.namespace
    case REQUEST:
      return null
    default:
      return state
  }
}<commit_msg>UI: make sure to remove the history when hitting refresh<commit_after>import {
  FETCH_SUCCESS, COMMIT_SUCCESS,
  FAILURE, REQUEST,
  SELECT_NAMESPACE
} from '../actions/namespaces.jsx'

import {
  ADD_NAMESPACE, UPDATE_NAMESPACE, REMOVE_NAMESPACE,
  ADD_BUCKET, UPDATE_BUCKET, REMOVE_BUCKET
} from '../actions/mutable.jsx'

import { INITIAL_HISTORY } from './history.jsx'

import Immutable from 'seamless-immutable'

// These are special buckets that exist on the top-level
// namespace object and need to be special-cased
const BUCKET_KEY_MAP = {
  '___DEFAULT_BUCKET___': 'default_bucket',
  '___DYNAMIC_BUCKET_TPL___': 'dynamic_bucket_template'
}

function addNamespace(state, action) {
  return Object.assign({}, state, {
    change: {
      type: ADD_NAMESPACE,
      key: action.namespace
    },
    items: state.items.set(action.namespace, {
      buckets: {},
      name: action.namespace
    })
  })
}

function updateNamespace(state, action) {
  return Object.assign({}, state, {
    change: {
      type: UPDATE_NAMESPACE,
      key: `${action.namespace}.${action.key}`,
      value: action.value
    },
    items: state.items.setIn(
      [action.namespace, action.key],
      action.value
    )
  })
}

function removeNamespace(state, action) {
  return Object.assign({}, state, {
    change: {
      type: REMOVE_NAMESPACE,
      key: action.namespace
    },
    items: state.items.without(action.namespace)
  })
}

function addBucket(state, action) {
  let bucketPath = ['buckets', action.bucket]
  const bucketKey = BUCKET_KEY_MAP[action.bucket]

  if (bucketKey) {
    action.bucket = bucketKey
    bucketPath = [bucketKey]
  }

  return Object.assign({}, state, {
    change: {
      type: ADD_BUCKET,
      key: `${action.namespace}.${action.bucket}`
    },
    items: state.items.setIn(
      [action.namespace, ...bucketPath],
      {
        name: action.bucket,
        namespace: action.namespace
      }
    )
  })
}

function updateBucket(state, action) {
  let bucketPath = ['buckets', action.bucket]
  const bucketKey = BUCKET_KEY_MAP[action.bucket]

  if (bucketKey) {
    action.bucket = bucketKey
    bucketPath = [bucketKey]
  }

  return Object.assign({}, state, {
    change: {
      type: UPDATE_BUCKET,
      key: `${action.namespace}.${action.bucket}.${action.key}`,
      value: action.value
    },
    items: state.items.setIn(
      [action.namespace, ...bucketPath, action.key],
      action.value
    )
  })
}

function removeBucket(state, action) {
  let bucketPath = ['buckets']
  const bucketKey = BUCKET_KEY_MAP[action.bucket]

  if (bucketKey) {
    action.bucket = bucketKey
    bucketPath = []
  }

  return Object.assign({}, state, {
    change: {
      type: REMOVE_BUCKET,
      key: `${action.namespace}.${action.bucket}`
    },
    items: state.items.updateIn(
      [action.namespace, ...bucketPath],
      (buckets) => buckets.without(action.bucket)
    )
  })
}

export function namespaces(state, action) {
  switch (action.type) {
    case ADD_NAMESPACE:
      return addNamespace(state, action)
    case UPDATE_NAMESPACE:
      return updateNamespace(state, action)
    case REMOVE_NAMESPACE:
      return removeNamespace(state, action)
    case ADD_BUCKET:
      return addBucket(state, action)
    case UPDATE_BUCKET:
      return updateBucket(state, action)
    case REMOVE_BUCKET:
      return removeBucket(state, action)
    case REQUEST:
    case COMMIT_SUCCESS:
    case FETCH_SUCCESS:
    case FAILURE:
      return handleRequest(state, action)
    default:
      return state
  }
}

function handleRequest(state, action) {
  if (action.error) {
    return Object.assign({}, state, {
      inRequest: false,
      error: action.payload
    })
  }

  switch (action.type) {
    case REQUEST:
      return Object.assign({}, state, {
        inRequest: true,
        error: null
      })
    case COMMIT_SUCCESS:
      return INITIAL_HISTORY
    case FETCH_SUCCESS:
      return Object.assign({}, INITIAL_HISTORY, {
        items: Immutable.from(action.payload.namespaces || {})
      })
  }
}

export function selectedNamespace(state = null, action) {
  switch (action.type) {
    case SELECT_NAMESPACE:
      return action.namespace
    case REQUEST:
      return null
    default:
      return state
  }
}
{'fraction_non_alphanumeric': 0.12008451696208475, 'fraction_numerical': 0.0, 'mean_word_length': 2.9298892988929888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6802019', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2744, 'n_words': 587}