activeTaskbarTab.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. await taskbarTabStore.upsertTaskbarTab(
  40. taskbarTabEntityType,
  41. taskbarTabEntityKey,
  42. tabEntityInternalId,
  43. )
  44. // Remember the entity key for the current taskbar tab,
  45. // so it can be used for checking the entity access.
  46. to.meta.taskbarTabEntityKey = taskbarTabEntityKey
  47. next()
  48. }
  49. export default activeTaskbarTab