useMacros.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, type Ref, ref } from 'vue'
  3. import type { MacroById } from '#shared/entities/macro/types.ts'
  4. import { useMacrosQuery } from '#shared/graphql/queries/macros.api.ts'
  5. import { useMacrosUpdateSubscription } from '#shared/graphql/subscriptions/macrosUpdate.api.ts'
  6. import { EnumTicketScreenBehavior } from '#shared/graphql/types.ts'
  7. import {
  8. QueryHandler,
  9. SubscriptionHandler,
  10. } from '#shared/server/apollo/handler/index.ts'
  11. export const macroScreenBehaviourMapping: Record<
  12. string,
  13. EnumTicketScreenBehavior
  14. > = {
  15. next_task: EnumTicketScreenBehavior.CloseTab,
  16. next_from_overview: EnumTicketScreenBehavior.CloseNextInOverview,
  17. next_task_on_close: EnumTicketScreenBehavior.CloseTabOnTicketClose,
  18. none: EnumTicketScreenBehavior.StayOnTab,
  19. }
  20. export const useMacros = (groupId: Ref<ID | undefined>) => {
  21. const macroQuery = new QueryHandler(
  22. useMacrosQuery(
  23. () => ({
  24. groupId: groupId.value as string,
  25. }),
  26. () => ({ enabled: !!groupId.value }),
  27. ),
  28. )
  29. const macroSubscription = new SubscriptionHandler(
  30. useMacrosUpdateSubscription(() => ({
  31. enabled: !!groupId.value,
  32. })),
  33. )
  34. macroSubscription.onResult((data) => {
  35. if (data.data?.macrosUpdate.macroUpdated) macroQuery.refetch()
  36. })
  37. const result = macroQuery.result()
  38. const macros = computed(() => result.value?.macros)
  39. return { macros }
  40. }
  41. export const useTicketMacros = (formSubmit: () => void) => {
  42. const activeMacro = ref<MacroById>()
  43. const executeMacro = async (macro: MacroById) => {
  44. activeMacro.value = macro
  45. formSubmit()
  46. }
  47. const disposeActiveMacro = () => {
  48. activeMacro.value = undefined
  49. }
  50. return { activeMacro, executeMacro, disposeActiveMacro }
  51. }