activeTaskbarTab.ts 2.1 KB

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