2024-07-07 15:32:30 +00:00
|
|
|
import {createTippy} from '../modules/tippy.ts';
|
|
|
|
import {GET} from '../modules/fetch.ts';
|
|
|
|
import {hideElem, showElem} from '../utils/dom.ts';
|
|
|
|
import {logoutFromWorker} from '../modules/worker.ts';
|
2021-01-21 14:51:52 +00:00
|
|
|
|
2024-02-23 21:19:54 +00:00
|
|
|
const {appSubUrl, notificationSettings, enableTimeTracking, assetVersionEncoded} = window.config;
|
2021-01-21 14:51:52 +00:00
|
|
|
|
2021-11-09 09:27:25 +00:00
|
|
|
export function initStopwatch() {
|
2021-10-21 07:37:43 +00:00
|
|
|
if (!enableTimeTracking) {
|
2021-02-19 23:06:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-21 14:51:52 +00:00
|
|
|
|
2024-04-30 14:52:46 +00:00
|
|
|
const stopwatchEls = document.querySelectorAll('.active-stopwatch');
|
2022-08-09 12:37:34 +00:00
|
|
|
const stopwatchPopup = document.querySelector('.active-stopwatch-popup');
|
2021-04-05 16:45:01 +00:00
|
|
|
|
2024-04-30 14:52:46 +00:00
|
|
|
if (!stopwatchEls.length || !stopwatchPopup) {
|
2021-04-05 16:45:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:58:27 +00:00
|
|
|
// global stop watch (in the head_navbar), it should always work in any case either the EventSource or the PeriodicPoller is used.
|
2024-04-30 14:52:46 +00:00
|
|
|
const seconds = stopwatchEls[0]?.getAttribute('data-seconds');
|
|
|
|
if (seconds) {
|
|
|
|
updateStopwatchTime(parseInt(seconds));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const stopwatchEl of stopwatchEls) {
|
|
|
|
stopwatchEl.removeAttribute('href'); // intended for noscript mode only
|
|
|
|
|
|
|
|
createTippy(stopwatchEl, {
|
2024-08-10 09:46:48 +00:00
|
|
|
content: stopwatchPopup.cloneNode(true) as Element,
|
2024-04-30 14:52:46 +00:00
|
|
|
placement: 'bottom-end',
|
|
|
|
trigger: 'click',
|
|
|
|
maxWidth: 'none',
|
|
|
|
interactive: true,
|
|
|
|
hideOnClick: true,
|
|
|
|
theme: 'default',
|
|
|
|
});
|
2022-08-03 19:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let usingPeriodicPoller = false;
|
|
|
|
const startPeriodicPoller = (timeout) => {
|
|
|
|
if (timeout <= 0 || !Number.isFinite(timeout)) return;
|
|
|
|
usingPeriodicPoller = true;
|
|
|
|
setTimeout(() => updateStopwatchWithCallback(startPeriodicPoller, timeout), timeout);
|
|
|
|
};
|
|
|
|
|
|
|
|
// if the browser supports EventSource and SharedWorker, use it instead of the periodic poller
|
|
|
|
if (notificationSettings.EventSourceUpdateTime > 0 && window.EventSource && window.SharedWorker) {
|
2021-02-19 10:05:35 +00:00
|
|
|
// Try to connect to the event source via the shared worker first
|
2022-08-23 12:58:04 +00:00
|
|
|
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js?v=${assetVersionEncoded}`, 'notification-worker');
|
2021-02-19 10:05:35 +00:00
|
|
|
worker.addEventListener('error', (event) => {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('worker error', event);
|
2021-02-19 10:05:35 +00:00
|
|
|
});
|
2021-08-17 05:32:48 +00:00
|
|
|
worker.port.addEventListener('messageerror', () => {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('unable to deserialize message');
|
2021-08-17 05:32:48 +00:00
|
|
|
});
|
2021-02-19 10:05:35 +00:00
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'start',
|
2021-10-21 07:37:43 +00:00
|
|
|
url: `${window.location.origin}${appSubUrl}/user/events`,
|
2021-02-19 10:05:35 +00:00
|
|
|
});
|
|
|
|
worker.port.addEventListener('message', (event) => {
|
|
|
|
if (!event.data || !event.data.type) {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('unknown worker message event', event);
|
2021-02-19 10:05:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.data.type === 'stopwatches') {
|
|
|
|
updateStopwatchData(JSON.parse(event.data.data));
|
2022-08-03 19:58:27 +00:00
|
|
|
} else if (event.data.type === 'no-event-source') {
|
|
|
|
// browser doesn't support EventSource, falling back to periodic poller
|
|
|
|
if (!usingPeriodicPoller) startPeriodicPoller(notificationSettings.MinTimeout);
|
2021-02-19 10:05:35 +00:00
|
|
|
} else if (event.data.type === 'error') {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('worker port event error', event.data);
|
2021-02-19 10:05:35 +00:00
|
|
|
} else if (event.data.type === 'logout') {
|
2021-04-07 23:48:13 +00:00
|
|
|
if (event.data.data !== 'here') {
|
2021-02-19 10:05:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2024-04-30 15:35:42 +00:00
|
|
|
logoutFromWorker();
|
2021-04-04 21:37:50 +00:00
|
|
|
} else if (event.data.type === 'close') {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2021-02-19 10:05:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
worker.port.addEventListener('error', (e) => {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('worker port error', e);
|
2021-02-19 10:05:35 +00:00
|
|
|
});
|
|
|
|
worker.port.start();
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:58:27 +00:00
|
|
|
startPeriodicPoller(notificationSettings.MinTimeout);
|
2021-01-21 14:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateStopwatchWithCallback(callback, timeout) {
|
|
|
|
const isSet = await updateStopwatch();
|
|
|
|
|
|
|
|
if (!isSet) {
|
2021-10-21 07:37:43 +00:00
|
|
|
timeout = notificationSettings.MinTimeout;
|
|
|
|
} else if (timeout < notificationSettings.MaxTimeout) {
|
|
|
|
timeout += notificationSettings.TimeoutStep;
|
2021-01-21 14:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateStopwatch() {
|
2024-02-23 21:19:54 +00:00
|
|
|
const response = await GET(`${appSubUrl}/user/stopwatches`);
|
|
|
|
if (!response.ok) {
|
|
|
|
console.error('Failed to fetch stopwatch data');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const data = await response.json();
|
2021-02-19 10:05:35 +00:00
|
|
|
return updateStopwatchData(data);
|
|
|
|
}
|
|
|
|
|
2021-11-12 12:37:45 +00:00
|
|
|
function updateStopwatchData(data) {
|
2021-01-21 14:51:52 +00:00
|
|
|
const watch = data[0];
|
2024-04-30 14:52:46 +00:00
|
|
|
const btnEls = document.querySelectorAll('.active-stopwatch');
|
2021-01-21 14:51:52 +00:00
|
|
|
if (!watch) {
|
2024-04-30 14:52:46 +00:00
|
|
|
hideElem(btnEls);
|
2021-01-21 14:51:52 +00:00
|
|
|
} else {
|
|
|
|
const {repo_owner_name, repo_name, issue_index, seconds} = watch;
|
2021-10-21 07:37:43 +00:00
|
|
|
const issueUrl = `${appSubUrl}/${repo_owner_name}/${repo_name}/issues/${issue_index}`;
|
2024-02-23 21:19:54 +00:00
|
|
|
document.querySelector('.stopwatch-link')?.setAttribute('href', issueUrl);
|
|
|
|
document.querySelector('.stopwatch-commit')?.setAttribute('action', `${issueUrl}/times/stopwatch/toggle`);
|
|
|
|
document.querySelector('.stopwatch-cancel')?.setAttribute('action', `${issueUrl}/times/stopwatch/cancel`);
|
|
|
|
const stopwatchIssue = document.querySelector('.stopwatch-issue');
|
|
|
|
if (stopwatchIssue) stopwatchIssue.textContent = `${repo_owner_name}/${repo_name}#${issue_index}`;
|
2022-08-03 19:58:27 +00:00
|
|
|
updateStopwatchTime(seconds);
|
2024-04-30 14:52:46 +00:00
|
|
|
showElem(btnEls);
|
2021-01-21 14:51:52 +00:00
|
|
|
}
|
2022-10-10 12:02:20 +00:00
|
|
|
return Boolean(data.length);
|
2021-01-21 14:51:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 14:52:46 +00:00
|
|
|
// TODO: This flickers on page load, we could avoid this by making a custom
|
|
|
|
// element to render time periods. Feeding a datetime in backend does not work
|
|
|
|
// when time zone between server and client differs.
|
2021-11-12 12:37:45 +00:00
|
|
|
function updateStopwatchTime(seconds) {
|
2024-04-30 14:52:46 +00:00
|
|
|
if (!Number.isFinite(seconds)) return;
|
|
|
|
const datetime = (new Date(Date.now() - seconds * 1000)).toISOString();
|
|
|
|
for (const parent of document.querySelectorAll('.header-stopwatch-dot')) {
|
|
|
|
const existing = parent.querySelector(':scope > relative-time');
|
|
|
|
if (existing) {
|
|
|
|
existing.setAttribute('datetime', datetime);
|
|
|
|
} else {
|
|
|
|
const el = document.createElement('relative-time');
|
|
|
|
el.setAttribute('format', 'micro');
|
|
|
|
el.setAttribute('datetime', datetime);
|
|
|
|
el.setAttribute('lang', 'en-US');
|
|
|
|
el.setAttribute('title', ''); // make <relative-time> show no title and therefor no tooltip
|
|
|
|
parent.append(el);
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 14:51:52 +00:00
|
|
|
}
|