diff --git a/client/src/hooks/useRunNotifications.ts b/client/src/hooks/useRunNotifications.ts index 18fa612..5ed7ea4 100644 --- a/client/src/hooks/useRunNotifications.ts +++ b/client/src/hooks/useRunNotifications.ts @@ -7,16 +7,20 @@ import type { RunEvent } from '../api/types'; import type { NotificationToggleState } from '../ui'; const STORAGE_KEY = 'liqa-run-notifications'; +const RETRY_MIN_MS = 1_000; +const RETRY_MAX_MS = 30_000; function supported(): boolean { return typeof window !== 'undefined' && 'Notification' in window && 'EventSource' in window; } -function readEnabled(): boolean { +/** The stored choice: `null` until the user toggles notifications explicitly. */ +function readEnabled(): boolean | null { try { - return localStorage.getItem(STORAGE_KEY) === '1'; + const value = localStorage.getItem(STORAGE_KEY); + return value === null ? null : value === '1'; } catch { - return false; + return null; } } @@ -41,10 +45,35 @@ export function useRunNotifications(): { const { t } = useTranslation(); const navigate = useNavigate(); const queryClient = useQueryClient(); - const [enabled, setEnabled] = useState(readEnabled); + const [stored, setStored] = useState(readEnabled); const [permission, setPermission] = useState(() => isSupported ? Notification.permission : 'denied', ); + // Permission granted outside the toggle (browser prompt or site settings) + // enables notifications unless the user turned them off explicitly + const enabled = stored ?? permission === 'granted'; + + // Follow permission changes made in the browser's site settings + useEffect(() => { + if (!isSupported) return; + const sync = () => setPermission(Notification.permission); + let status: PermissionStatus | undefined; + let cancelled = false; + navigator.permissions + ?.query({ name: 'notifications' }) + .then((s) => { + if (cancelled) return; + status = s; + status.addEventListener('change', sync); + }) + .catch(() => {}); + window.addEventListener('focus', sync); + return () => { + cancelled = true; + status?.removeEventListener('change', sync); + window.removeEventListener('focus', sync); + }; + }, [isSupported]); // Latest callbacks without reopening the stream when they change const handlers = useRef({ t, navigate }); @@ -56,7 +85,9 @@ export function useRunNotifications(): { useEffect(() => { if (!active) return; - const source = new EventSource(RUN_EVENTS_URL); + let source: EventSource; + let retryTimer: ReturnType | undefined; + let retryDelay = RETRY_MIN_MS; const onEvent = (message: MessageEvent) => { let event: RunEvent; @@ -90,9 +121,28 @@ export function useRunNotifications(): { }; }; - source.addEventListener('run.started', onEvent as EventListener); - source.addEventListener('run.finished', onEvent as EventListener); - return () => source.close(); + const connect = () => { + source = new EventSource(RUN_EVENTS_URL); + source.addEventListener('open', () => { + retryDelay = RETRY_MIN_MS; + }); + // The browser retries transient errors itself, but some failures close + // the stream for good (e.g. Firefox drops quiet connections after a + // network change), so reopen it with backoff + source.addEventListener('error', () => { + if (source.readyState !== EventSource.CLOSED) return; + retryTimer = setTimeout(connect, retryDelay); + retryDelay = Math.min(retryDelay * 2, RETRY_MAX_MS); + }); + source.addEventListener('run.started', onEvent as EventListener); + source.addEventListener('run.finished', onEvent as EventListener); + }; + connect(); + + return () => { + clearTimeout(retryTimer); + source.close(); + }; }, [active, queryClient]); const toggle = useCallback(() => { @@ -101,7 +151,7 @@ export function useRunNotifications(): { void Notification.requestPermission().then((result) => { setPermission(result); if (result === 'granted') { - setEnabled(true); + setStored(true); writeEnabled(true); } }); @@ -112,7 +162,7 @@ export function useRunNotifications(): { return; } const next = !enabled; - setEnabled(next); + setStored(next); writeEnabled(next); }, [isSupported, enabled]); diff --git a/client/src/ui/NotificationToggle/NotificationToggle.module.css b/client/src/ui/NotificationToggle/NotificationToggle.module.css index 9e60379..3b0c4cd 100644 --- a/client/src/ui/NotificationToggle/NotificationToggle.module.css +++ b/client/src/ui/NotificationToggle/NotificationToggle.module.css @@ -37,5 +37,12 @@ } .btn[aria-pressed='true'] { - color: var(--color-text); + background: var(--color-primary); + border-color: var(--color-primary); + color: var(--color-primary-fg); +} + +.btn[aria-pressed='true']:hover { + background: var(--color-primary-hover); + border-color: var(--color-primary-hover); } diff --git a/docs/scenario.md b/docs/scenario.md index 2a0e182..de9895a 100644 --- a/docs/scenario.md +++ b/docs/scenario.md @@ -140,7 +140,7 @@ The server publishes run lifecycle events on a server-sent events (SSE) stream, - `run.finished` is sent exactly once per run, with the final `status` (`pass` or `fail`), including runs that fail during setup, time out, or are failed on server restart. - A `ping` event is sent every 25 s to keep idle connections open through proxies. -The UI uses this stream for opt-in browser notifications: the bell button in the side panel footer asks for the notification permission and then shows "Run started / passed / failed: " notifications while the UI is open in a tab; clicking one opens the run. The choice is stored in the browser's `localStorage`. Browsers only offer notifications in a secure context (HTTPS or `localhost`), so over plain HTTP on another host the bell has no effect. When the permission is blocked, the button is disabled; re-allow it in the browser's site settings. Finished runs also refresh the runs lists immediately. +The UI uses this stream for opt-in browser notifications: the bell button in the side panel footer asks for the notification permission (granting it from the browser's own prompt or site settings works too) and then shows "Run started / passed / failed: " notifications while the UI is open in a tab; clicking one opens the run. The bell is highlighted while notifications are on; turning them off is stored in the browser's `localStorage`. Browsers only offer notifications in a secure context (HTTPS or `localhost`), so over plain HTTP on another host the bell has no effect. When the permission is blocked, the button is disabled; re-allow it in the browser's site settings. Finished runs also refresh the runs lists immediately. ## Statuses