CommonNotifications.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!-- Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. /* eslint-disable vue/no-v-html */
  4. import useNotifications from '@shared/components/CommonNotifications/composable'
  5. import type { Notification } from '@shared/components/CommonNotifications/types'
  6. import { markup } from '@shared/utils/markup'
  7. const notificationTypeClassMap = {
  8. warn: 'bg-yellow text-white',
  9. success: 'bg-green text-white',
  10. error: 'bg-red/60 text-white',
  11. info: 'bg-white text-black',
  12. }
  13. const iconNameMap = {
  14. warn: 'mobile-info',
  15. success: 'mobile-check',
  16. error: 'mobile-warning',
  17. info: 'mobile-info',
  18. }
  19. const { notifications, removeNotification } = useNotifications()
  20. const getClassName = (notification: Notification) => {
  21. return notificationTypeClassMap[notification.type]
  22. }
  23. const clickHandler = (notification: Notification) => {
  24. const { callback } = notification
  25. removeNotification(notification.id)
  26. if (callback) callback()
  27. }
  28. </script>
  29. <template>
  30. <div id="Notifications" class="flex w-full justify-center">
  31. <div class="fixed top-0 z-50 ltr:right-0 rtl:left-0" role="alert">
  32. <TransitionGroup
  33. tag="div"
  34. enter-class="opacity-0"
  35. leave-active-class="transition-opacity duration-1000 opacity-0"
  36. >
  37. <div
  38. v-for="notification in notifications"
  39. :key="notification.id"
  40. data-test-id="notification"
  41. >
  42. <div class="flex justify-center">
  43. <div
  44. class="m-3 flex cursor-pointer items-center rounded py-2 px-4"
  45. :class="getClassName(notification)"
  46. @click="clickHandler(notification)"
  47. >
  48. <CommonIcon :name="iconNameMap[notification.type]" size="small" />
  49. <span
  50. class="text-sm ltr:ml-2 rtl:mr-2"
  51. v-html="
  52. markup(
  53. $t(
  54. notification.message,
  55. ...(notification.messagePlaceholder || []),
  56. ),
  57. )
  58. "
  59. />
  60. </div>
  61. </div>
  62. </div>
  63. </TransitionGroup>
  64. </div>
  65. </div>
  66. </template>