application.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { defineStore } from 'pinia'
  3. import { computed, effectScope, ref, type Ref } from 'vue'
  4. import { useNotifications } from '#shared/components/CommonNotifications/index.ts'
  5. import { useApplicationLoaded } from '#shared/composables/useApplicationLoaded.ts'
  6. import { useApplicationConfigQuery } from '#shared/graphql/queries/applicationConfig.api.ts'
  7. import { useConfigUpdatesSubscription } from '#shared/graphql/subscriptions/configUpdates.api.ts'
  8. import type {
  9. ApplicationConfigQuery,
  10. ApplicationConfigQueryVariables,
  11. } from '#shared/graphql/types.ts'
  12. import {
  13. QueryHandler,
  14. SubscriptionHandler,
  15. } from '#shared/server/apollo/handler/index.ts'
  16. import type { ConfigList } from '#shared/types/store.ts'
  17. import testFlags from '#shared/utils/testFlags.ts'
  18. let configUpdatesSubscriptionInitialized = false
  19. let applicationConfigQuery: QueryHandler<
  20. ApplicationConfigQuery,
  21. ApplicationConfigQueryVariables
  22. >
  23. const getApplicationConfigQuery = () => {
  24. if (applicationConfigQuery) return applicationConfigQuery
  25. const scope = effectScope()
  26. scope.run(() => {
  27. applicationConfigQuery = new QueryHandler(
  28. useApplicationConfigQuery({ fetchPolicy: 'no-cache' }),
  29. )
  30. })
  31. return applicationConfigQuery
  32. }
  33. // TODO: consider switching from notification to a modal dialog, and improving the message
  34. const notifications = useNotifications()
  35. const { loaded } = useApplicationLoaded()
  36. export const useApplicationStore = defineStore(
  37. 'application',
  38. () => {
  39. const loading = computed(() => !loaded.value)
  40. const initialized = ref(false)
  41. const setLoaded = (): void => {
  42. if (notifications.hasErrors()) {
  43. const loadingAppElement: Maybe<HTMLElement> =
  44. document.getElementById('loading-app')
  45. loadingAppElement
  46. ?.getElementsByClassName('loading-animation')
  47. .item(0)
  48. ?.classList.add('error')
  49. loadingAppElement
  50. ?.getElementsByClassName('loading-sr-text')
  51. .item(0)
  52. ?.setAttribute('aria-hidden', 'true')
  53. const loadingFailedElement = loadingAppElement
  54. ?.getElementsByClassName('loading-failed')
  55. .item(0)
  56. loadingFailedElement?.classList.add('active')
  57. loadingFailedElement?.setAttribute('aria-hidden', 'false')
  58. return
  59. }
  60. loaded.value = true
  61. testFlags.set('applicationLoaded.loaded')
  62. }
  63. const config = ref<Record<string, unknown>>({})
  64. const initializeConfigUpdateSubscription = (): void => {
  65. const scope = effectScope()
  66. scope.run(() => {
  67. const configUpdatesSubscription = new SubscriptionHandler(
  68. useConfigUpdatesSubscription(),
  69. )
  70. configUpdatesSubscription.onResult((result) => {
  71. const updatedSetting = result.data?.configUpdates.setting
  72. if (updatedSetting) {
  73. config.value[updatedSetting.key] = updatedSetting.value
  74. } else {
  75. testFlags.set('useConfigUpdatesSubscription.subscribed')
  76. }
  77. })
  78. configUpdatesSubscriptionInitialized = true
  79. })
  80. }
  81. const getConfig = async (): Promise<void> => {
  82. const configQuery = getApplicationConfigQuery()
  83. const { data: result } = await configQuery.refetch()
  84. if (result?.applicationConfig) {
  85. result.applicationConfig.forEach((item) => {
  86. config.value[item.key] = item.value
  87. })
  88. // app/assets/javascripts/app/config.coffee
  89. config.value.api_path = '/api/v1'
  90. }
  91. if (!configUpdatesSubscriptionInitialized) {
  92. initializeConfigUpdateSubscription()
  93. }
  94. }
  95. const resetAndGetConfig = async (): Promise<void> => {
  96. config.value = {}
  97. await getConfig()
  98. }
  99. const hasCustomProductBranding = computed(() =>
  100. Boolean(
  101. config.value.product_logo && config.value.product_logo !== 'logo.svg',
  102. ),
  103. )
  104. // this is called right before router renders a page, - we remove "loading" element here instead of "setLoaded"
  105. // so we don't have a short period when App route is not fetched yet and we see a black screen
  106. const setInitialized = () => {
  107. initialized.value = true
  108. const appElement = document.getElementById('app')
  109. if (appElement) {
  110. appElement.dataset.loaded = 'true'
  111. }
  112. const loadingAppElement = document.getElementById('loading-app')
  113. loadingAppElement?.remove()
  114. }
  115. return {
  116. loaded,
  117. loading,
  118. setLoaded,
  119. config: config as unknown as Ref<ConfigList>,
  120. initializeConfigUpdateSubscription,
  121. getConfig,
  122. resetAndGetConfig,
  123. hasCustomProductBranding,
  124. initialized,
  125. setInitialized,
  126. }
  127. },
  128. {
  129. requiresAuth: false,
  130. },
  131. )