<commit_before>import debounce from 'lodash.debounce'
import element from 'virtual-element'

import Editor from './editor'
import {getSearchResult} from './search'
import Menu from './menu'
import MenuCover from './menu_cover'
import Panel from './panel'

let currentState

function afterMount({}, el, setState) {
  globals.setRootState = setState

  initBookmarkEvent()
}

function afterUpdate({props, state}, prevProps, prevState, setState) {
  if (prevState.isSearching && !state.isSearching) {
    setState({
      trees: getDefaultTrees(props)
    })
  }
}

function beforeMount() {
  initStyleOptions()
}

function beforeRender({state}) {
  // assume this module will always update
  currentState = state
}

function contextMenuHandler(event) {
  const target = event.target

  // allow native context menu if it is an input element
  if (target.tagName === 'INPUT') {
    return
  }

  // disable native context menu
  event.preventDefault()
}

function getDefaultTrees(props) {
  return Immutable([props.defExpandTree])
}

function initBookmarkEvent() {
  const renewCurrentTrees = () => renewTrees(currentState.trees)

  const renewTrees = debounce(async function(oldTrees) {
    // Promise.all cannot recognize immutable array
    const newTrees = await Promise.all(oldTrees.asMutable().map((treeInfo) => {
      if (treeInfo.id === 'search-result') {
        return getSearchResult()
      }

      return globals.getFlatTree(treeInfo.id)
    }))

    globals.setRootState({
      // to make sure the menu is not activated when bookmark is updating
      editorTarget: null,
      menuTarget: null,

      trees: Immutable(newTrees)
    })
  }, 100)

  const renewSlicedTreesById = (itemId) => {
    const removeFromIndex = currentState.trees.findIndex((treeInfo) => {
      return treeInfo.id === itemId
    })

    const slicedTrees = globals.getSlicedTrees(currentState.trees, removeFromIndex)

    renewTrees(slicedTrees)
  }

  chrome.bookmarks.onChanged.addListener(renewCurrentTrees)
  chrome.bookmarks.onCreated.addListener(renewCurrentTrees)
  chrome.bookmarks.onMoved.addListener(renewSlicedTreesById)<commit_msg>Fix closing folder after deleting a bookmark<commit_after>import debounce from 'lodash.debounce'
import element from 'virtual-element'

import Editor from './editor'
import {getSearchResult} from './search'
import Menu from './menu'
import MenuCover from './menu_cover'
import Panel from './panel'

let currentState

function afterMount({}, el, setState) {
  globals.setRootState = setState

  initBookmarkEvent()
}

function afterUpdate({props, state}, prevProps, prevState, setState) {
  if (prevState.isSearching && !state.isSearching) {
    setState({
      trees: getDefaultTrees(props)
    })
  }
}

function beforeMount() {
  initStyleOptions()
}

function beforeRender({state}) {
  // assume this module will always update
  currentState = state
}

function contextMenuHandler(event) {
  const target = event.target

  // allow native context menu if it is an input element
  if (target.tagName === 'INPUT') {
    return
  }

  // disable native context menu
  event.preventDefault()
}

function getDefaultTrees(props) {
  return Immutable([props.defExpandTree])
}

function initBookmarkEvent() {
  const renewCurrentTrees = () => renewTrees(currentState.trees)

  const renewTrees = debounce(async function(oldTrees) {
    // Promise.all cannot recognize immutable array
    const newTrees = await Promise.all(oldTrees.asMutable().map((treeInfo) => {
      if (treeInfo.id === 'search-result') {
        return getSearchResult()
      }

      return globals.getFlatTree(treeInfo.id)
    }))

    globals.setRootState({
      // to make sure the menu is not activated when bookmark is updating
      editorTarget: null,
      menuTarget: null,

      trees: Immutable(newTrees)
    })
  }, 100)

  const renewSlicedTreesById = (itemId) => {
    const removeFromIndex = currentState.trees.findIndex((treeInfo) => treeInfo.id === itemId)

    if (removeFromIndex >= 0) {
      const slicedTrees = globals.getSlicedTrees(currentState.trees, removeFromIndex)

      renewTrees(slicedTrees)
    } else {
      renewCurrentTrees()
    }
  }

  chrome.bookmarks.onChanged.addListener(renewCurrentTrees)
  chrome.bookmarks.onCreated.addListener(renewCurrentTrees)
  chrome.bookmarks.onMoved.addListener(renewSlicedTreesById)
{'fraction_non_alphanumeric': 0.10736407432897453, 'fraction_numerical': 0.001605872906629961, 'mean_word_length': 3.9209932279909707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '330565', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1302, 'n_words': 315}