Release 202506091000
This commit is contained in:
56
web/src/lib/notificationOptions.ts
Normal file
56
web/src/lib/notificationOptions.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { NotificationData, NotificationPayload } from './serverEventsTypes'
|
||||
|
||||
export type CustomNotificationOptions = NotificationOptions & {
|
||||
actions?: { action: string; title: string; icon?: string }[]
|
||||
timestamp: number
|
||||
data: NotificationData
|
||||
}
|
||||
|
||||
export function makeNotificationOptions(
|
||||
payload: NotificationPayload | null,
|
||||
options: { removeActions?: boolean } = {}
|
||||
) {
|
||||
const defaultOptions: CustomNotificationOptions = {
|
||||
body: 'You have a new notification',
|
||||
lang: 'en-US',
|
||||
icon: '/favicon.svg',
|
||||
badge: '/favicon.svg',
|
||||
requireInteraction: false,
|
||||
silent: false,
|
||||
actions: options.removeActions
|
||||
? undefined
|
||||
: [
|
||||
{
|
||||
action: 'view',
|
||||
title: 'View',
|
||||
icon: 'https://api.iconify.design/ri/arrow-right-line.svg',
|
||||
},
|
||||
],
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
defaultActionUrl: '/notifications',
|
||||
payload: null,
|
||||
},
|
||||
}
|
||||
|
||||
if (!payload) {
|
||||
return defaultOptions
|
||||
}
|
||||
|
||||
return {
|
||||
...defaultOptions,
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
body: payload.body || undefined,
|
||||
actions: options.removeActions
|
||||
? undefined
|
||||
: payload.actions.map((action) => ({
|
||||
action: action.action,
|
||||
title: action.title,
|
||||
icon: action.icon,
|
||||
})),
|
||||
data: {
|
||||
...defaultOptions.data,
|
||||
payload,
|
||||
},
|
||||
} as CustomNotificationOptions
|
||||
}
|
||||
Reference in New Issue
Block a user