Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions libs/shared/ui-utils/src/lib/hooks/useBrowserNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,36 @@ const ICON_URL = '/assets/images/jetstream-icon-white-bg.png';
* @returns
*/
export function useBrowserNotifications(serverUrl: string, isFocused?: () => boolean) {
const notification = useRef<Notification>(null);
const notification = useRef<Notification | null>(null);

const visibilitychange = useCallback((event: Event) => {
if (notification.current && !document.hidden) {
notification.current.close();
const closeNotification = useCallback(() => {
try {
if (notification.current && typeof notification.current.close === 'function') {
notification.current.close();
}
Comment thread
paustint marked this conversation as resolved.
} catch (ex) {
logger.error('[NOTIFICATION][CLOSE][ERROR]', ex);
Comment thread
paustint marked this conversation as resolved.
} finally {
notification.current = null;
Comment on lines +19 to +25
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeNotification sets notification.current = null both inside the if block and again in finally. The inner assignment is redundant and makes the control flow harder to follow; consider keeping the single assignment in finally (or only after a successful close) to reduce duplication.

Copilot uses AI. Check for mistakes.
}
Comment on lines +17 to 26
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeNotification assigns notification.current = null, but the ref is declared as useRef<Notification>(null) (and is written to elsewhere). With strictNullChecks enabled for this lib, the ref should be typed as nullable and mutable (e.g., useRef<Notification | null>(null)) so these assignments are type-safe.

Copilot uses AI. Check for mistakes.
}, []);

const visibilitychange = useCallback(
(_event: Event) => {
if (!document.hidden) {
closeNotification();
}
},
Comment thread
paustint marked this conversation as resolved.
[closeNotification],
);

// ensure that notifications are cleared if browser tab is closed
const handleUnload = useCallback((event: Event) => {
if (notification.current) {
notification.current.close();
}
}, []);
const handleUnload = useCallback(
(_event: Event) => {
closeNotification();
},
[closeNotification],
Comment thread
paustint marked this conversation as resolved.
);

/**
* Send notification to user if permission exists and the tab is not visible
Expand Down
Loading