forked from beznazwiska/claude-peak-hours
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
121 lines (103 loc) · 4.04 KB
/
Copy pathsw.js
File metadata and controls
121 lines (103 loc) · 4.04 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
/**
* Service Worker for Claude Peak Hours PWA + Windows Widget.
*/
// Import shared peak hours logic
importScripts('peak-hours.js');
const CACHE_NAME = 'claude-peak-hours-v1';
const ASSETS = [
'./',
'./index.html',
'./peak-hours.js',
'./manifest.json',
'./icons/icon-192.png',
'./icons/icon-512.png',
];
// ---------------------------------------------------------------------------
// Standard PWA lifecycle
// ---------------------------------------------------------------------------
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((cached) => cached || fetch(event.request))
);
});
// ---------------------------------------------------------------------------
// Widget data helper
// ---------------------------------------------------------------------------
function getWidgetData() {
const state = getPeakState();
const isPolish = true; // Default to Polish for the widget
const statusIcon = state.status === 'off-peak'
? 'https://vallhalen.github.io/claude-peak-hours-windows/icons/dot-green.png'
: state.status === 'peak'
? 'https://vallhalen.github.io/claude-peak-hours-windows/icons/dot-red.png'
: 'https://vallhalen.github.io/claude-peak-hours-windows/icons/dot-yellow.png';
return {
statusIcon,
statusTitle: isPolish
? (state.isPeak ? 'ZWIĘKSZONE ZUŻYCIE' : 'PEŁNA MOC')
: (state.isPeak ? 'HIGHER USAGE' : 'FULL POWER'),
statusColor: state.status === 'off-peak' ? 'Good' : state.status === 'peak' ? 'Attention' : 'Warning',
statusDescription: isPolish
? (state.isPeak ? 'Limity zużywają się szybciej' : 'Claude działa na full — korzystaj!')
: (state.isPeak ? 'Limits consumed faster' : 'Claude at full capacity — go ahead!'),
nextChangeLabel: isPolish
? (state.isPeak ? 'Pełna moc za' : 'Ograniczenia za')
: (state.isPeak ? 'Full power in' : 'Higher usage in'),
countdown: state.countdownText,
peakHoursLocal: state.peakHoursLocal,
workdaysLabel: isPolish ? 'Dni robocze' : 'Workdays',
workdaysValue: isPolish ? 'Pon–Pt' : 'Mon–Fri',
};
}
// ---------------------------------------------------------------------------
// Windows Widget events (PWA Widget API)
// ---------------------------------------------------------------------------
self.addEventListener('widgetinstall', (event) => {
event.waitUntil(updateWidget(event.widget));
});
self.addEventListener('widgetresume', (event) => {
event.waitUntil(updateWidget(event.widget));
});
self.addEventListener('widgetclick', (event) => {
// Open the PWA when widget is clicked
event.waitUntil(
clients.openWindow('/').then(() => updateWidget(event.widget))
);
});
self.addEventListener('widgetuninstall', (event) => {
// Nothing to clean up
});
async function updateWidget(widget) {
try {
const templateResponse = await fetch('./widget/template.json');
const template = await templateResponse.text();
const data = JSON.stringify(getWidgetData());
await self.widgets.updateByTag('peak-status', { template, data });
} catch (e) {
console.error('Widget update failed:', e);
}
}
// Periodic widget update (called by the system based on manifest "update" interval)
self.addEventListener('periodicsync', (event) => {
if (event.tag === 'widget-update') {
event.waitUntil(
self.widgets.getByTag('peak-status').then((widget) => {
if (widget) return updateWidget(widget);
})
);
}
});