usePublicLinks.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed } from 'vue'
  3. import { usePublicLinksQuery } from '#shared/entities/public-links/graphql/queries/links.api.ts'
  4. import { PublicLinkUpdatesDocument } from '#shared/entities/public-links/graphql/subscriptions/currentLinks.api.ts'
  5. import {
  6. EnumPublicLinksScreen,
  7. type PublicLinkUpdatesSubscriptionVariables,
  8. type PublicLinkUpdatesSubscription,
  9. type PublicLinksQuery,
  10. } from '#shared/graphql/types.ts'
  11. import QueryHandler from '#shared/server/apollo/handler/QueryHandler.ts'
  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. }