usePublicLinks.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { usePublicLinksQuery } from '#shared/entities/public-links/graphql/queries/links.api.ts'
  3. import { PublicLinkUpdatesDocument } from '#shared/entities/public-links/graphql/subscriptions/currentLinks.api.ts'
  4. import {
  5. EnumPublicLinksScreen,
  6. type PublicLinkUpdatesSubscriptionVariables,
  7. type PublicLinkUpdatesSubscription,
  8. type PublicLinksQuery,
  9. } from '#shared/graphql/types.ts'
  10. import QueryHandler from '#shared/server/apollo/handler/QueryHandler.ts'
  11. import { computed } from 'vue'
  12. export const usePublicLinks = (screen: EnumPublicLinksScreen) => {
  13. const publicLinksQuery = new QueryHandler(usePublicLinksQuery({ screen }))
  14. publicLinksQuery.subscribeToMore<
  15. PublicLinkUpdatesSubscriptionVariables,
  16. PublicLinkUpdatesSubscription
  17. >({
  18. document: PublicLinkUpdatesDocument,
  19. variables: {
  20. screen,
  21. },
  22. updateQuery(_, { subscriptionData }) {
  23. const publicLinks = subscriptionData.data.publicLinkUpdates?.publicLinks
  24. // if we return empty array here, the actual query will be aborted, because we have fetchPolicy "cache-and-network"
  25. // if we return existing value, it will throw an error, because "publicLinks" doesn't exist yet on the query result
  26. if (!publicLinks) {
  27. return null as unknown as PublicLinksQuery
  28. }
  29. return {
  30. publicLinks,
  31. }
  32. },
  33. })
  34. const links = computed(() => {
  35. const publicLinks = publicLinksQuery.result()
  36. return publicLinks.value?.publicLinks || []
  37. })
  38. return { links }
  39. }