Handle undefined notification payloads

This commit is contained in:
Eric Bailey
2025-05-22 10:41:27 -05:00
parent dad8e6165a
commit 8b42b93c98
+14 -1
View File
@@ -26,7 +26,13 @@ export type NotificationReason =
| 'chat-message' | 'chat-message'
| 'starterpack-joined' | 'starterpack-joined'
/**
* Manually overridden type, but retains the possibility of
* `notification.request.trigger.payload` being `undefined`, as specified in
* the source types.
*/
type NotificationPayload = type NotificationPayload =
| undefined
| { | {
reason: Exclude<NotificationReason, 'chat-message'> reason: Exclude<NotificationReason, 'chat-message'>
uri: string uri: string
@@ -47,7 +53,7 @@ const DEFAULT_HANDLER_OPTIONS = {
} satisfies Notifications.NotificationBehavior } satisfies Notifications.NotificationBehavior
// These need to stay outside the hook to persist between account switches // These need to stay outside the hook to persist between account switches
let storedPayload: NotificationPayload | undefined let storedPayload: NotificationPayload
let prevDate = 0 let prevDate = 0
const logger = Logger.create(Logger.Context.Notifications) const logger = Logger.create(Logger.Context.Notifications)
@@ -191,6 +197,11 @@ export function useNotificationsHandler() {
logger.debug('Notifications: received', {e}) logger.debug('Notifications: received', {e})
const payload = e.request.trigger.payload as NotificationPayload const payload = e.request.trigger.payload as NotificationPayload
if (!payload) {
return DEFAULT_HANDLER_OPTIONS
}
if ( if (
payload.reason === 'chat-message' && payload.reason === 'chat-message' &&
payload.recipientDid === currentAccount?.did payload.recipientDid === currentAccount?.did
@@ -231,6 +242,8 @@ export function useNotificationsHandler() {
const payload = e.notification.request.trigger const payload = e.notification.request.trigger
.payload as NotificationPayload .payload as NotificationPayload
if (!payload) return
logger.debug( logger.debug(
'User pressed a notification, opening notifications tab', 'User pressed a notification, opening notifications tab',
{}, {},