useMacros.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, onBeforeUnmount, type Ref, ref } from 'vue'
  3. import { useRoute } from 'vue-router'
  4. import type { MacroById } from '#shared/entities/macro/types.ts'
  5. import { useMacrosQuery } from '#shared/graphql/queries/macros.api.ts'
  6. import { EnumTicketScreenBehavior } from '#shared/graphql/types.ts'
  7. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  8. import { useMacroStore } from '#shared/stores/macro.ts'
  9. export const macroScreenBehaviourMapping: Record<
  10. string,
  11. EnumTicketScreenBehavior
  12. > = {
  13. next_task: EnumTicketScreenBehavior.CloseTab,
  14. next_from_overview: EnumTicketScreenBehavior.CloseNextInOverview,
  15. next_task_on_close: EnumTicketScreenBehavior.CloseTabOnTicketClose,
  16. none: EnumTicketScreenBehavior.StayOnTab,
  17. }
  18. export const useMacros = (groupId: Ref<ID | undefined>) => {
  19. const macroQuery = new QueryHandler(
  20. useMacrosQuery(
  21. () => ({
  22. groupId: groupId.value as string,
  23. }),
  24. () => ({ enabled: !!groupId.value }),
  25. ),
  26. )
  27. const { activate, deactivate } = useMacroStore()
  28. const route = useRoute()
  29. // TODO: Drop this mechanism once Apollo implements an effective deduplication of subscriptions on the client level.
  30. // More information: https://github.com/apollographql/apollo-client/issues/10117
  31. const usageKey = route.meta.taskbarTabEntityKey ?? 'apply-template'
  32. activate(usageKey, macroQuery)
  33. onBeforeUnmount(() => {
  34. deactivate(usageKey)
  35. })
  36. const result = macroQuery.result()
  37. const macros = computed(() => result.value?.macros)
  38. return { macros }
  39. }
  40. export const useTicketMacros = (formSubmit: () => void) => {
  41. const activeMacro = ref<MacroById>()
  42. const executeMacro = async (macro: MacroById) => {
  43. activeMacro.value = macro
  44. formSubmit()
  45. }
  46. const disposeActiveMacro = () => {
  47. activeMacro.value = undefined
  48. }
  49. return { activeMacro, executeMacro, disposeActiveMacro }
  50. }