import { useNotification } from '@kyvg/vue3-notification'; import { useI18n } from 'vue-i18n'; import { InteractivityActions, NavigationCubeActions, NotificationPayload, NotificationsActions, } from '@3cr/types-ts'; import { t as t3 } from '@3cr/translations-ts'; type NotificationType = 'success' | 'info' | 'warn' | 'error'; export function useNotificationHandler() { const { t } = useI18n(); const { notify } = useNotification(); function getNotificationText(notification: NotificationPayload): string { return t3(notification.Code, {}); } function getNotificationTitle(action: NotificationsActions): string { switch (action) { case NotificationsActions.no01: return t('enums.notificationType.success'); case NotificationsActions.no02: return t('enums.notificationType.error'); case NotificationsActions.no03: return t('enums.notificationType.warning'); default: case NotificationsActions.no04: return t('enums.notificationType.info'); } } function getNotificationType(action: NotificationsActions): NotificationType { switch (action) { case NotificationsActions.no01: return 'success'; case NotificationsActions.no02: return 'error'; case NotificationsActions.no03: return 'warn'; default: case NotificationsActions.no04: return 'info'; } } function shouldDisplayNotification( action: NotificationsActions, notification: NotificationPayload, ): boolean { // Hide type specific messages const hiddenTypes = [NotificationsActions.no01, NotificationsActions.no04]; if (hiddenTypes.includes(action)) { return false; } // Hide all slider actions if (notification.Action.startsWith('sl')) { return false; } // Hide action specific messages const hiddenActions = [ InteractivityActions.in01, InteractivityActions.in02, InteractivityActions.in03, InteractivityActions.in04, NavigationCubeActions.nc01, ] as string[]; return !hiddenActions.includes(notification.Action); } async function handler(action: NotificationsActions, message: string) { const notification = JSON.parse(message) as NotificationPayload; if (shouldDisplayNotification(action, notification)) { await announce(action, notification); } } async function announce( action: NotificationsActions, notification: NotificationPayload, ) { const title = getNotificationTitle(action); const text = getNotificationText(notification); const type = getNotificationType(action); notify({ title, text, type }); } return { handler, announce }; }