123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { useRouteQuery } from '@vueuse/router'
- import { onMounted, reactive, watch } from 'vue'
- import {
- useNotifications,
- NotificationTypes,
- } from '#shared/components/CommonNotifications/index.ts'
- import { useApplicationBuildChecksumQuery } from '#shared/graphql/queries/applicationBuildChecksum.api.ts'
- import { useAppMaintenanceSubscription } from '#shared/graphql/subscriptions/appMaintenance.api.ts'
- import type {
- ApplicationBuildChecksumQuery,
- ApplicationBuildChecksumQueryVariables,
- AppMaintenanceSubscription,
- AppMaintenanceSubscriptionVariables,
- } from '#shared/graphql/types.ts'
- import { EnumAppMaintenanceType } from '#shared/graphql/types.ts'
- import {
- QueryHandler,
- SubscriptionHandler,
- } from '#shared/server/apollo/handler/index.ts'
- import testFlags from '#shared/utils/testFlags.ts'
- let checksumQuery: QueryHandler<
- ApplicationBuildChecksumQuery,
- ApplicationBuildChecksumQueryVariables
- >
- let previousChecksum: string
- let appMaintenanceSubscription: SubscriptionHandler<
- AppMaintenanceSubscription,
- AppMaintenanceSubscriptionVariables
- >
- interface UseAppMaintenanceCheckOptions {
- onNeedRefresh?: () => void
- }
- const useAppMaintenanceCheck = (
- maintenanceOptions: UseAppMaintenanceCheckOptions = {},
- ) => {
- const notify = (message: string, callback?: () => void) => {
- useNotifications().notify({
- id: 'app-maintenance',
- message,
- type: NotificationTypes.Warn,
- persistent: true,
- callback,
- })
- }
- onMounted(() => {
- if (checksumQuery) return
- // Default poll interval: every minute.
- const defaultPollInterval = 60 * 1000
- const applicationRebuildCheckInterval = useRouteQuery(
- 'ApplicationRebuildCheckInterval',
- defaultPollInterval.toString(),
- )
- const options = reactive({
- pollInterval: parseInt(applicationRebuildCheckInterval.value, 10),
- })
- watch(applicationRebuildCheckInterval, () => {
- options.pollInterval = parseInt(applicationRebuildCheckInterval.value, 10)
- })
- checksumQuery = new QueryHandler(useApplicationBuildChecksumQuery(options))
- const notificationMessage = __(
- 'A newer version of the app is available. Please reload at your earliest.',
- )
- checksumQuery.watchOnResult((queryResult): void => {
- if (!queryResult?.applicationBuildChecksum.length) return
- if (!previousChecksum) {
- previousChecksum = queryResult?.applicationBuildChecksum
- testFlags.set('useApplicationBuildChecksumQuery.firstResult')
- }
- if (queryResult?.applicationBuildChecksum !== previousChecksum) {
- notify(notificationMessage, maintenanceOptions.onNeedRefresh)
- }
- })
- appMaintenanceSubscription = new SubscriptionHandler(
- useAppMaintenanceSubscription(),
- )
- appMaintenanceSubscription.onResult((result) => {
- const type = result.data?.appMaintenance?.type
- let message = notificationMessage
- if (!type) {
- testFlags.set('useAppMaintenanceSubscription.subscribed')
- return
- }
- switch (type) {
- case EnumAppMaintenanceType.ConfigChanged:
- message = __(
- 'The configuration of Zammad has changed. Please reload at your earliest.',
- )
- break
- case EnumAppMaintenanceType.RestartAuto:
- case EnumAppMaintenanceType.RestartManual:
- // TODO: this case cannot be handled right now. Legacy interface performs a connectivity check.
- break
- default:
- break
- }
- notify(message, () => window.location.reload())
- })
- })
- }
- export default useAppMaintenanceCheck
|