useTicketInformation.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { injectLocal, provideLocal } from '@vueuse/shared'
  3. import { computed } from 'vue'
  4. import { useTicketQuery } from '#shared/entities/ticket/graphql/queries/ticket.api.ts'
  5. import type { TicketById } from '#shared/entities/ticket/types.ts'
  6. import { useErrorHandler } from '#shared/errors/useErrorHandler.ts'
  7. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  8. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  9. import type { TicketInformation } from '#desktop/entities/ticket/types.ts'
  10. import type { Ref, InjectionKey } from 'vue'
  11. export const TICKET_KEY = Symbol('ticket') as InjectionKey<TicketInformation>
  12. export const useProvideTicketInformation = (
  13. internalId: Ref<number | string>,
  14. ) => {
  15. const ticketId = computed(() =>
  16. convertToGraphQLId('Ticket', internalId.value),
  17. )
  18. // TODO: stay witht his for now, but need to be re-implemented for the tab situation.
  19. const { createQueryErrorHandler } = useErrorHandler()
  20. const ticketQuery = new QueryHandler(
  21. // Currently we need no subscribeToMore here, because the tab registration holds the ticket subscription.
  22. useTicketQuery(
  23. () => ({
  24. ticketId: ticketId.value,
  25. }),
  26. { fetchPolicy: 'cache-first' },
  27. ),
  28. {
  29. errorCallback: createQueryErrorHandler({
  30. notFound: __(
  31. 'Ticket with specified ID was not found. Try checking the URL for errors.',
  32. ),
  33. forbidden: __('You have insufficient rights to view this ticket.'),
  34. }),
  35. },
  36. )
  37. const result = ticketQuery.result()
  38. const ticket = computed(() => result.value?.ticket as TicketById)
  39. provideLocal(TICKET_KEY, {
  40. ticket,
  41. ticketId,
  42. ticketInternalId: internalId as Ref<number>,
  43. })
  44. }
  45. export const useTicketInformation = () => {
  46. return injectLocal(TICKET_KEY) as TicketInformation
  47. }