Sortable per-county downloaded files table that refreshes when jobs finish, and web push notifications for job completion
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// Service worker: shows a notification when the server pushes a finished job,
|
||||
// and focuses (or opens) the dashboard when the notification is clicked.
|
||||
|
||||
const STATUS_TEXT = {
|
||||
success: 'succeeded', failed: 'failed', cancelled: 'was cancelled', interrupted: 'was interrupted'
|
||||
};
|
||||
const STATUS_ICON = { success: '✓', failed: '✗', cancelled: '⊘', interrupted: '⚠' };
|
||||
|
||||
self.addEventListener('push', event => {
|
||||
let job = {};
|
||||
try { job = (event.data && event.data.json()).job || {}; } catch (e) { /* show a generic notice */ }
|
||||
const status = STATUS_TEXT[job.status] || 'finished';
|
||||
const failed = job.status === 'failed' && job.exit_code !== null && job.exit_code !== undefined;
|
||||
const where = job.county ? ` (${job.county_name || job.county})` : '';
|
||||
const title = `${STATUS_ICON[job.status] || ''} ${job.label || job.script || 'Job'}${where} ${status}`.trim();
|
||||
const body = (failed ? `Exit code ${job.exit_code}. ` : '') +
|
||||
(job.user ? `Started by ${job.user}. ` : '') + 'Tap to view the output.';
|
||||
event.waitUntil(self.registration.showNotification(title, {
|
||||
body,
|
||||
tag: job.id || 'job',
|
||||
data: { url: '/', jobId: job.id },
|
||||
}));
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', event => {
|
||||
event.notification.close();
|
||||
const url = new URL((event.notification.data && event.notification.data.url) || '/', self.location.origin).href;
|
||||
event.waitUntil(clients.matchAll({ type: 'window', includeUncontrolled: true }).then(wins => {
|
||||
const win = wins.find(w => new URL(w.url).pathname === '/');
|
||||
return win ? win.focus() : clients.openWindow(url);
|
||||
}));
|
||||
});
|
||||
Reference in New Issue
Block a user