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';
|
||||
|
||||
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<NotificationPermission>(() =>
|
||||
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<typeof setTimeout> | undefined;
|
||||
let retryDelay = RETRY_MIN_MS;
|
||||
|
||||
const onEvent = (message: MessageEvent<string>) => {
|
||||
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]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user