fix(client): keep run notifications connected in Firefox
- reopen the run events stream with backoff when the browser closes it for good (Firefox drops quiet connections after a network change) - enable notifications when the permission is granted outside the bell, unless turned off explicitly - follow permission changes made in the browser's site settings - highlight the bell while notifications are on
This commit is contained in:
3 files changed
+69
-12
No files matched your search
@@ -7,16 +7,20 @@ import type { RunEvent } from '../api/types';
|
|||||||
import type { NotificationToggleState } from '../ui';
|
import type { NotificationToggleState } from '../ui';
|
||||||
|
|
||||||
const STORAGE_KEY = 'liqa-run-notifications';
|
const STORAGE_KEY = 'liqa-run-notifications';
|
||||||
|
const RETRY_MIN_MS = 1_000;
|
||||||
|
const RETRY_MAX_MS = 30_000;
|
||||||
|
|
||||||
function supported(): boolean {
|
function supported(): boolean {
|
||||||
return typeof window !== 'undefined' && 'Notification' in window && 'EventSource' in window;
|
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 {
|
try {
|
||||||
return localStorage.getItem(STORAGE_KEY) === '1';
|
const value = localStorage.getItem(STORAGE_KEY);
|
||||||
|
return value === null ? null : value === '1';
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,10 +45,35 @@ export function useRunNotifications(): {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [enabled, setEnabled] = useState(readEnabled);
|
const [stored, setStored] = useState(readEnabled);
|
||||||
const [permission, setPermission] = useState<NotificationPermission>(() =>
|
const [permission, setPermission] = useState<NotificationPermission>(() =>
|
||||||
isSupported ? Notification.permission : 'denied',
|
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
|
// Latest callbacks without reopening the stream when they change
|
||||||
const handlers = useRef({ t, navigate });
|
const handlers = useRef({ t, navigate });
|
||||||
@@ -56,7 +85,9 @@ export function useRunNotifications(): {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!active) return;
|
if (!active) return;
|
||||||
const source = new EventSource(RUN_EVENTS_URL);
|
let source: EventSource;
|
||||||
|
let retryTimer: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
let retryDelay = RETRY_MIN_MS;
|
||||||
|
|
||||||
const onEvent = (message: MessageEvent<string>) => {
|
const onEvent = (message: MessageEvent<string>) => {
|
||||||
let event: RunEvent;
|
let event: RunEvent;
|
||||||
@@ -90,9 +121,28 @@ export function useRunNotifications(): {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
source.addEventListener('run.started', onEvent as EventListener);
|
const connect = () => {
|
||||||
source.addEventListener('run.finished', onEvent as EventListener);
|
source = new EventSource(RUN_EVENTS_URL);
|
||||||
return () => source.close();
|
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]);
|
}, [active, queryClient]);
|
||||||
|
|
||||||
const toggle = useCallback(() => {
|
const toggle = useCallback(() => {
|
||||||
@@ -101,7 +151,7 @@ export function useRunNotifications(): {
|
|||||||
void Notification.requestPermission().then((result) => {
|
void Notification.requestPermission().then((result) => {
|
||||||
setPermission(result);
|
setPermission(result);
|
||||||
if (result === 'granted') {
|
if (result === 'granted') {
|
||||||
setEnabled(true);
|
setStored(true);
|
||||||
writeEnabled(true);
|
writeEnabled(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -112,7 +162,7 @@ export function useRunNotifications(): {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const next = !enabled;
|
const next = !enabled;
|
||||||
setEnabled(next);
|
setStored(next);
|
||||||
writeEnabled(next);
|
writeEnabled(next);
|
||||||
}, [isSupported, enabled]);
|
}, [isSupported, enabled]);
|
||||||
|
|
||||||
|
|||||||
@@ -37,5 +37,12 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.btn[aria-pressed='true'] {
|
.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);
|
||||||
}
|
}
|
||||||
+1
-1
@@ -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.
|
- `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.
|
- 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: <scenario>" 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: <scenario>" 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
|
## Statuses
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user