debian-mirror-gitlab/app/assets/javascripts/diffs/utils/file_reviews.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
function getFileReviewsKey(mrPath) {
return `${mrPath}-file-reviews`;
}
2021-03-11 19:13:27 +05:30
export function isFileReviewed(reviews, file) {
const fileReviews = reviews[file.file_identifier_hash];
return file?.id && fileReviews?.length ? new Set(fileReviews).has(file.id) : false;
}
export function reviewStatuses(files, reviews) {
2021-04-17 20:07:23 +05:30
return files.reduce((flat, file) => {
return {
...flat,
[file.id]: isFileReviewed(reviews, file),
};
}, {});
2021-03-11 19:13:27 +05:30
}
2021-03-08 18:12:59 +05:30
export function getReviewsForMergeRequest(mrPath) {
const reviewsForMr = localStorage.getItem(getFileReviewsKey(mrPath));
let reviews = {};
if (reviewsForMr) {
try {
reviews = JSON.parse(reviewsForMr);
} catch (err) {
reviews = {};
}
}
return reviews;
}
export function setReviewsForMergeRequest(mrPath, reviews) {
localStorage.setItem(getFileReviewsKey(mrPath), JSON.stringify(reviews));
return reviews;
}
export function reviewable(file) {
return Boolean(file.id) && Boolean(file.file_identifier_hash);
}
export function markFileReview(reviews, file, reviewed = true) {
const usableReviews = { ...(reviews || {}) };
2021-03-11 19:13:27 +05:30
const updatedReviews = usableReviews;
2021-03-08 18:12:59 +05:30
let fileReviews;
if (reviewable(file)) {
2021-03-11 19:13:27 +05:30
fileReviews = new Set(usableReviews[file.file_identifier_hash] || []);
2021-03-08 18:12:59 +05:30
if (reviewed) {
fileReviews.add(file.id);
} else {
fileReviews.delete(file.id);
}
2021-03-11 19:13:27 +05:30
updatedReviews[file.file_identifier_hash] = Array.from(fileReviews);
2021-03-08 18:12:59 +05:30
if (updatedReviews[file.file_identifier_hash].length === 0) {
delete updatedReviews[file.file_identifier_hash];
}
}
return updatedReviews;
}