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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.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);
}
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 }) => {
2021-03-08 18:12:59 +05:30
const assignedMergeRequests = data.assigned_merge_requests;
const reviewerMergeRequests = data.review_requested_merge_requests;
2022-08-27 11:52:29 +05:30
const fullCount = 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);
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
};
}
}
}