diff options
Diffstat (limited to 'app/assets/javascripts/diffs/store/actions.js')
-rw-r--r-- | app/assets/javascripts/diffs/store/actions.js | 68 |
1 files changed, 59 insertions, 9 deletions
diff --git a/app/assets/javascripts/diffs/store/actions.js b/app/assets/javascripts/diffs/store/actions.js index a8d348e1836..d469ed8ee82 100644 --- a/app/assets/javascripts/diffs/store/actions.js +++ b/app/assets/javascripts/diffs/store/actions.js @@ -25,7 +25,6 @@ import { DIFF_VIEW_COOKIE_NAME, MR_TREE_SHOW_KEY, TREE_LIST_STORAGE_KEY, - WHITESPACE_STORAGE_KEY, TREE_LIST_WIDTH_STORAGE_KEY, OLD_LINE_KEY, NEW_LINE_KEY, @@ -38,6 +37,9 @@ import { INLINE_DIFF_LINES_KEY, PARALLEL_DIFF_LINES_KEY, DIFFS_PER_PAGE, + DIFF_WHITESPACE_COOKIE_NAME, + SHOW_WHITESPACE, + NO_SHOW_WHITESPACE, } from '../constants'; import { diffViewerModes } from '~/ide/constants'; @@ -103,7 +105,9 @@ export const fetchDiffFiles = ({ state, commit }) => { .catch(() => worker.terminate()); }; -export const fetchDiffFilesBatch = ({ commit, state }) => { +export const fetchDiffFilesBatch = ({ commit, state, dispatch }) => { + const id = window?.location?.hash; + const isNoteLink = id.indexOf('#note') === 0; const urlParams = { per_page: DIFFS_PER_PAGE, w: state.showWhitespace ? '0' : '1', @@ -123,16 +127,36 @@ export const fetchDiffFilesBatch = ({ commit, state }) => { commit(types.SET_DIFF_DATA_BATCH, { diff_files }); commit(types.SET_BATCH_LOADING, false); + if (!isNoteLink && !state.currentDiffFileId) { + commit(types.UPDATE_CURRENT_DIFF_FILE_ID, diff_files[0].file_hash); + } + + if (isNoteLink) { + dispatch('setCurrentDiffFileIdFromNote', id.split('_').pop()); + } + if (!pagination.next_page) { commit(types.SET_RETRIEVING_BATCHES, false); + + // We need to check that the currentDiffFileId points to a file that exists + if ( + state.currentDiffFileId && + !state.diffFiles.some(f => f.file_hash === state.currentDiffFileId) && + !isNoteLink + ) { + commit(types.UPDATE_CURRENT_DIFF_FILE_ID, state.diffFiles[0].file_hash); + } + if (gon.features?.codeNavigation) { // eslint-disable-next-line promise/catch-or-return,promise/no-nesting import('~/code_navigation').then(m => m.default({ - blobs: state.diffFiles.map(f => ({ - path: f.new_path, - codeNavigationPath: f.code_navigation_path, - })), + blobs: state.diffFiles + .filter(f => f.code_navigation_path) + .map(f => ({ + path: f.new_path, + codeNavigationPath: f.code_navigation_path, + })), definitionPathPrefix: state.definitionPathPrefix, }), ); @@ -211,9 +235,11 @@ export const setHighlightedRow = ({ commit }, lineCode) => { // This is adding line discussions to the actual lines in the diff tree // once for parallel and once for inline mode export const assignDiscussionsToDiff = ( - { commit, state, rootState }, + { commit, state, rootState, dispatch }, discussions = rootState.notes.discussions, ) => { + const id = window?.location?.hash; + const isNoteLink = id.indexOf('#note') === 0; const diffPositionByLineCode = getDiffPositionByLineCode( state.diffFiles, state.useSingleDiffStyle, @@ -230,6 +256,10 @@ export const assignDiscussionsToDiff = ( }); }); + if (isNoteLink) { + dispatch('setCurrentDiffFileIdFromNote', id.split('_').pop()); + } + Vue.nextTick(() => { eventHub.$emit('scrollToDiscussion'); }); @@ -448,6 +478,8 @@ export const toggleTreeOpen = ({ commit }, path) => { }; export const scrollToFile = ({ state, commit }, path) => { + if (!state.treeEntries[path]) return; + const { fileHash } = state.treeEntries[path]; document.location.hash = fileHash; @@ -484,11 +516,12 @@ export const setRenderTreeList = ({ commit }, renderTreeList) => { export const setShowWhitespace = ({ commit }, { showWhitespace, pushState = false }) => { commit(types.SET_SHOW_WHITESPACE, showWhitespace); + const w = showWhitespace ? SHOW_WHITESPACE : NO_SHOW_WHITESPACE; - localStorage.setItem(WHITESPACE_STORAGE_KEY, showWhitespace); + Cookies.set(DIFF_WHITESPACE_COOKIE_NAME, w); if (pushState) { - historyPushState(mergeUrlParams({ w: showWhitespace ? '0' : '1' }, window.location.href)); + historyPushState(mergeUrlParams({ w }, window.location.href)); } eventHub.$emit('refetchDiffData'); @@ -710,5 +743,22 @@ export function moveToNeighboringCommit({ dispatch, state }, { direction }) { } } +export const setCurrentDiffFileIdFromNote = ({ commit, rootGetters }, noteId) => { + const note = rootGetters.notesById[noteId]; + + if (!note) return; + + const fileHash = rootGetters.getDiscussion(note.discussion_id).diff_file.file_hash; + + commit(types.UPDATE_CURRENT_DIFF_FILE_ID, fileHash); +}; + +export const navigateToDiffFileIndex = ({ commit, state }, index) => { + const fileHash = state.diffFiles[index].file_hash; + document.location.hash = fileHash; + + commit(types.UPDATE_CURRENT_DIFF_FILE_ID, fileHash); +}; + // prevent babel-plugin-rewire from generating an invalid default during karma tests export default () => {}; |