2021-06-26 22:02:00 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-06-26 14:32:40 +02:00
|
|
|
import logger from "../logger";
|
|
|
|
|
|
|
|
function getNotificationFunction(type: "info" | "warn" | "error") {
|
|
|
|
return (message: string, { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}): void => {
|
2021-02-10 19:05:20 +01:00
|
|
|
if (ui.notifications) {
|
2021-06-26 14:32:40 +02:00
|
|
|
ui.notifications[type](message, { permanent });
|
|
|
|
if (log) {
|
|
|
|
logger[type](message);
|
|
|
|
}
|
2021-02-10 19:05:20 +01:00
|
|
|
} else {
|
2021-06-26 14:32:40 +02:00
|
|
|
logger[type](message);
|
2021-02-10 19:05:20 +01:00
|
|
|
}
|
2021-06-26 14:32:40 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const notifications = {
|
|
|
|
info: getNotificationFunction("info"),
|
|
|
|
warn: getNotificationFunction("warn"),
|
|
|
|
error: getNotificationFunction("error"),
|
2021-02-10 19:05:20 +01:00
|
|
|
notify: (
|
|
|
|
message: string,
|
|
|
|
type: "info" | "warning" | "error" = "info",
|
2021-06-26 14:32:40 +02:00
|
|
|
{ permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {},
|
2021-02-10 19:05:20 +01:00
|
|
|
): void => {
|
|
|
|
if (ui.notifications) {
|
|
|
|
ui.notifications.notify(message, type, { permanent });
|
2021-06-26 14:32:40 +02:00
|
|
|
if (log) {
|
|
|
|
logger.getLoggingFunction(type)(message);
|
|
|
|
}
|
2021-02-10 19:05:20 +01:00
|
|
|
} else {
|
2021-06-26 14:32:40 +02:00
|
|
|
logger.getLoggingFunction(type)(message);
|
2021-02-10 19:05:20 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default notifications;
|