usePushMessages.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { onMounted } from 'vue'
  3. import {
  4. useNotifications,
  5. NotificationTypes,
  6. } from '#shared/components/CommonNotifications/index.ts'
  7. import { usePushMessagesSubscription } from '#shared/graphql/subscriptions/pushMessages.api.ts'
  8. import type {
  9. PushMessagesSubscription,
  10. PushMessagesSubscriptionVariables,
  11. } from '#shared/graphql/types.ts'
  12. import { SubscriptionHandler } from '#shared/server/apollo/handler/index.ts'
  13. import testFlags from '#shared/utils/testFlags.ts'
  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