export function fetchDiffFilesBatch()

in app/assets/javascripts/diffs/stores/legacy_diffs/actions.js [229:329]


export function fetchDiffFilesBatch(linkedFileLoading = false) {
  let perPage = this.viewDiffsFileByFile ? 1 : this.perPage;
  let increaseAmount = 1.4;
  const startPage = 0;
  const id = window?.location?.hash;
  const isNoteLink = id.indexOf('#note') === 0;
  const urlParams = {
    w: this.showWhitespace ? '0' : '1',
    view: 'inline',
  };
  const hash = window.location.hash.replace('#', '').split('diff-content-').pop();
  let totalLoaded = 0;
  let scrolledVirtualScroller = hash === '';

  if (!linkedFileLoading) {
    this[types.SET_BATCH_LOADING_STATE]('loading');
    this[types.SET_RETRIEVING_BATCHES](true);
  }
  eventHub.$emit(EVT_PERF_MARK_DIFF_FILES_START);

  const getBatch = (page = startPage) =>
    axios
      .get(mergeUrlParams({ ...urlParams, page, per_page: perPage }, this.endpointBatch))
      .then(({ data: { pagination, diff_files: diffFiles } }) => {
        totalLoaded += diffFiles.length;

        this[types.SET_DIFF_DATA_BATCH]({ diff_files: diffFiles });
        this[types.SET_BATCH_LOADING_STATE]('loaded');

        if (!scrolledVirtualScroller && !linkedFileLoading) {
          const index = this.diffFiles.findIndex(
            (f) =>
              f.file_hash === hash || f[INLINE_DIFF_LINES_KEY].find((l) => l.line_code === hash),
          );

          if (index >= 0) {
            eventHub.$emit('scrollToIndex', index);
            scrolledVirtualScroller = true;
          }
        }

        if (!isNoteLink && !this.currentDiffFileId) {
          this[types.SET_CURRENT_DIFF_FILE](diffFiles[0]?.file_hash);
        }

        if (isNoteLink) {
          this.setCurrentDiffFileIdFromNote(id.split('_').pop());
        }

        if (totalLoaded === pagination.total_pages || pagination.total_pages === null) {
          this[types.SET_RETRIEVING_BATCHES](false);
          eventHub.$emit('doneLoadingBatches');

          // We need to check that the currentDiffFileId points to a file that exists
          if (
            this.currentDiffFileId &&
            !this.diffFiles.some((f) => f.file_hash === this.currentDiffFileId) &&
            !isNoteLink
          ) {
            this[types.SET_CURRENT_DIFF_FILE](this.diffFiles[0].file_hash);
          }

          if (this.diffFiles?.length) {
            // eslint-disable-next-line promise/catch-or-return,promise/no-nesting
            import('~/code_navigation').then((m) =>
              m.default({
                blobs: this.diffFiles
                  .filter((f) => f.code_navigation_path)
                  .map((f) => ({
                    path: f.new_path,
                    codeNavigationPath: f.code_navigation_path,
                  })),
                definitionPathPrefix: this.definitionPathPrefix,
              }),
            );
          }

          return null;
        }

        const nextPage = page + perPage;
        perPage = Math.min(Math.ceil(perPage * increaseAmount), 30);
        increaseAmount = Math.min(increaseAmount + 0.2, 2);

        return nextPage;
      })
      .then((nextPage) => {
        if (nextPage) {
          return getBatch(nextPage);
        }

        return null;
      })
      .catch((error) => {
        this[types.SET_RETRIEVING_BATCHES](false);
        this[types.SET_BATCH_LOADING_STATE]('error');
        throw error;
      });

  return getBatch();
}