2022-01-29 02:30:11 +05:30
|
|
|
import $ from 'jquery';
|
2021-01-21 20:21:52 +05:30
|
|
|
import prettyMilliseconds from 'pretty-ms';
|
|
|
|
|
2022-01-29 02:30:11 +05:30
|
|
|
const {appSubUrl, csrfToken, notificationSettings, enableTimeTracking} = window.config;
|
2021-01-21 20:21:52 +05:30
|
|
|
let updateTimeInterval = null; // holds setInterval id when active
|
|
|
|
|
2021-11-09 14:57:25 +05:30
|
|
|
export function initStopwatch() {
|
2021-10-21 13:07:43 +05:30
|
|
|
if (!enableTimeTracking) {
|
2021-02-20 04:36:56 +05:30
|
|
|
return;
|
|
|
|
}
|
2021-01-21 20:21:52 +05:30
|
|
|
|
2021-02-20 04:36:56 +05:30
|
|
|
const stopwatchEl = $('.active-stopwatch-trigger');
|
2021-04-05 22:15:01 +05:30
|
|
|
|
|
|
|
if (!stopwatchEl.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:21:52 +05:30
|
|
|
stopwatchEl.removeAttr('href'); // intended for noscript mode only
|
|
|
|
stopwatchEl.popup({
|
|
|
|
position: 'bottom right',
|
|
|
|
hoverable: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// form handlers
|
|
|
|
$('form > button', stopwatchEl).on('click', function () {
|
|
|
|
$(this).parent().trigger('submit');
|
|
|
|
});
|
|
|
|
|
2021-10-21 13:07:43 +05:30
|
|
|
if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
|
2021-02-19 15:35:35 +05:30
|
|
|
// Try to connect to the event source via the shared worker first
|
|
|
|
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
|
|
|
|
worker.addEventListener('error', (event) => {
|
|
|
|
console.error(event);
|
|
|
|
});
|
2021-08-17 11:02:48 +05:30
|
|
|
worker.port.addEventListener('messageerror', () => {
|
2021-02-19 15:35:35 +05:30
|
|
|
console.error('Unable to deserialize message');
|
2021-08-17 11:02:48 +05:30
|
|
|
});
|
2021-02-19 15:35:35 +05:30
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'start',
|
2021-10-21 13:07:43 +05:30
|
|
|
url: `${window.location.origin}${appSubUrl}/user/events`,
|
2021-02-19 15:35:35 +05:30
|
|
|
});
|
|
|
|
worker.port.addEventListener('message', (event) => {
|
|
|
|
if (!event.data || !event.data.type) {
|
|
|
|
console.error(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.data.type === 'stopwatches') {
|
|
|
|
updateStopwatchData(JSON.parse(event.data.data));
|
|
|
|
} else if (event.data.type === 'error') {
|
|
|
|
console.error(event.data);
|
|
|
|
} else if (event.data.type === 'logout') {
|
2021-04-08 05:18:13 +05:30
|
|
|
if (event.data.data !== 'here') {
|
2021-02-19 15:35:35 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2021-10-21 13:07:43 +05:30
|
|
|
window.location.href = appSubUrl;
|
2021-04-05 03:07:50 +05:30
|
|
|
} else if (event.data.type === 'close') {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2021-02-19 15:35:35 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
worker.port.addEventListener('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
worker.port.start();
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-21 13:07:43 +05:30
|
|
|
if (notificationSettings.MinTimeout <= 0) {
|
2021-01-21 20:21:52 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn = (timeout) => {
|
2021-11-12 18:07:45 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
const _promise = updateStopwatchWithCallback(fn, timeout);
|
2021-01-21 20:21:52 +05:30
|
|
|
}, timeout);
|
|
|
|
};
|
|
|
|
|
2021-10-21 13:07:43 +05:30
|
|
|
fn(notificationSettings.MinTimeout);
|
2021-01-21 20:21:52 +05:30
|
|
|
|
|
|
|
const currSeconds = $('.stopwatch-time').data('seconds');
|
|
|
|
if (currSeconds) {
|
|
|
|
updateTimeInterval = updateStopwatchTime(currSeconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateStopwatchWithCallback(callback, timeout) {
|
|
|
|
const isSet = await updateStopwatch();
|
|
|
|
|
|
|
|
if (!isSet) {
|
2021-10-21 13:07:43 +05:30
|
|
|
timeout = notificationSettings.MinTimeout;
|
|
|
|
} else if (timeout < notificationSettings.MaxTimeout) {
|
|
|
|
timeout += notificationSettings.TimeoutStep;
|
2021-01-21 20:21:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
callback(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateStopwatch() {
|
|
|
|
const data = await $.ajax({
|
|
|
|
type: 'GET',
|
2022-04-08 00:29:56 +05:30
|
|
|
url: `${appSubUrl}/user/stopwatches`,
|
2021-10-21 13:07:43 +05:30
|
|
|
headers: {'X-Csrf-Token': csrfToken},
|
2021-01-21 20:21:52 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
if (updateTimeInterval) {
|
|
|
|
clearInterval(updateTimeInterval);
|
|
|
|
updateTimeInterval = null;
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:35:35 +05:30
|
|
|
return updateStopwatchData(data);
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:07:45 +05:30
|
|
|
function updateStopwatchData(data) {
|
2021-01-21 20:21:52 +05:30
|
|
|
const watch = data[0];
|
|
|
|
const btnEl = $('.active-stopwatch-trigger');
|
|
|
|
if (!watch) {
|
2022-04-26 02:15:22 +05:30
|
|
|
if (updateTimeInterval) {
|
|
|
|
clearInterval(updateTimeInterval);
|
|
|
|
updateTimeInterval = null;
|
|
|
|
}
|
2021-01-21 20:21:52 +05:30
|
|
|
btnEl.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
const {repo_owner_name, repo_name, issue_index, seconds} = watch;
|
2021-10-21 13:07:43 +05:30
|
|
|
const issueUrl = `${appSubUrl}/${repo_owner_name}/${repo_name}/issues/${issue_index}`;
|
2021-01-21 20:21:52 +05:30
|
|
|
$('.stopwatch-link').attr('href', issueUrl);
|
|
|
|
$('.stopwatch-commit').attr('action', `${issueUrl}/times/stopwatch/toggle`);
|
|
|
|
$('.stopwatch-cancel').attr('action', `${issueUrl}/times/stopwatch/cancel`);
|
|
|
|
$('.stopwatch-issue').text(`${repo_owner_name}/${repo_name}#${issue_index}`);
|
|
|
|
$('.stopwatch-time').text(prettyMilliseconds(seconds * 1000));
|
2021-11-12 18:07:45 +05:30
|
|
|
updateStopwatchTime(seconds);
|
2021-01-21 20:21:52 +05:30
|
|
|
btnEl.removeClass('hidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!data.length;
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:07:45 +05:30
|
|
|
function updateStopwatchTime(seconds) {
|
2021-01-21 20:21:52 +05:30
|
|
|
const secs = parseInt(seconds);
|
|
|
|
if (!Number.isFinite(secs)) return;
|
|
|
|
|
|
|
|
const start = Date.now();
|
|
|
|
updateTimeInterval = setInterval(() => {
|
|
|
|
const delta = Date.now() - start;
|
|
|
|
const dur = prettyMilliseconds(secs * 1000 + delta, {compact: true});
|
|
|
|
$('.stopwatch-time').text(dur);
|
|
|
|
}, 1000);
|
|
|
|
}
|