debian-mirror-gitlab/app/assets/javascripts/commons/nav/user_merge_requests.js

101 lines
3 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import { getUserCounts } from '~/rest_api';
2019-09-30 21:07:59 +05:30
let channel;
function broadcastCount(newCount) {
if (!channel) {
return;
}
channel.postMessage(newCount);
}
function updateUserMergeRequestCounts(newCount) {
2021-03-08 18:12:59 +05:30
const mergeRequestsCountEl = document.querySelector('.js-assigned-mr-count');
mergeRequestsCountEl.textContent = newCount.toLocaleString();
}
function updateReviewerMergeRequestCounts(newCount) {
const mergeRequestsCountEl = document.querySelector('.js-reviewer-mr-count');
mergeRequestsCountEl.textContent = newCount.toLocaleString();
}
function updateMergeRequestCounts(newCount) {
const mergeRequestsCountEl = document.querySelector('.js-merge-requests-count');
2019-09-30 21:07:59 +05:30
mergeRequestsCountEl.textContent = newCount.toLocaleString();
2022-05-07 20:08:51 +05:30
mergeRequestsCountEl.classList.toggle('gl-display-none', Number(newCount) === 0);
}
function updateAttentionRequestsCount(count) {
const attentionCountEl = document.querySelector('.js-attention-count');
attentionCountEl.textContent = count.toLocaleString();
if (Number(count) === 0) {
attentionCountEl.classList.replace('badge-warning', 'badge-neutral');
} else {
attentionCountEl.classList.replace('badge-neutral', 'badge-warning');
}
2019-09-30 21:07:59 +05:30
}
/**
* Refresh user counts (and broadcast if open)
*/
export function refreshUserMergeRequestCounts() {
2021-03-08 18:12:59 +05:30
return getUserCounts()
2019-09-30 21:07:59 +05:30
.then(({ data }) => {
2022-05-07 20:08:51 +05:30
const attentionRequestsEnabled = window.gon?.features?.mrAttentionRequests;
2021-03-08 18:12:59 +05:30
const assignedMergeRequests = data.assigned_merge_requests;
const reviewerMergeRequests = data.review_requested_merge_requests;
2022-05-07 20:08:51 +05:30
const attentionRequests = data.attention_requests;
const fullCount = attentionRequestsEnabled
? attentionRequests
: assignedMergeRequests + reviewerMergeRequests;
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
updateUserMergeRequestCounts(assignedMergeRequests);
updateReviewerMergeRequestCounts(reviewerMergeRequests);
updateMergeRequestCounts(fullCount);
broadcastCount(fullCount);
2022-05-07 20:08:51 +05:30
if (attentionRequestsEnabled) {
updateAttentionRequestsCount(attentionRequests);
}
2019-09-30 21:07:59 +05:30
})
2021-03-08 18:12:59 +05:30
.catch((ex) => {
2019-09-30 21:07:59 +05:30
console.error(ex); // eslint-disable-line no-console
});
}
/**
* Close the broadcast channel for user counts
*/
export function closeUserCountsBroadcast() {
if (!channel) {
return;
}
channel.close();
channel = null;
}
/**
* Open the broadcast channel for user counts, adds user id so we only update
*
* **Please note:**
* Not supported in all browsers, but not polyfilling for now
* to keep bundle size small and
* no special functionality lost except cross tab notifications
*/
export function openUserCountsBroadcast() {
closeUserCountsBroadcast();
if (window.BroadcastChannel) {
const currentUserId = typeof gon !== 'undefined' && gon && gon.current_user_id;
if (currentUserId) {
channel = new BroadcastChannel(`mr_count_channel_${currentUserId}`);
2021-03-08 18:12:59 +05:30
channel.onmessage = (ev) => {
updateMergeRequestCounts(ev.data);
2019-09-30 21:07:59 +05:30
};
}
}
}