Files
kycnotme/web/src/components/NotificationEventsScript.astro
2025-06-14 18:56:58 +00:00

33 lines
946 B
Plaintext

---
---
<script>
import { isBrowserNotificationsEnabled, showBrowserNotification } from '../lib/client/browserNotifications'
import {
makeBrowserNotificationOptions,
makeBrowserNotificationTitle,
} from '../lib/client/notificationOptions'
document.addEventListener('sse:new-notification', (event) => {
if (isBrowserNotificationsEnabled()) {
const payload = event.detail
const notification = showBrowserNotification(
makeBrowserNotificationTitle(payload.title),
makeBrowserNotificationOptions(payload, { removeActions: true })
)
// Handle notification click
if (notification) {
notification.onclick = () => {
const defaultAction = payload.actions.find((a) => a.url) ?? payload.actions[0]
if (defaultAction?.url) {
window.open(defaultAction.url, '_blank')
}
notification.close()
}
}
}
})
</script>