Files
redash/client/app/services/notifications.js
Arik Fraimovich 56d3be2248 Prettier all the Javascript code & GitHub Action (#4433)
* Prettier all the JS files

* Add GitHub Action to autoformat code pushed to master

* Fix eslint violation due to formatting.

* Remove GitHub actions for styling

* Add restyled.io config
2019-12-11 17:05:38 +02:00

54 lines
1.4 KiB
JavaScript

import { find } from "lodash";
import debug from "debug";
import recordEvent from "@/services/recordEvent";
const logger = debug("redash:notifications");
const Notification = window.Notification || null;
if (!Notification) {
logger("HTML5 notifications are not supported.");
}
const hidden = find(["hidden", "webkitHidden", "mozHidden", "msHidden"], prop => prop in document);
class NotificationsService {
// eslint-disable-next-line class-methods-use-this
get pageVisible() {
return !document[hidden];
}
// eslint-disable-next-line class-methods-use-this
getPermissions() {
if (Notification && Notification.permission === "default") {
Notification.requestPermission(status => {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
}
showNotification(title, content) {
if (!Notification || this.pageVisible || Notification.permission !== "granted") {
return;
}
// using the 'tag' to avoid showing duplicate notifications
const notification = new Notification(title, {
tag: title + content,
body: content,
icon: "/images/redash_icon_small.png",
});
setTimeout(() => {
notification.close();
}, 3000);
notification.onclick = function onClick() {
window.focus();
this.close();
recordEvent("click", "notification");
};
}
}
export default new NotificationsService();