debian-mirror-gitlab/app/assets/javascripts/notes/stores/collapse_utils.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { DESCRIPTION_TYPE, TIME_DIFFERENCE_VALUE } from '../constants';
2018-11-08 19:23:39 +05:30
/**
* Checks the time difference between two notes from their 'created_at' dates
* returns an integer
*/
export const getTimeDifferenceMinutes = (noteBeggining, noteEnd) => {
const descriptionNoteBegin = new Date(noteBeggining.created_at);
const descriptionNoteEnd = new Date(noteEnd.created_at);
const timeDifferenceMinutes = (descriptionNoteEnd - descriptionNoteBegin) / 1000 / 60;
return Math.ceil(timeDifferenceMinutes);
};
/**
* Checks if a note is a system note and if the content is description
*
* @param {Object} note
* @returns {Boolean}
*/
export const isDescriptionSystemNote = note => note.system && note.note === DESCRIPTION_TYPE;
/**
* Collapses the system notes of a description type, e.g. Changed the description, n minutes ago
* the notes will collapse as long as they happen no more than 10 minutes away from each away
* in between the notes can be anything, another type of system note
* (such as 'changed the weight') or a comment.
*
* @param {Array} notes
* @returns {Array}
*/
export const collapseSystemNotes = notes => {
let lastDescriptionSystemNote = null;
let lastDescriptionSystemNoteIndex = -1;
return notes.slice(0).reduce((acc, currentNote) => {
const note = currentNote.notes[0];
if (isDescriptionSystemNote(note)) {
// is it the first one?
if (!lastDescriptionSystemNote) {
lastDescriptionSystemNote = note;
lastDescriptionSystemNoteIndex = acc.length;
} else if (lastDescriptionSystemNote) {
2018-12-13 13:39:08 +05:30
const timeDifferenceMinutes = getTimeDifferenceMinutes(lastDescriptionSystemNote, note);
2018-11-08 19:23:39 +05:30
2019-12-26 22:10:19 +05:30
// are they less than 10 minutes apart from the same user?
2020-03-13 15:44:24 +05:30
if (
timeDifferenceMinutes > TIME_DIFFERENCE_VALUE ||
note.author.id !== lastDescriptionSystemNote.author.id ||
lastDescriptionSystemNote.description_version_deleted
) {
2018-11-08 19:23:39 +05:30
// update the previous system note
lastDescriptionSystemNote = note;
lastDescriptionSystemNoteIndex = acc.length;
} else {
2019-12-26 22:10:19 +05:30
// set the first version to fetch grouped system note versions
note.start_description_version_id = lastDescriptionSystemNote.description_version_id;
2018-11-08 19:23:39 +05:30
// delete the previous one
acc.splice(lastDescriptionSystemNoteIndex, 1);
// update the previous system note index
lastDescriptionSystemNoteIndex = acc.length;
}
}
}
2019-12-26 22:10:19 +05:30
2018-11-08 19:23:39 +05:30
acc.push(currentNote);
return acc;
}, []);
};