application.ts 4.5 KB

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