usePushMessages.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { onMounted } from 'vue'
  3. import {
  4. useNotifications,
  5. NotificationTypes,
  6. } from '@shared/components/CommonNotifications'
  7. import { usePushMessagesSubscription } from '@shared/graphql/subscriptions/pushMessages.api'
  8. import type {
  9. PushMessagesSubscription,
  10. PushMessagesSubscriptionVariables,
  11. } from '@shared/graphql/types'
  12. import { SubscriptionHandler } from '@shared/server/apollo/handler'
  13. import testFlags from '@shared/utils/testFlags'
  14. let subscription: SubscriptionHandler<
  15. PushMessagesSubscription,
  16. PushMessagesSubscriptionVariables
  17. >
  18. const usePushMessages = () => {
  19. const notify = (message: string) => {
  20. useNotifications().notify({
  21. message,
  22. type: NotificationTypes.Warn,
  23. persistent: true,
  24. })
  25. }
  26. onMounted(() => {
  27. if (subscription) return
  28. subscription = new SubscriptionHandler(usePushMessagesSubscription())
  29. subscription.onResult((result) => {
  30. const message = result.data?.pushMessages
  31. if (!message?.title && !message?.text) {
  32. testFlags.set('usePushMessagesSubscription.subscribed')
  33. return
  34. }
  35. notify(`${message.title}: ${message.text}`)
  36. })
  37. })
  38. }
  39. export default usePushMessages