All files / src/composables useNotificationHandler.ts

100% Statements 28/28
100% Branches 16/16
100% Functions 7/7
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                          60x 60x     5x       5x   1x   1x   1x     2x         5x   1x   1x   1x     2x                 10x 10x 7x       3x 1x       2x               2x       10x 10x 2x             5x 5x 5x 5x     60x    
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 };
}