useTaskbarTab.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { computed, watch, type Ref } from 'vue'
  4. import type { EnumTaskbarEntity } from '#shared/graphql/types.ts'
  5. import { useUserCurrentTaskbarTabsStore } from '../stores/taskbarTabs.ts'
  6. import type { TaskbarTabContext } from '../types.ts'
  7. export const useTaskbarTab = (
  8. tabEntityType: EnumTaskbarEntity,
  9. context?: Ref<TaskbarTabContext>,
  10. ) => {
  11. const { activeTaskbarTabContext, activeTaskbarTab } = storeToRefs(
  12. useUserCurrentTaskbarTabsStore(),
  13. )
  14. const { updateTaskbarTab, deleteTaskbarTab } =
  15. useUserCurrentTaskbarTabsStore()
  16. // Keep track of the passed context and update the store state accordingly.
  17. if (context) {
  18. watch(
  19. context,
  20. (newValue) => {
  21. activeTaskbarTabContext.value = newValue
  22. },
  23. { immediate: true },
  24. )
  25. }
  26. watch(
  27. () => activeTaskbarTabContext.value?.formIsDirty,
  28. (isDirty) => {
  29. if (isDirty === undefined || !activeTaskbarTab.value?.taskbarTabId) return
  30. if (activeTaskbarTab.value.dirty === isDirty) return
  31. updateTaskbarTab(activeTaskbarTab.value.taskbarTabId, {
  32. ...activeTaskbarTab.value,
  33. dirty: isDirty,
  34. })
  35. },
  36. )
  37. const activeTaskbarTabFormId = computed(
  38. () => activeTaskbarTab.value?.formId || undefined,
  39. )
  40. const activeTaskbarTabNewArticlePresent = computed(
  41. () => !!activeTaskbarTab.value?.formNewArticlePresent,
  42. )
  43. const activeTaskbarTabDelete = () => {
  44. if (!activeTaskbarTab.value?.taskbarTabId) return
  45. deleteTaskbarTab(activeTaskbarTab.value?.taskbarTabId)
  46. }
  47. return {
  48. activeTaskbarTabFormId,
  49. activeTaskbarTabNewArticlePresent,
  50. activeTaskbarTab,
  51. activeTaskbarTabDelete,
  52. }
  53. }