storeSubscriptions.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { watch } from 'vue'
  3. import { triggerWebSocketReconnect } from '#shared/server/connection.ts'
  4. import { useLocaleStore } from '#shared/stores/locale.ts'
  5. import { useSessionStore } from '#shared/stores/session.ts'
  6. import { useApplicationStore } from '#shared/stores/application.ts'
  7. export default function initializeStoreSubscriptions(): void {
  8. const session = useSessionStore()
  9. const locale = useLocaleStore()
  10. const application = useApplicationStore()
  11. watch(
  12. () => application.loaded,
  13. () => {
  14. watch(
  15. () => session.id,
  16. () => {
  17. // Reopen WS connection to reflect authentication state.
  18. triggerWebSocketReconnect()
  19. },
  20. )
  21. watch(
  22. () => session.user,
  23. (newValue, oldValue) => {
  24. if (
  25. !newValue ||
  26. (oldValue?.preferences?.locale &&
  27. locale.localeData &&
  28. newValue.preferences?.locale !== locale.localeData.locale)
  29. ) {
  30. locale.setLocale(newValue?.preferences?.locale)
  31. }
  32. },
  33. )
  34. },
  35. )
  36. }