activeTaskbarTab.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { EnumTaskbarEntity } from '#shared/graphql/types.ts'
  3. import { useUserCurrentTaskbarTabsStore } from '#desktop/entities/user/current/stores/taskbarTabs.ts'
  4. import type {
  5. NavigationGuard,
  6. RouteLocationNormalized,
  7. NavigationGuardNext,
  8. } from 'vue-router'
  9. const activeTaskbarTab: NavigationGuard = async (
  10. to: RouteLocationNormalized,
  11. from: RouteLocationNormalized,
  12. next: NavigationGuardNext,
  13. ) => {
  14. if (
  15. !to.meta?.taskbarTabEntity ||
  16. (!to.params.internalId && !to.params.tabId)
  17. ) {
  18. if (to.meta?.requiresAuth) {
  19. // Reset the previously active tab state if the new route does not support the taskbar.
  20. // This needs to be handled here, since the activation of the next tab state happens below in the same guard,
  21. // and it may get overwritten if it's executed from a separate place (e.g. a component lifecycle method).
  22. useUserCurrentTaskbarTabsStore().resetActiveTaskbarTab()
  23. }
  24. next()
  25. return
  26. }
  27. const taskbarTabStore = useUserCurrentTaskbarTabsStore()
  28. const taskbarTabEntityType = to.meta.taskbarTabEntity as EnumTaskbarEntity
  29. const taskbarTypePlugin =
  30. taskbarTabStore.getTaskbarTabTypePlugin(taskbarTabEntityType)
  31. const tabEntityInternalId = (to.params.internalId ||
  32. to.params.tabId) as string
  33. const taskbarTabEntityKey =
  34. taskbarTypePlugin.buildEntityTabKey(tabEntityInternalId)
  35. // TODO: instead of that I would only load the single item so that the page can already start working?
  36. if (taskbarTabStore.loading) {
  37. await taskbarTabStore.waitForTaskbarListLoaded()
  38. }
  39. taskbarTabStore.upsertTaskbarTab(
  40. taskbarTabEntityType,
  41. taskbarTabEntityKey,
  42. tabEntityInternalId,
  43. )
  44. next()
  45. }
  46. export default activeTaskbarTab