useTicketExternalIssueTracker.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { isEqual } from 'lodash-es'
  3. import { computed, ref, type Ref, watch } from 'vue'
  4. import { EnumTicketExternalReferencesIssueTrackerType } from '#shared/graphql/types.ts'
  5. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  6. import { useTicketExternalReferencesIssueTrackerItemListQuery } from '#desktop/pages/ticket/graphql/queries/ticketExternalReferencesIssueTrackerList.api.ts'
  7. import { TicketSidebarScreenType } from '#desktop/pages/ticket/types/sidebar.ts'
  8. export const useTicketExternalIssueTracker = (
  9. screenType: TicketSidebarScreenType,
  10. issueTrackerType: EnumTicketExternalReferencesIssueTrackerType,
  11. links: Ref<string[]>,
  12. ticketId?: string,
  13. ) => {
  14. const skipNextLinkUpdate = ref(false)
  15. const issueTrackerQuery = new QueryHandler(
  16. useTicketExternalReferencesIssueTrackerItemListQuery(
  17. () => ({
  18. ticketId,
  19. issueTrackerType,
  20. issueTrackerLinks: ticketId ? undefined : links.value,
  21. }),
  22. () => ({
  23. enabled:
  24. screenType === TicketSidebarScreenType.TicketCreate
  25. ? links.value.length > 0
  26. : !!ticketId,
  27. fetchPolicy:
  28. screenType === TicketSidebarScreenType.TicketCreate
  29. ? 'cache-first'
  30. : 'cache-and-network',
  31. }),
  32. ),
  33. )
  34. const isLoading = issueTrackerQuery.loading()
  35. const queryResult = issueTrackerQuery.result()
  36. const issueList = computed(
  37. () => queryResult.value?.ticketExternalReferencesIssueTrackerItemList,
  38. )
  39. const isLoadingIssues = computed(() => {
  40. // Return already true when a checklist result already exists from the cache, also
  41. // when maybe a loading is in progress(because of cache + network).
  42. if (issueList.value !== undefined) return false
  43. return isLoading.value
  44. })
  45. const issueListUrls = computed(() => {
  46. return issueList.value?.map((item) => item.url)
  47. })
  48. if (ticketId) {
  49. watch(links, (newValue) => {
  50. if (isEqual(newValue, issueListUrls.value) || skipNextLinkUpdate.value) {
  51. skipNextLinkUpdate.value = false
  52. return
  53. }
  54. issueTrackerQuery.refetch()
  55. })
  56. }
  57. return { isLoadingIssues, issueList, skipNextLinkUpdate }
  58. }