diff options
Diffstat (limited to 'app/assets/javascripts/diffs/store/actions.js')
-rw-r--r-- | app/assets/javascripts/diffs/store/actions.js | 103 |
1 files changed, 82 insertions, 21 deletions
diff --git a/app/assets/javascripts/diffs/store/actions.js b/app/assets/javascripts/diffs/store/actions.js index 1c0e20183e2..e15d403bd11 100644 --- a/app/assets/javascripts/diffs/store/actions.js +++ b/app/assets/javascripts/diffs/store/actions.js @@ -10,7 +10,7 @@ import axios from '~/lib/utils/axios_utils'; import { HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK } from '~/lib/utils/http_status'; import Poll from '~/lib/utils/poll'; -import { mergeUrlParams, getLocationHash } from '~/lib/utils/url_utility'; +import { mergeUrlParams, getLocationHash, getParameterValues } from '~/lib/utils/url_utility'; import notesEventHub from '~/notes/event_hub'; import { generateTreeList } from '~/diffs/utils/tree_worker_utils'; import { sortTree } from '~/ide/stores/utils'; @@ -54,7 +54,6 @@ import { } from '../constants'; import { DISCUSSION_SINGLE_DIFF_FAILED, - LOAD_SINGLE_DIFF_FAILED, BUILDING_YOUR_MR, SOMETHING_WENT_WRONG, ERROR_LOADING_FULL_DIFF, @@ -119,7 +118,9 @@ export const setBaseConfig = ({ commit }, options) => { }; export const prefetchSingleFile = async ({ state, getters, commit }, treeEntry) => { - const versionPath = state.mergeRequestDiff?.version_path; + const url = new URL(state.endpointBatch, 'https://gitlab.com'); + const diffId = getParameterValues('diff_id', url)[0]; + const startSha = getParameterValues('start_sha', url)[0]; if ( treeEntry && @@ -133,12 +134,14 @@ export const prefetchSingleFile = async ({ state, getters, commit }, treeEntry) w: state.showWhitespace ? '0' : '1', view: 'inline', commit_id: getters.commitId, + diff_head: true, }; - if (versionPath) { - const { diffId, startSha } = getDerivedMergeRequestInformation({ endpoint: versionPath }); - + if (diffId) { urlParams.diff_id = diffId; + } + + if (startSha) { urlParams.start_sha = startSha; } @@ -161,7 +164,9 @@ export const prefetchSingleFile = async ({ state, getters, commit }, treeEntry) export const fetchFileByFile = async ({ state, getters, commit }) => { const isNoteLink = isUrlHashNoteLink(window?.location?.hash); const id = parseUrlHashAsFileHash(window?.location?.hash, state.currentDiffFileId); - const versionPath = state.mergeRequestDiff?.version_path; + const url = new URL(state.endpointBatch, 'https://gitlab.com'); + const diffId = getParameterValues('diff_id', url)[0]; + const startSha = getParameterValues('start_sha', url)[0]; const treeEntry = id ? getters.flatBlobsList.find(({ fileHash }) => fileHash === id) : getters.flatBlobsList[0]; @@ -179,12 +184,14 @@ export const fetchFileByFile = async ({ state, getters, commit }) => { w: state.showWhitespace ? '0' : '1', view: 'inline', commit_id: getters.commitId, + diff_head: true, }; - if (versionPath) { - const { diffId, startSha } = getDerivedMergeRequestInformation({ endpoint: versionPath }); - + if (diffId) { urlParams.diff_id = diffId; + } + + if (startSha) { urlParams.start_sha = startSha; } @@ -210,7 +217,7 @@ export const fetchFileByFile = async ({ state, getters, commit }) => { } }; -export const fetchDiffFilesBatch = ({ commit, state, dispatch }) => { +export const fetchDiffFilesBatch = ({ commit, state, dispatch }, pinnedFileLoading = false) => { let perPage = state.viewDiffsFileByFile ? 1 : state.perPage; let increaseAmount = 1.4; const startPage = 0; @@ -224,8 +231,10 @@ export const fetchDiffFilesBatch = ({ commit, state, dispatch }) => { let totalLoaded = 0; let scrolledVirtualScroller = hash === ''; - commit(types.SET_BATCH_LOADING_STATE, 'loading'); - commit(types.SET_RETRIEVING_BATCHES, true); + if (!pinnedFileLoading) { + commit(types.SET_BATCH_LOADING_STATE, 'loading'); + commit(types.SET_RETRIEVING_BATCHES, true); + } eventHub.$emit(EVT_PERF_MARK_DIFF_FILES_START); const getBatch = (page = startPage) => @@ -237,7 +246,7 @@ export const fetchDiffFilesBatch = ({ commit, state, dispatch }) => { commit(types.SET_DIFF_DATA_BATCH, { diff_files: diffFiles }); commit(types.SET_BATCH_LOADING_STATE, 'loaded'); - if (!scrolledVirtualScroller) { + if (!scrolledVirtualScroller && !pinnedFileLoading) { const index = state.diffFiles.findIndex( (f) => f.file_hash === hash || f[INLINE_DIFF_LINES_KEY].find((l) => l.line_code === hash), @@ -301,9 +310,10 @@ export const fetchDiffFilesBatch = ({ commit, state, dispatch }) => { return null; }) - .catch(() => { + .catch((error) => { commit(types.SET_RETRIEVING_BATCHES, false); commit(types.SET_BATCH_LOADING_STATE, 'error'); + throw error; }); return getBatch(); @@ -384,7 +394,11 @@ export const fetchCoverageFiles = ({ commit, state }) => { coveragePoll.makeRequest(); }; -export const setHighlightedRow = ({ commit }, lineCode) => { +export const setHighlightedRow = ({ commit }, { lineCode, event }) => { + if (event && event.target.href) { + event.preventDefault(); + window.history.replaceState(null, undefined, event.target.href); + } const fileHash = lineCode.split('_')[0]; commit(types.SET_HIGHLIGHTED_ROW, lineCode); commit(types.SET_CURRENT_DIFF_FILE, fileHash); @@ -657,6 +671,8 @@ export const goToFile = ({ state, commit, dispatch, getters }, { path }) => { } else { if (!state.treeEntries[path]) return; + dispatch('unpinFile'); + const { fileHash } = state.treeEntries[path]; commit(types.SET_CURRENT_DIFF_FILE, fileHash); @@ -667,11 +683,7 @@ export const goToFile = ({ state, commit, dispatch, getters }, { path }) => { scrollToElement('.diff-files-holder', { duration: 0 }); if (!getters.isTreePathLoaded(path)) { - dispatch('fetchFileByFile').catch(() => { - createAlert({ - message: LOAD_SINGLE_DIFF_FAILED, - }); - }); + dispatch('fetchFileByFile'); } } }; @@ -995,6 +1007,8 @@ export const setCurrentDiffFileIdFromNote = ({ commit, getters, rootGetters }, n }; export const navigateToDiffFileIndex = ({ state, getters, commit, dispatch }, index) => { + dispatch('unpinFile'); + const { fileHash } = getters.flatBlobsList[index]; document.location.hash = fileHash; @@ -1055,3 +1069,50 @@ export const toggleFileCommentForm = ({ state, commit }, filePath) => { export const addDraftToFile = ({ commit }, { filePath, draft }) => commit(types.ADD_DRAFT_TO_FILE, { filePath, draft }); + +export const fetchPinnedFile = ({ state, commit }, pinnedFileUrl) => { + const isNoteLink = isUrlHashNoteLink(window?.location?.hash); + + commit(types.SET_BATCH_LOADING_STATE, 'loading'); + commit(types.SET_RETRIEVING_BATCHES, true); + + return axios + .get(pinnedFileUrl) + .then(({ data: diffData }) => { + const [{ file_hash }] = diffData.diff_files; + + // we must store pinned file in the `diffs`, otherwise collapsing and commenting on a file won't work + // once the same file arrives in a file batch we must only update its' position + // we also must not update file's position since it's loaded out of order + commit(types.SET_DIFF_DATA_BATCH, { diff_files: diffData.diff_files, updatePosition: false }); + commit(types.SET_PINNED_FILE_HASH, file_hash); + + if (!isNoteLink && !state.currentDiffFileId) { + commit(types.SET_CURRENT_DIFF_FILE, file_hash); + } + + commit(types.SET_BATCH_LOADING_STATE, 'loaded'); + + setTimeout(() => { + handleLocationHash(); + }); + + eventHub.$emit('diffFilesModified'); + }) + .catch((error) => { + commit(types.SET_BATCH_LOADING_STATE, 'error'); + throw error; + }) + .finally(() => { + commit(types.SET_RETRIEVING_BATCHES, false); + }); +}; + +export const unpinFile = ({ getters, commit }) => { + if (!getters.pinnedFile) return; + commit(types.SET_PINNED_FILE_HASH, null); + const newUrl = new URL(window.location); + newUrl.searchParams.delete('pin'); + newUrl.hash = ''; + window.history.replaceState(null, undefined, newUrl); +}; |