Repository navigation
Expand file tree
/
Copy pathabout.ts
More file actions
122 lines (110 loc) · 4.7 KB
/
Copy pathabout.ts
File metadata and controls
122 lines (110 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { serviceWorkerManager } from './src/ServiceWorkerManager.js';
import type {
UpdateAvailableDetail,
UpdateRestartRequiredDetail,
} from './src/ServiceWorkerManager.js';
import '/components/UpdateNotification.js';
import '/components/ConfirmationModal.js';
interface ConfirmationModal extends HTMLElement {
showModal(
message: string,
buttons: { btnClearCache: HTMLButtonElement; clearCacheStatus: HTMLSpanElement }
): void;
}
interface UpdateNotification extends HTMLElement {
show(update: UpdateAvailableDetail): void;
showRestartRequired(release: UpdateRestartRequiredDetail['release']): void;
showUnavailable(message: string): void;
showUpdating(status?: string): void;
showFailure(message: string): void;
}
const whenLoaded = Promise.all([
customElements.whenDefined('update-notification'),
customElements.whenDefined('confirmation-modal'),
]);
whenLoaded.then(async () => {
const updateNotification = document.querySelector('update-notification') as UpdateNotification;
const confirmationModal = document.querySelector('confirmation-modal') as ConfirmationModal;
window.addEventListener('sw-update-available', event => {
console.log('Service worker update available, showing notification');
updateNotification?.show((event as CustomEvent<UpdateAvailableDetail>).detail);
});
window.addEventListener('sw-update-restart-required', event => {
const detail = (event as CustomEvent<UpdateRestartRequiredDetail>).detail;
updateNotification?.showRestartRequired(detail.release);
});
window.addEventListener('sw-update-error', event => {
const detail = (event as CustomEvent<{ message: string }>).detail;
updateNotification?.showUnavailable(detail.message);
});
window.addEventListener('sw-update-progress', event => {
const detail = (event as CustomEvent<{ status: string }>).detail;
updateNotification?.showUpdating(detail.status);
});
updateNotification?.addEventListener('update-accepted', event => {
const pendingWorker = (event as CustomEvent<{ pendingWorker: ServiceWorker }>).detail
.pendingWorker;
serviceWorkerManager.handleUpdateNow(pendingWorker).catch(error => {
console.error('[about] Update failed:', error);
updateNotification.showFailure('Update failed. The current version remains active.');
});
});
updateNotification?.addEventListener('update-restart-requested', () => {
window.location.reload();
});
await serviceWorkerManager.register();
const version = document.getElementById('version') as HTMLSpanElement;
const latestVersion = document.getElementById('latestVersion');
const latestVersionContainer = document.getElementById('latestVersionContainer');
const noUpdateContainer = document.getElementById('noUpdateContainer');
const btnUpdate = document.getElementById('btnUpdate');
const currentVersion = await serviceWorkerManager.getVersion();
if (currentVersion) {
version.innerText = currentVersion;
} else {
version.innerText = 'Not available';
}
const latestVersionInfo = await serviceWorkerManager.getLatestVersion();
if (latestVersion && latestVersionContainer && noUpdateContainer) {
if (latestVersionInfo && latestVersionInfo !== currentVersion) {
latestVersion.innerText = latestVersionInfo;
latestVersionContainer.style.display = 'flex';
noUpdateContainer.style.display = 'none';
} else {
latestVersionContainer.style.display = 'none';
if (currentVersion) {
noUpdateContainer.style.display = 'block';
} else {
noUpdateContainer.style.display = 'none';
}
}
}
btnUpdate?.addEventListener('click', () => {
serviceWorkerManager.checkForUpdates();
});
const btnClearCache = document.getElementById('btnClearCache') as HTMLButtonElement;
const clearCacheStatus = document.getElementById('clearCacheStatus') as HTMLSpanElement;
btnClearCache?.addEventListener('click', async () => {
confirmationModal?.showModal('This will clear all cached data and reload the page. Continue?', {
btnClearCache: btnClearCache,
clearCacheStatus: clearCacheStatus,
});
confirmationModal?.addEventListener('confirm', async () => {
if (clearCacheStatus) {
btnClearCache.disabled = true;
clearCacheStatus.innerText = 'Clearing caches...';
try {
await serviceWorkerManager.clearAllCaches();
} catch (error) {
console.error('Failed to clear caches:', error);
clearCacheStatus.innerText = 'Failed to clear caches';
btnClearCache.disabled = false;
}
}
});
confirmationModal?.addEventListener('cancel', () => {
btnClearCache.disabled = false;
clearCacheStatus && (clearCacheStatus.innerText = '');
});
});
});