debian-mirror-gitlab/app/assets/javascripts/diffs/store/getters.js

176 lines
5 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { __, n__ } from '~/locale';
2021-02-22 17:27:13 +05:30
import {
PARALLEL_DIFF_VIEW_TYPE,
INLINE_DIFF_VIEW_TYPE,
INLINE_DIFF_LINES_KEY,
} from '../constants';
2021-03-11 19:13:27 +05:30
import { computeSuggestionCommitMessage } from '../utils/suggestions';
import { parallelizeDiffLines } from './utils';
2018-11-08 19:23:39 +05:30
2020-04-22 19:07:51 +05:30
export * from './getters_versions_dropdowns';
2021-03-08 18:12:59 +05:30
export const isParallelView = (state) => state.diffViewType === PARALLEL_DIFF_VIEW_TYPE;
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
export const isInlineView = (state) => state.diffViewType === INLINE_DIFF_VIEW_TYPE;
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
export const whichCollapsedTypes = (state) => {
const automatic = state.diffFiles.some((file) => file.viewer?.automaticallyCollapsed);
const manual = state.diffFiles.some((file) => file.viewer?.manuallyCollapsed);
2021-01-29 00:20:46 +05:30
return {
any: automatic || manual,
automatic,
manual,
};
};
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
export const commitId = (state) => (state.commit && state.commit.id ? state.commit.id : null);
2018-11-08 19:23:39 +05:30
/**
* Checks if the diff has all discussions expanded
* @param {Object} diff
* @returns {Boolean}
*/
2021-03-08 18:12:59 +05:30
export const diffHasAllExpandedDiscussions = (state, getters) => (diff) => {
2018-11-08 19:23:39 +05:30
const discussions = getters.getDiffFileDiscussions(diff);
2018-11-20 20:47:30 +05:30
return (
2021-03-08 18:12:59 +05:30
(discussions && discussions.length && discussions.every((discussion) => discussion.expanded)) ||
2018-11-20 20:47:30 +05:30
false
);
2018-11-08 19:23:39 +05:30
};
/**
2019-02-15 15:39:39 +05:30
* Checks if the diff has all discussions collapsed
2018-11-08 19:23:39 +05:30
* @param {Object} diff
* @returns {Boolean}
*/
2021-03-08 18:12:59 +05:30
export const diffHasAllCollapsedDiscussions = (state, getters) => (diff) => {
2018-11-08 19:23:39 +05:30
const discussions = getters.getDiffFileDiscussions(diff);
2018-11-20 20:47:30 +05:30
return (
2021-03-08 18:12:59 +05:30
(discussions &&
discussions.length &&
discussions.every((discussion) => !discussion.expanded)) ||
2018-11-20 20:47:30 +05:30
false
);
2018-11-08 19:23:39 +05:30
};
/**
* Checks if the diff has any open discussions
* @param {Object} diff
* @returns {Boolean}
*/
2021-03-08 18:12:59 +05:30
export const diffHasExpandedDiscussions = () => (diff) => {
return diff[INLINE_DIFF_LINES_KEY].filter((l) => l.discussions.length >= 1).some(
(l) => l.discussionsExpanded,
2021-02-22 17:27:13 +05:30
);
2018-11-08 19:23:39 +05:30
};
2018-11-18 11:00:15 +05:30
/**
* Checks if the diff has any discussion
* @param {Boolean} diff
* @returns {Boolean}
*/
2021-03-08 18:12:59 +05:30
export const diffHasDiscussions = () => (diff) => {
return diff[INLINE_DIFF_LINES_KEY].some((l) => l.discussions.length >= 1);
2021-01-03 14:25:43 +05:30
};
2018-11-18 11:00:15 +05:30
2018-11-08 19:23:39 +05:30
/**
* Returns an array with the discussions of the given diff
* @param {Object} diff
* @returns {Array}
*/
2021-03-08 18:12:59 +05:30
export const getDiffFileDiscussions = (state, getters, rootState, rootGetters) => (diff) =>
2018-11-08 19:23:39 +05:30
rootGetters.discussions.filter(
2021-03-08 18:12:59 +05:30
(discussion) => discussion.diff_discussion && discussion.diff_file.file_hash === diff.file_hash,
2018-11-08 19:23:39 +05:30
) || [];
2021-03-08 18:12:59 +05:30
export const getDiffFileByHash = (state) => (fileHash) =>
state.diffFiles.find((file) => file.file_hash === fileHash);
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
export const flatBlobsList = (state) =>
Object.values(state.treeEntries).filter((f) => f.type === 'blob');
2019-03-02 22:35:43 +05:30
export const allBlobs = (state, getters) =>
getters.flatBlobsList.reduce((acc, file) => {
const { parentPath } = file;
2021-03-08 18:12:59 +05:30
if (parentPath && !acc.some((f) => f.path === parentPath)) {
2019-03-02 22:35:43 +05:30
acc.push({
path: parentPath,
isHeader: true,
tree: [],
});
}
2021-03-08 18:12:59 +05:30
acc.find((f) => f.path === parentPath).tree.push(file);
2019-03-02 22:35:43 +05:30
return acc;
}, []);
2018-12-05 23:21:45 +05:30
2021-03-08 18:12:59 +05:30
export const getCommentFormForDiffFile = (state) => (fileHash) =>
state.commentForms.find((form) => form.fileHash === fileHash);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
/**
* Returns the test coverage hits for a specific line of a given file
* @param {string} file
* @param {number} line
* @returns {number}
*/
2021-03-08 18:12:59 +05:30
export const fileLineCoverage = (state) => (file, line) => {
2020-04-08 14:13:33 +05:30
if (!state.coverageFiles.files) return {};
const fileCoverage = state.coverageFiles.files[file];
if (!fileCoverage) return {};
const lineCoverage = fileCoverage[String(line)];
if (lineCoverage === 0) {
return { text: __('No test coverage'), class: 'no-coverage' };
} else if (lineCoverage >= 0) {
return {
text: n__('Test coverage: %d hit', 'Test coverage: %d hits', lineCoverage),
class: 'coverage',
};
}
return {};
};
2019-07-07 11:18:12 +05:30
/**
* Returns index of a currently selected diff in diffFiles
* @returns {number}
*/
2021-03-08 18:12:59 +05:30
export const currentDiffIndex = (state) =>
Math.max(
0,
state.diffFiles.findIndex((diff) => diff.file_hash === state.currentDiffFileId),
);
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
export const diffLines = (state) => (file, unifiedDiffComponents) => {
2021-01-29 00:20:46 +05:30
if (!unifiedDiffComponents && state.diffViewType === INLINE_DIFF_VIEW_TYPE) {
2020-11-24 15:15:51 +05:30
return null;
}
2021-01-29 00:20:46 +05:30
return parallelizeDiffLines(
file.highlighted_diff_lines || [],
state.diffViewType === INLINE_DIFF_VIEW_TYPE,
);
2020-11-24 15:15:51 +05:30
};
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
export function suggestionCommitMessage(state, _, rootState) {
2021-03-11 19:13:27 +05:30
return (values = {}) =>
computeSuggestionCommitMessage({
message: state.defaultSuggestionCommitMessage,
values: {
2021-04-29 21:17:54 +05:30
branch_name: rootState.page.mrMetadata.branch_name,
project_path: rootState.page.mrMetadata.project_path,
project_name: rootState.page.mrMetadata.project_name,
username: rootState.page.mrMetadata.username,
user_full_name: rootState.page.mrMetadata.user_full_name,
2021-03-11 19:13:27 +05:30
...values,
},
});
2021-03-08 18:12:59 +05:30
}
2021-06-08 01:23:25 +05:30
export const isVirtualScrollingEnabled = (state) =>
!state.viewDiffsFileByFile && window.gon?.features?.diffsVirtualScrolling;