{"size": 2970, "ext": "js", "max_stars_repo_path": "app/assets/javascripts/ide/stores/actions/tree.js", "max_stars_repo_name": "abdullatif1992/gitlabhq", "max_stars_repo_stars_event_min_datetime": "2018-06-05T06:02:41.000Z", "max_stars_repo_stars_event_max_datetime": "2018-06-05T06:02:41.000Z", "max_issues_repo_path": "app/assets/javascripts/ide/stores/actions/tree.js", "max_issues_repo_name": "abdullatif1992/gitlabhq", "max_issues_repo_issues_event_min_datetime": "2021-05-21T00:46:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T11:15:45.000Z", "max_forks_repo_path": "app/assets/javascripts/ide/stores/actions/tree.js", "max_forks_repo_name": "abdullatif1992/gitlabhq", "max_forks_repo_forks_event_min_datetime": "2019-11-28T19:12:40.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-04T05:29:58.000Z"}
{"max_stars_count": 1, "max_issues_count": 5, "max_forks_count": 2, "avg_line_length": 30.9375, "max_line_length": 100, "alphanum_fraction": 0.5764309764}
import { normalizeHeaders } from '~/lib/utils/common_utils';
import flash from '~/flash';
import service from '../../services';
import * as types from '../mutation_types';
import { findEntry } from '../utils';
import FilesDecoratorWorker from '../workers/files_decorator_worker';

export const toggleTreeOpen = ({ commit }, path) => {
  commit(types.TOGGLE_TREE_OPEN, path);
};

export const handleTreeEntryAction = ({ commit, dispatch }, row) => {
  if (row.type === 'tree') {
    dispatch('toggleTreeOpen', row.path);
  } else if (row.type === 'blob' && (row.opened || row.changed)) {
    if (row.changed && !row.opened) {
      commit(types.TOGGLE_FILE_OPEN, row.path);
    }

    dispatch('setFileActive', row.path);
  } else {
    dispatch('getFileData', { path: row.path });
  }
};

export const getLastCommitData = ({ state, commit, dispatch }, tree = state) => {
  if (!tree || tree.lastCommitPath === null || !tree.lastCommitPath) return;

  service
    .getTreeLastCommit(tree.lastCommitPath)
    .then(res => {
      const lastCommitPath = normalizeHeaders(res.headers)['MORE-LOGS-URL'] || null;

      commit(types.SET_LAST_COMMIT_URL, { tree, url: lastCommitPath });

      return res.json();
    })
    .then(data => {
      data.forEach(lastCommit => {
        const entry = findEntry(tree.tree, lastCommit.type, lastCommit.file_name);

        if (entry) {
          commit(types.SET_LAST_COMMIT_DATA, { entry, lastCommit });
        }
      });

      dispatch('getLastCommitData', tree);
    })
    .catch(() => flash('Error fetching log data.', 'alert', document, null, false, true));
};

export const getFiles = ({ state, commit }, { projectId, branchId } = {}) =>
  new Promise((resolve, reject) => {
    if (!state.trees[`${projectId}/${branchId}`]) {
      const selectedProject = state.projects[projectId];
      commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` });

      service
        .getFiles(selectedProject.web_url, branchId)
        .then(res => res.json())
        .then(data => {
          const worker = new FilesDecoratorWorker();
          worker.addEventListener('message', e => {
            const { entries, treeList } = e.data;
            const selectedTree = state.trees[`${projectId}/${branchId}`];

            commit(types.SET_ENTRIES, entries);
            commit(types.SET_DIRECTORY_DATA, {
              treePath: `${projectId}/${branchId}`,
              data: treeList,
            });
            commit(types.TOGGLE_LOADING, {
              entry: selectedTree,
              forceValue: false,
            });

            worker.terminate();

            resolve();
          });

          worker.postMessage({
            data,
            projectId,
            branchId,
          });
        })
        .catch(e => {
          flash('Error loading tree data. Please try again.', 'alert', document, null, false, true);
          reject(e);
        });
    } else {
      resolve();
    }
  });
