Notification action (#12742)

This commit is contained in:
Nicolas Mowen 2024-08-05 06:56:57 -06:00
parent 3abf89596a
commit 04fd05bc7d
2 changed files with 36 additions and 8 deletions

View File

@ -165,6 +165,7 @@ class WebPushClient(Communicator): # type: ignore[misc]
"direct_url": direct_url, "direct_url": direct_url,
"image": image, "image": image,
"id": reviewId, "id": reviewId,
"type": "alert",
} }
), ),
) )

View File

@ -5,6 +5,20 @@ self.addEventListener("push", function (event) {
if (event.data) { if (event.data) {
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
const data = event.data.json(); const data = event.data.json();
let actions = [];
switch (data.type ?? "unknown") {
case "alert":
actions = [
{
action: "markReviewed",
title: "Mark as Reviewed",
},
];
break;
}
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
self.registration.showNotification(data.title, { self.registration.showNotification(data.title, {
body: data.message, body: data.message,
@ -13,6 +27,7 @@ self.addEventListener("push", function (event) {
badge: "/images/maskable-badge.png", badge: "/images/maskable-badge.png",
tag: data.id, tag: data.id,
data: { id: data.id, link: data.direct_url }, data: { id: data.id, link: data.direct_url },
actions,
}); });
} else { } else {
// pass // pass
@ -26,14 +41,26 @@ self.addEventListener("notificationclick", (event) => {
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
event.notification.close(); event.notification.close();
// @ts-expect-error we know this exists switch (event.action ?? "default") {
if (event.notification.data) { case "markReviewed":
const url = event.notification.data.link; if (event.notification.data) {
// eslint-disable-next-line no-undef fetch("/api/reviews/viewed", {
if (clients.openWindow) { method: "POST",
// eslint-disable-next-line no-undef headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": 1 },
return clients.openWindow(url); body: JSON.stringify({ ids: [event.notification.data.id] }),
} });
}
break;
default:
// @ts-expect-error we know this exists
if (event.notification.data) {
const url = event.notification.data.link;
// eslint-disable-next-line no-undef
if (clients.openWindow) {
// eslint-disable-next-line no-undef
return clients.openWindow(url);
}
}
} }
} }
}); });