[IMP] web_notify: action button name and close

- We can now set a button name to the notification action.
- We can set an icon button as well.
- After the button is clicked, the notification gets closed.
This commit is contained in:
David
2024-04-01 17:22:25 +02:00
committed by trisdoan
parent 09d844578a
commit 8a0c6237e7
8 changed files with 57 additions and 38 deletions

View File

@@ -10,33 +10,43 @@ export const webNotificationService = {
let webNotifTimeouts = {};
/**
* Displays the web notification on user's screen
* @param {*} notifications
*/
function displaywebNotification(notifications) {
Object.values(webNotifTimeouts).forEach((notif) =>
browser.clearTimeout(notif)
);
webNotifTimeouts = {};
notifications.forEach(function (notif) {
browser.setTimeout(function () {
let buttons = [];
notifications.forEach((notif) => {
browser.setTimeout(() => {
var buttons = [];
if (notif.action) {
const params =
(notif.action.context && notif.action.context.params) || {};
buttons = [
{
name: env._t("Open"),
name: params.button_name || env._t("Open"),
primary: true,
onClick: async () => {
await action.doAction(notif.action);
},
...(params.button_icon && {icon: params.button_icon}),
},
];
}
notification.add(Markup(notif.message), {
const notificationRemove = notification.add(Markup(notif.message), {
title: notif.title,
type: notif.type,
sticky: notif.sticky,
className: notif.className,
buttons: buttons,
buttons: buttons.map((button) => {
const onClick = button.onClick;
button.onClick = async () => {
await onClick();
notificationRemove();
};
return button;
}),
});
});
});