47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
'use strict';
|
|
|
|
import logger from './logger';
|
|
|
|
const notificationPrefix = 'Darkness Dependent Vision:';
|
|
|
|
/**
|
|
* @typedef {"info" | "warn" | "error"} NotificationType
|
|
*/
|
|
|
|
/**
|
|
* @typedef {(message: string, {permanent, log}?: {permanent?: boolean, log?: boolean}) => void} NotificationFunction
|
|
*/
|
|
|
|
/**
|
|
* @param {NotificationType} type The type of the notification function to get
|
|
* @returns {NotificationFunction} A function that can be called to send a notification to the user
|
|
*/
|
|
function getNotificationFunction(type) {
|
|
return (message, { permanent = false, log = false } = {}) => {
|
|
ui.notifications[type](`${notificationPrefix} ${message}`, { permanent });
|
|
if (log) {
|
|
logger[type](message);
|
|
}
|
|
};
|
|
}
|
|
|
|
const notifications = {
|
|
info: getNotificationFunction('info'),
|
|
warn: getNotificationFunction('warn'),
|
|
error: getNotificationFunction('error'),
|
|
notify: (message, type, { permanent = false, log = false } = {}) => {
|
|
ui.notifications.notify(`${notificationPrefix} ${message}`, type, { permanent });
|
|
if (log) {
|
|
logger.getLoggingFunction(type)(message);
|
|
}
|
|
},
|
|
};
|
|
|
|
Object.freeze(notifications);
|
|
|
|
export default notifications;
|