useTicketInformation.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, inject, provide } from 'vue'
  3. import { useTicketQuery } from '#shared/entities/ticket/graphql/queries/ticket.api.ts'
  4. import type { TicketById } from '#shared/entities/ticket/types.ts'
  5. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  6. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  7. import type { TicketInformation } from '#desktop/entities/ticket/types.ts'
  8. import type { Ref, InjectionKey } from 'vue'
  9. export const TICKET_KEY = Symbol('ticket') as InjectionKey<TicketInformation>
  10. export const initializeTicketInformation = (
  11. internalId: Ref<number | string>,
  12. ) => {
  13. const ticketId = computed(() =>
  14. convertToGraphQLId('Ticket', internalId.value),
  15. )
  16. const ticketQuery = new QueryHandler(
  17. // Currently we need no subscribeToMore here, because the tab registration holds the ticket subscription.
  18. useTicketQuery(
  19. () => ({
  20. ticketId: ticketId.value,
  21. }),
  22. { fetchPolicy: 'cache-first' },
  23. ),
  24. )
  25. const result = ticketQuery.result()
  26. const ticket = computed(() => result.value?.ticket as TicketById)
  27. return {
  28. ticket,
  29. ticketId,
  30. ticketInternalId: internalId as Ref<number>,
  31. }
  32. }
  33. export const provideTicketInformation = (data: TicketInformation) => {
  34. provide(TICKET_KEY, data)
  35. }
  36. export const useTicketInformation = () => {
  37. return inject(TICKET_KEY) as TicketInformation
  38. }