useTaskbarTabStateUpdates.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { ref } from 'vue'
  4. import SubscriptionHandler from '#shared/server/apollo/handler/SubscriptionHandler.ts'
  5. import type { FormUpdaterOptions } from '#shared/types/form.ts'
  6. import { useUserCurrentTaskbarItemStateUpdatesSubscription } from '../graphql/subscriptions/userCurrentTaskbarItemStateUpdates.api.ts'
  7. import { useUserCurrentTaskbarTabsStore } from '../stores/taskbarTabs.ts'
  8. export const useTaskbarTabStateUpdates = (
  9. autoSaveTriggerFormUpdater: (options?: FormUpdaterOptions) => void,
  10. ) => {
  11. const skipNextStateUpdate = ref(false)
  12. const { activeTaskbarTabId } = storeToRefs(useUserCurrentTaskbarTabsStore())
  13. const setSkipNextStateUpdate = (skip: boolean) => {
  14. skipNextStateUpdate.value = skip
  15. }
  16. const stateUpdatesSubscription = new SubscriptionHandler(
  17. useUserCurrentTaskbarItemStateUpdatesSubscription(
  18. () => ({
  19. taskbarItemId: activeTaskbarTabId.value as string,
  20. }),
  21. () => ({
  22. enabled: !!activeTaskbarTabId.value,
  23. }),
  24. ),
  25. )
  26. stateUpdatesSubscription.onResult((result) => {
  27. if (
  28. activeTaskbarTabId.value &&
  29. !skipNextStateUpdate.value &&
  30. result.data?.userCurrentTaskbarItemStateUpdates.stateChanged
  31. ) {
  32. autoSaveTriggerFormUpdater({
  33. additionalParams: {
  34. taskbarId: activeTaskbarTabId.value,
  35. applyTaskbarState: true,
  36. },
  37. })
  38. }
  39. setSkipNextStateUpdate(false)
  40. })
  41. return { setSkipNextStateUpdate }
  42. }