-
-
Notifications
You must be signed in to change notification settings - Fork 30
refactor: streamline notification closing logic in useBrowserNotifica… #1670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
| } | ||
| } catch (ex) { | ||
| logger.error('[NOTIFICATION][CLOSE][ERROR]', ex); | ||
|
paustint marked this conversation as resolved.
|
||
| } finally { | ||
| notification.current = null; | ||
|
Comment on lines
+19
to
+25
|
||
| } | ||
|
Comment on lines
+17
to
26
|
||
| }, []); | ||
|
|
||
| const visibilitychange = useCallback( | ||
| (_event: Event) => { | ||
| if (!document.hidden) { | ||
| closeNotification(); | ||
| } | ||
| }, | ||
|
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], | ||
|
paustint marked this conversation as resolved.
|
||
| ); | ||
|
|
||
| /** | ||
| * Send notification to user if permission exists and the tab is not visible | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.