|
| 1 | +import prettyMilliseconds from 'pretty-ms'; |
| 2 | +const {AppSubUrl, csrf, NotificationSettings} = window.config; |
| 3 | + |
| 4 | +let updateTimeInterval = null; // holds setInterval id when active |
| 5 | + |
| 6 | +export async function initStopwatch() { |
| 7 | + const stopwatchEl = $('.active-stopwatch-trigger'); |
| 8 | + |
| 9 | + stopwatchEl.removeAttr('href'); // intended for noscript mode only |
| 10 | + stopwatchEl.popup({ |
| 11 | + position: 'bottom right', |
| 12 | + hoverable: true, |
| 13 | + }); |
| 14 | + |
| 15 | + // form handlers |
| 16 | + $('form > button', stopwatchEl).on('click', function () { |
| 17 | + $(this).parent().trigger('submit'); |
| 18 | + }); |
| 19 | + |
| 20 | + if (!stopwatchEl || NotificationSettings.MinTimeout <= 0) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const fn = (timeout) => { |
| 25 | + setTimeout(async () => { |
| 26 | + await updateStopwatchWithCallback(fn, timeout); |
| 27 | + }, timeout); |
| 28 | + }; |
| 29 | + |
| 30 | + fn(NotificationSettings.MinTimeout); |
| 31 | + |
| 32 | + const currSeconds = $('.stopwatch-time').data('seconds'); |
| 33 | + if (currSeconds) { |
| 34 | + updateTimeInterval = updateStopwatchTime(currSeconds); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +async function updateStopwatchWithCallback(callback, timeout) { |
| 39 | + const isSet = await updateStopwatch(); |
| 40 | + |
| 41 | + if (!isSet) { |
| 42 | + timeout = NotificationSettings.MinTimeout; |
| 43 | + } else if (timeout < NotificationSettings.MaxTimeout) { |
| 44 | + timeout += NotificationSettings.TimeoutStep; |
| 45 | + } |
| 46 | + |
| 47 | + callback(timeout); |
| 48 | +} |
| 49 | + |
| 50 | +async function updateStopwatch() { |
| 51 | + const data = await $.ajax({ |
| 52 | + type: 'GET', |
| 53 | + url: `${AppSubUrl}/api/v1/user/stopwatches`, |
| 54 | + headers: {'X-Csrf-Token': csrf}, |
| 55 | + }); |
| 56 | + |
| 57 | + if (updateTimeInterval) { |
| 58 | + clearInterval(updateTimeInterval); |
| 59 | + updateTimeInterval = null; |
| 60 | + } |
| 61 | + |
| 62 | + const watch = data[0]; |
| 63 | + const btnEl = $('.active-stopwatch-trigger'); |
| 64 | + if (!watch) { |
| 65 | + btnEl.addClass('hidden'); |
| 66 | + } else { |
| 67 | + const {repo_owner_name, repo_name, issue_index, seconds} = watch; |
| 68 | + const issueUrl = `${AppSubUrl}/${repo_owner_name}/${repo_name}/issues/${issue_index}`; |
| 69 | + $('.stopwatch-link').attr('href', issueUrl); |
| 70 | + $('.stopwatch-commit').attr('action', `${issueUrl}/times/stopwatch/toggle`); |
| 71 | + $('.stopwatch-cancel').attr('action', `${issueUrl}/times/stopwatch/cancel`); |
| 72 | + $('.stopwatch-issue').text(`${repo_owner_name}/${repo_name}#${issue_index}`); |
| 73 | + $('.stopwatch-time').text(prettyMilliseconds(seconds * 1000)); |
| 74 | + updateStopwatchTime(seconds); |
| 75 | + btnEl.removeClass('hidden'); |
| 76 | + } |
| 77 | + |
| 78 | + return !!data.length; |
| 79 | +} |
| 80 | + |
| 81 | +async function updateStopwatchTime(seconds) { |
| 82 | + const secs = parseInt(seconds); |
| 83 | + if (!Number.isFinite(secs)) return; |
| 84 | + |
| 85 | + const start = Date.now(); |
| 86 | + updateTimeInterval = setInterval(() => { |
| 87 | + const delta = Date.now() - start; |
| 88 | + const dur = prettyMilliseconds(secs * 1000 + delta, {compact: true}); |
| 89 | + $('.stopwatch-time').text(dur); |
| 90 | + }, 1000); |
| 91 | +} |
0 commit comments