storeSubscriptions.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 { useApplicationStore } from '#shared/stores/application.ts'
  5. import { useLocaleStore } from '#shared/stores/locale.ts'
  6. import { useSessionStore } from '#shared/stores/session.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. }