UNPKG

@finos/legend-application

Version:
117 lines 4.91 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ApplicationError, assertErrorThrown, assertTrue, isString, } from '@finos/legend-shared'; import { action, makeObservable, observable } from 'mobx'; export const DEFAULT_NOTIFICATION_HIDE_TIME = 6000; // ms export const DEFAULT_ERROR_NOTIFICATION_HIDE_TIME = 10000; // ms export var NOTIFCATION_SEVERITY; (function (NOTIFCATION_SEVERITY) { NOTIFCATION_SEVERITY["ILEGAL_STATE"] = "ILEGAL_STATE"; NOTIFCATION_SEVERITY["ERROR"] = "ERROR"; NOTIFCATION_SEVERITY["WARNING"] = "WARNING"; NOTIFCATION_SEVERITY["SUCCESS"] = "SUCCESS"; NOTIFCATION_SEVERITY["INFO"] = "INFO"; })(NOTIFCATION_SEVERITY || (NOTIFCATION_SEVERITY = {})); export class Notification { severity; message; actions; autoHideDuration; constructor(severity, message, actions, autoHideDuration) { this.severity = severity; this.message = message; this.actions = actions; this.autoHideDuration = autoHideDuration; } } export class NotificationService { notification; constructor() { makeObservable(this, { notification: observable, setNotification: action, notify: action, notifySuccess: action, notifyWarning: action, notifyIllegalState: action, notifyError: action, }); } setNotification(notification) { this.notification = notification; } notify(message, actions, autoHideDuration) { this.setNotification(new Notification(NOTIFCATION_SEVERITY.INFO, message, actions ?? [], autoHideDuration === null ? undefined : (autoHideDuration ?? DEFAULT_NOTIFICATION_HIDE_TIME))); } notifySuccess(message, actions, autoHideDuration) { this.setNotification(new Notification(NOTIFCATION_SEVERITY.SUCCESS, message, actions ?? [], autoHideDuration === null ? undefined : (autoHideDuration ?? DEFAULT_NOTIFICATION_HIDE_TIME))); } notifyWarning(content, actions, autoHideDuration) { this.setNotification(new Notification(NOTIFCATION_SEVERITY.WARNING, content instanceof Error ? content.message : content, actions ?? [], autoHideDuration === null ? undefined : (autoHideDuration ?? DEFAULT_NOTIFICATION_HIDE_TIME))); } notifyError(content, actions) { const message = this.getErrorMessage(content); if (message) { this.setNotification(new Notification(NOTIFCATION_SEVERITY.ERROR, message, actions ?? [], undefined)); } } getErrorMessage(content) { let message; if (content instanceof ApplicationError) { message = content.detail; } else if (content instanceof Error) { message = content.message; } else { assertTrue(isString(content), `Can't display error`); message = content; } return message; } notifyIllegalState(message, actions, autoHideDuration) { this.setNotification(new Notification(NOTIFCATION_SEVERITY.ILEGAL_STATE, isString(message) ? `[PLEASE NOTIFY DEVELOPER] ${message}` : message, actions ?? [], autoHideDuration === null ? undefined : (autoHideDuration ?? DEFAULT_NOTIFICATION_HIDE_TIME))); } notifyUnsupportedFeature(featureName) { this.notifyWarning(`Unsupported feature: ${featureName}`); } /** * This function creates a more user-friendly way to throw error in the UI. Rather than crashing the whole app, we will * just notify and replacing the value should get with an alternative (e.g. `undefined`). A good use-case for this * is where we would not expect an error to throw (i.e. `IllegalStateError`), but we want to be sure that if the error * ever occurs, it still shows very apparently in the UI, as such, printing out in the console is not good enough, * but crashing the app is bad too, so this is a good balance. */ notifyAndReturnAlternativeOnError = (fn, alternative) => { try { return fn(); } catch (error) { assertErrorThrown(error); this.notifyIllegalState(error.message); return alternative; } }; } //# sourceMappingURL=NotificationService.js.map