From 5b5d65b63e451ce44762a7f2bdac7fdcaaf148d3 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 6 Aug 2026 19:39:59 +0200 Subject: [PATCH] Add local notification on rest timer completion --- frontend/src/store/useUI.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js index b4eb439..0330ee7 100644 --- a/frontend/src/store/useUI.js +++ b/frontend/src/store/useUI.js @@ -10,6 +10,35 @@ import { useStore } from './useStore.js' const pushRestTimer = sec => { if (useStore.getState().user) api('/api/push/rest-timer', { method: 'POST', body: JSON.stringify({ seconds: sec }) }).catch(() => {}) } const cancelPushRestTimer = () => { if (useStore.getState().user) api('/api/push/rest-timer/cancel', { method: 'POST', body: '{}' }).catch(() => {}) } +const notificationsSupported = () => typeof window !== 'undefined' && 'Notification' in window +let requestRestNotificationPermissionP = null + +const requestRestNotificationPermission = async () => { + if (!notificationsSupported()) return false + if (Notification.permission === 'granted') return true + if (Notification.permission === 'denied') return false + if (!requestRestNotificationPermissionP) { + requestRestNotificationPermissionP = Notification.requestPermission() + .then(perm => perm === 'granted') + .catch(() => false) + .finally(() => { + requestRestNotificationPermissionP = null + }) + } + return requestRestNotificationPermissionP +} + +const maybeRestNotification = async () => { + if (!notificationsSupported()) return + if (!document.hidden && document.visibilityState !== 'hidden') return + if (Notification.permission !== 'granted' && !(await requestRestNotificationPermission())) return + try { + new Notification(t('Rest over'), { body: t('Rest over — next set!') }) + } catch { + // Intentionally ignore: notification APIs vary by browser and policy in edge cases. + } +} + let toastTm = null let timerInt = null let timerTick = null @@ -42,6 +71,7 @@ export const useUI = create((set, get) => ({ get().stopRest() const endsAt = Date.now() + sec * 1000 set({ timer: { left: sec, total: sec, endsAt } }) + requestRestNotificationPermission() pushRestTimer(sec) timerTick = () => { const tm = get().timer @@ -51,7 +81,7 @@ export const useUI = create((set, get) => ({ const snd = useStore.getState().S.sound if (left <= 0) { beep(snd, 880, 0.15); beep(snd, 880, 0.15, 0.25); beep(snd, 1320, 0.4, 0.5) - vibrate([200, 100, 200]); get().toast(t('Rest over — next set!')); get().stopRest(); return + vibrate([200, 100, 200]); maybeRestNotification(); get().toast(t('Rest over — next set!')); get().stopRest(); return } if (left <= 3) beep(snd, 660, 0.1) set({ timer: { ...tm, left } })