useTaskbarTab.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { isEqual } from 'lodash-es'
  3. import { storeToRefs } from 'pinia'
  4. import {
  5. computed,
  6. inject,
  7. provide,
  8. watch,
  9. type ComputedRef,
  10. type InjectionKey,
  11. type Ref,
  12. } from 'vue'
  13. import type { UserTaskbarTab } from '#desktop/components/UserTaskbarTabs/types.ts'
  14. import { useUserCurrentTaskbarTabsStore } from '../stores/taskbarTabs.ts'
  15. import type { TaskbarTabContext } from '../types.ts'
  16. interface CurrentTaskbarTabData {
  17. currentTaskbarTab: ComputedRef<UserTaskbarTab | undefined>
  18. currentTaskbarTabId: ComputedRef<string | undefined>
  19. currentTaskbarEntityKey: string | undefined
  20. currentTaskbarTabFormId: ComputedRef<string | undefined>
  21. currentTaskbarTabNewArticlePresent: ComputedRef<boolean>
  22. }
  23. export const CURRENT_TASKBAR_TAB_KEY = Symbol(
  24. 'current-taskbar-tab',
  25. ) as InjectionKey<CurrentTaskbarTabData>
  26. export const initializeCurrentTaskbarTab = (taskbarEntityKey?: string) => {
  27. const { taskbarTabListByTabEntityKey } = storeToRefs(
  28. useUserCurrentTaskbarTabsStore(),
  29. )
  30. const currentTaskbarTab = computed<UserTaskbarTab | undefined>(
  31. (existingTaskbarTab) => {
  32. if (!taskbarEntityKey) return
  33. if (
  34. existingTaskbarTab &&
  35. isEqual(
  36. existingTaskbarTab,
  37. taskbarTabListByTabEntityKey.value[taskbarEntityKey],
  38. )
  39. ) {
  40. return existingTaskbarTab
  41. }
  42. return taskbarTabListByTabEntityKey.value[taskbarEntityKey]
  43. },
  44. )
  45. const currentTaskbarTabEntityAccess = computed(
  46. () => currentTaskbarTab.value?.entityAccess,
  47. )
  48. const currentTaskbarTabId = computed(
  49. () => currentTaskbarTab.value?.taskbarTabId,
  50. )
  51. const currentTaskbarTabFormId = computed(
  52. () => currentTaskbarTab.value?.formId || undefined,
  53. )
  54. const currentTaskbarTabNewArticlePresent = computed(
  55. () => !!currentTaskbarTab.value?.formNewArticlePresent,
  56. )
  57. return {
  58. currentTaskbarTab,
  59. currentTaskbarTabEntityAccess,
  60. currentTaskbarTabId,
  61. currentTaskbarTabFormId,
  62. currentTaskbarTabNewArticlePresent,
  63. }
  64. }
  65. export const provideCurrentTaskbarTab = (data: CurrentTaskbarTabData) => {
  66. provide(CURRENT_TASKBAR_TAB_KEY, data)
  67. }
  68. export const useTaskbarTab = (context?: Ref<TaskbarTabContext>) => {
  69. const { taskbarTabContexts } = storeToRefs(useUserCurrentTaskbarTabsStore())
  70. const {
  71. currentTaskbarTab,
  72. currentTaskbarTabId,
  73. currentTaskbarTabFormId,
  74. currentTaskbarEntityKey,
  75. currentTaskbarTabNewArticlePresent,
  76. } = inject(CURRENT_TASKBAR_TAB_KEY) as CurrentTaskbarTabData
  77. const { updateTaskbarTab, deleteTaskbarTab } =
  78. useUserCurrentTaskbarTabsStore()
  79. // Keep track of the passed context and update the store state accordingly.
  80. if (context) {
  81. watch(
  82. context,
  83. (newValue) => {
  84. if (!currentTaskbarTab.value?.tabEntityKey) return
  85. taskbarTabContexts.value[currentTaskbarTab.value.tabEntityKey] =
  86. newValue
  87. },
  88. { immediate: true },
  89. )
  90. }
  91. watch(
  92. () =>
  93. currentTaskbarTab.value &&
  94. taskbarTabContexts.value[currentTaskbarTab.value.tabEntityKey]
  95. ?.formIsDirty,
  96. (isDirty) => {
  97. if (isDirty === undefined || !currentTaskbarTab.value?.taskbarTabId)
  98. return
  99. if (currentTaskbarTab.value.dirty === isDirty) return
  100. updateTaskbarTab(currentTaskbarTab.value.taskbarTabId, {
  101. ...currentTaskbarTab.value,
  102. dirty: isDirty,
  103. })
  104. },
  105. )
  106. const currentTaskbarTabDelete = () => {
  107. if (!currentTaskbarTabId.value) return
  108. deleteTaskbarTab(currentTaskbarTabId.value)
  109. }
  110. return {
  111. currentTaskbarTab,
  112. currentTaskbarEntityKey,
  113. currentTaskbarTabId,
  114. currentTaskbarTabFormId,
  115. currentTaskbarTabNewArticlePresent,
  116. currentTaskbarTabDelete,
  117. }
  118. }