2023-07-28 00:51:07 -07:00
|
|
|
import { ElNotification as Notification } from 'element-plus';
|
|
|
|
import type { NotificationInstance, NotificationOptions, MessageBoxState } from 'element-plus';
|
2023-11-28 03:15:08 -08:00
|
|
|
import { sanitizeHtml } from '@/utils/htmlUtils';
|
2023-04-18 03:41:55 -07:00
|
|
|
import { useTelemetry } from '@/composables/useTelemetry';
|
2023-05-15 09:41:13 -07:00
|
|
|
import { useWorkflowsStore } from '@/stores/workflows.store';
|
2023-04-18 03:41:55 -07:00
|
|
|
import { useI18n } from './useI18n';
|
|
|
|
import { useExternalHooks } from './useExternalHooks';
|
|
|
|
|
2023-07-28 00:51:07 -07:00
|
|
|
const messageDefaults: Partial<Omit<NotificationOptions, 'message'>> = {
|
2023-04-18 03:41:55 -07:00
|
|
|
dangerouslyUseHTMLString: true,
|
|
|
|
position: 'bottom-right',
|
|
|
|
};
|
|
|
|
|
2023-07-28 00:51:07 -07:00
|
|
|
const stickyNotificationQueue: NotificationInstance[] = [];
|
2023-04-18 03:41:55 -07:00
|
|
|
|
|
|
|
export function useToast() {
|
|
|
|
const telemetry = useTelemetry();
|
|
|
|
const workflowsStore = useWorkflowsStore();
|
|
|
|
const externalHooks = useExternalHooks();
|
2023-07-28 00:51:07 -07:00
|
|
|
const i18n = useI18n();
|
2023-04-18 03:41:55 -07:00
|
|
|
|
2023-10-19 05:02:59 -07:00
|
|
|
function showMessage(messageData: NotificationOptions, track = true) {
|
2023-04-18 03:41:55 -07:00
|
|
|
messageData = { ...messageDefaults, ...messageData };
|
2023-10-19 05:02:59 -07:00
|
|
|
messageData.message =
|
|
|
|
typeof messageData.message === 'string'
|
|
|
|
? sanitizeHtml(messageData.message)
|
|
|
|
: messageData.message;
|
2023-04-18 03:41:55 -07:00
|
|
|
|
2023-10-19 05:02:59 -07:00
|
|
|
const notification = Notification(messageData);
|
2023-04-18 03:41:55 -07:00
|
|
|
|
|
|
|
if (messageData.duration === 0) {
|
|
|
|
stickyNotificationQueue.push(notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messageData.type === 'error' && track) {
|
|
|
|
telemetry.track('Instance FE emitted error', {
|
|
|
|
error_title: messageData.title,
|
|
|
|
error_message: messageData.message,
|
2023-05-15 09:41:13 -07:00
|
|
|
caused_by_credential: causedByCredential(messageData.message),
|
2023-04-18 03:41:55 -07:00
|
|
|
workflow_id: workflowsStore.workflowId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showToast(config: {
|
|
|
|
title: string;
|
2023-10-19 05:02:59 -07:00
|
|
|
message: NotificationOptions['message'];
|
2023-04-18 03:41:55 -07:00
|
|
|
onClick?: () => void;
|
|
|
|
onClose?: () => void;
|
|
|
|
duration?: number;
|
|
|
|
customClass?: string;
|
|
|
|
closeOnClick?: boolean;
|
2023-07-28 00:51:07 -07:00
|
|
|
type?: MessageBoxState['type'];
|
2023-05-22 06:09:29 -07:00
|
|
|
dangerouslyUseHTMLString?: boolean;
|
2023-04-18 03:41:55 -07:00
|
|
|
}) {
|
|
|
|
// eslint-disable-next-line prefer-const
|
2023-07-28 00:51:07 -07:00
|
|
|
let notification: NotificationInstance;
|
2023-04-18 03:41:55 -07:00
|
|
|
if (config.closeOnClick) {
|
|
|
|
const cb = config.onClick;
|
|
|
|
config.onClick = () => {
|
|
|
|
if (notification) {
|
|
|
|
notification.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
notification = showMessage({
|
|
|
|
title: config.title,
|
|
|
|
message: config.message,
|
|
|
|
onClick: config.onClick,
|
|
|
|
onClose: config.onClose,
|
|
|
|
duration: config.duration,
|
|
|
|
customClass: config.customClass,
|
|
|
|
type: config.type,
|
2023-05-22 06:09:29 -07:00
|
|
|
dangerouslyUseHTMLString: config.dangerouslyUseHTMLString ?? true,
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
function collapsableDetails({ description, node }: Error) {
|
|
|
|
if (!description) return '';
|
|
|
|
|
|
|
|
const errorDescription =
|
|
|
|
description.length > 500 ? `${description.slice(0, 500)}...` : description;
|
|
|
|
|
|
|
|
return `
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<details>
|
|
|
|
<summary
|
|
|
|
style="color: #ff6d5a; font-weight: bold; cursor: pointer;"
|
|
|
|
>
|
|
|
|
${i18n.baseText('showMessage.showDetails')}
|
|
|
|
</summary>
|
|
|
|
<p>${node.name}: ${errorDescription}</p>
|
|
|
|
</details>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showError(e: Error | unknown, title: string, message?: string) {
|
|
|
|
const error = e as Error;
|
|
|
|
const messageLine = message ? `${message}<br/>` : '';
|
|
|
|
showMessage(
|
|
|
|
{
|
|
|
|
title,
|
|
|
|
message: `
|
|
|
|
${messageLine}
|
|
|
|
<i>${error.message}</i>
|
|
|
|
${collapsableDetails(error)}`,
|
|
|
|
type: 'error',
|
|
|
|
duration: 0,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2023-05-10 08:10:03 -07:00
|
|
|
void externalHooks.run('showMessage.showError', {
|
2023-04-18 03:41:55 -07:00
|
|
|
title,
|
|
|
|
message,
|
|
|
|
errorMessage: error.message,
|
|
|
|
});
|
|
|
|
|
|
|
|
telemetry.track('Instance FE emitted error', {
|
|
|
|
error_title: title,
|
|
|
|
error_description: message,
|
|
|
|
error_message: error.message,
|
2023-05-15 09:41:13 -07:00
|
|
|
caused_by_credential: causedByCredential(error.message),
|
2023-04-18 03:41:55 -07:00
|
|
|
workflow_id: workflowsStore.workflowId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:51:07 -07:00
|
|
|
function showAlert(config: NotificationOptions): NotificationInstance {
|
2023-05-15 09:41:13 -07:00
|
|
|
return Notification(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
function causedByCredential(message: string | undefined) {
|
|
|
|
if (!message) return false;
|
|
|
|
|
|
|
|
return message.includes('Credentials for') && message.includes('are not set');
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearAllStickyNotifications() {
|
|
|
|
stickyNotificationQueue.forEach((notification) => {
|
|
|
|
if (notification) {
|
|
|
|
notification.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stickyNotificationQueue.length = 0;
|
|
|
|
}
|
|
|
|
|
2023-04-18 03:41:55 -07:00
|
|
|
return {
|
|
|
|
showMessage,
|
|
|
|
showToast,
|
|
|
|
showError,
|
2023-05-15 09:41:13 -07:00
|
|
|
showAlert,
|
|
|
|
clearAllStickyNotifications,
|
2023-04-18 03:41:55 -07:00
|
|
|
};
|
|
|
|
}
|