<commit_before>import config from 'config'
import api from 'backendAPI'
import { autorun } from 'mobx'
import { FsSocketClient } from 'backendAPI/websocketClients'
import store, { getState, dispatch } from 'store'
import mobxStore from 'mobxStore'
import * as TabActions from 'components/Tab/actions'
import * as GitActions from 'components/Git/actions'
import * as FileActions from './actions'

function handleGitFiles (node) {
  const path = node.path
  const pathArray = path.split('/.git/refs/heads/')
  if (pathArray.length > 1) {
    const branchName = pathArray[1]
    const gitState = getState().GitState
    const current = gitState.branches.current
    if (branchName === current) {
      const history = gitState.history
      const focusedNodes = Object.values(mobxStore.FileTreeState.entities).filter(node => node.isFocused)
      const historyPath = focusedNodes[0] ? focusedNodes[0].path : '/'
      dispatch(GitActions.fetchHistory({
        path: historyPath,
        size: history.size,
        page: 0,
        reset: true
      }))
      api.gitStatus().then(({ files, clean }) => {
        const gitStatus = mobxStore.FileTreeState.gitStatus
        const result = gitStatus.map((node) => {
          const file = files.find(file => (`/${file.name}`) === node.path)
          return {
            ...node,
            gitStatus: file ? file.status : 'CLEAN',
          }
        })

        FileActions.loadNodeData(result)
      })
    }
    return true
  }
  return false
}

export default function subscribeToFileChange () {
  autorun(() => {
    if (!config.fsSocketConnected) return
    const client = FsSocketClient.$$singleton.stompClient
    client.subscribe('CONNECTED', (frame) => {
      console.log('FS CONNECTED', frame)
    })

    client.subscribe(`/topic/ws/${config.spaceKey}/change`, (frame) => {
      const data = JSON.parse(frame.body)
      const node = data.fileInfo
      switch (data.changeType) {
        case 'create':
          if (handleGitFiles(node)) {
            break
          }
          FileActions.loadNodeData([node])
          break
        case 'modify':
          if (handleGitFiles(node)) {
            break
          }
          FileActions.loadNodeData([node])
          const tabsToUpdate = mobxStore.EditorTabState.tabs.values().filter(tab => tab.path === node.path)
          if (tabsToUpdate.length) {
            api.readFile(node.path).then(({ content }) => {
              dispatch(TabActions.updateTabByPath({
                path: node.path,
                content,
              }))
            })
          }
          break
        case 'delete':
          dispatch(FileActions.removeNode(node))
          break
      }
    })<commit_msg>:bug: unsubscribe from invalid channel<commit_after>import config from 'config'
import api from 'backendAPI'
import { autorun } from 'mobx'
import { FsSocketClient } from 'backendAPI/websocketClients'
import store, { getState, dispatch } from 'store'
import mobxStore from 'mobxStore'
import * as TabActions from 'components/Tab/actions'
import * as GitActions from 'components/Git/actions'
import * as FileActions from './actions'

function handleGitFiles (node) {
  const path = node.path
  const pathArray = path.split('/.git/refs/heads/')
  if (pathArray.length > 1) {
    const branchName = pathArray[1]
    const gitState = getState().GitState
    const current = gitState.branches.current
    if (branchName === current) {
      const history = gitState.history
      const focusedNodes = Object.values(mobxStore.FileTreeState.entities).filter(node => node.isFocused)
      const historyPath = focusedNodes[0] ? focusedNodes[0].path : '/'
      dispatch(GitActions.fetchHistory({
        path: historyPath,
        size: history.size,
        page: 0,
        reset: true
      }))
      api.gitStatus().then(({ files, clean }) => {
        const gitStatus = mobxStore.FileTreeState.gitStatus
        const result = gitStatus.map((node) => {
          const file = files.find(file => (`/${file.name}`) === node.path)
          return {
            ...node,
            gitStatus: file ? file.status : 'CLEAN',
          }
        })

        FileActions.loadNodeData(result)
      })
    }
    return true
  }
  return false
}

export default function subscribeToFileChange () {
  autorun(() => {
    if (!config.fsSocketConnected) return
    const client = FsSocketClient.$$singleton.stompClient

    client.subscribe(`/topic/ws/${config.spaceKey}/change`, (frame) => {
      const data = JSON.parse(frame.body)
      const node = data.fileInfo
      switch (data.changeType) {
        case 'create':
          if (handleGitFiles(node)) {
            break
          }
          FileActions.loadNodeData([node])
          break
        case 'modify':
          if (handleGitFiles(node)) {
            break
          }
          FileActions.loadNodeData([node])
          const tabsToUpdate = mobxStore.EditorTabState.tabs.values().filter(tab => tab.path === node.path)
          if (tabsToUpdate.length) {
            api.readFile(node.path).then(({ content }) => {
              dispatch(TabActions.updateTabByPath({
                path: node.path,
                content,
              }))
            })
          }
          break
        case 'delete':
          dispatch(FileActions.removeNode(node))
          break
      }
    })
{'fraction_non_alphanumeric': 0.12113306000745434, 'fraction_numerical': 0.0018635855385762206, 'mean_word_length': 2.2745576571079926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '2353838', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1505, 'n_words': 333}