Line data Source code
1 : import { whenever } from '@vueuse/core'
2 : import { computed, nextTick, onMounted, onUnmounted } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { useToast } from '@/components/ui/toast/toastStore'
6 : import { useVersionCompatibilityStore } from './versionCompatibilityStore'
7 :
8 : interface UseFrontendVersionMismatchWarningOptions {
9 : immediate?: boolean
10 : }
11 :
12 : /**
13 : * Composable for handling frontend version mismatch warnings.
14 : *
15 : * Displays toast notifications when the frontend version is incompatible with the backend,
16 : * either because the frontend is outdated or newer than the backend expects.
17 : * Automatically dismisses warnings when shown and persists dismissal state for 7 days.
18 : *
19 : * @param options - Configuration options
20 : * @param options.immediate - If true, automatically shows warning when version mismatch is detected
21 : * @returns Object with methods and computed properties for managing version warnings
22 : *
23 : * @example
24 : * ```ts
25 : * // Show warning immediately when mismatch detected
26 : * const { showWarning, shouldShowWarning } = useFrontendVersionMismatchWarning({ immediate: true })
27 : *
28 : * // Manual control
29 : * const { showWarning } = useFrontendVersionMismatchWarning()
30 : * showWarning() // Call when needed
31 : * ```
32 : */
33 2355 : export function useFrontendVersionMismatchWarning(
34 : options: UseFrontendVersionMismatchWarningOptions = {}
35 : ) {
36 2355 : const { immediate = false } = options
37 2355 : const { t } = useI18n()
38 2355 : const toast = useToast()
39 2355 : const versionCompatibilityStore = useVersionCompatibilityStore()
40 :
41 : // Track if we've already shown the warning
42 2355 : let hasShownWarning = false
43 :
44 2355 : const emitAlert = (detail: string) => {
45 1 : toast.warning(t('g.versionMismatchWarning'), {
46 : description: t('g.versionMismatchUpdateInstructions', { detail })
47 : })
48 : }
49 :
50 2355 : const showWarning = () => {
51 : // Prevent showing the warning multiple times
52 1 : if (hasShownWarning) return
53 :
54 1 : const message = versionCompatibilityStore.warningMessage
55 1 : const packageMessages = versionCompatibilityStore.packageWarningMessages
56 1 : if (!message && packageMessages.length === 0) return
57 :
58 1 : if (message) {
59 1 : emitAlert(
60 : t('g.frontendOutdated', {
61 : frontendVersion: message.frontendVersion,
62 : requiredVersion: message.requiredVersion
63 : })
64 : )
65 : }
66 :
67 1 : for (const pkg of packageMessages) {
68 0 : emitAlert(
69 : t('g.comfyPackageOutdated', {
70 : name: pkg.name,
71 : installedVersion: pkg.installedVersion,
72 : requiredVersion: pkg.requiredVersion
73 : })
74 : )
75 : }
76 :
77 1 : hasShownWarning = true
78 :
79 : // Automatically dismiss the warning so it won't show again for 7 days
80 1 : versionCompatibilityStore.dismissWarning()
81 : }
82 :
83 2355 : let stopWatcher: (() => void) | null = null
84 :
85 2355 : onMounted(async () => {
86 2355 : if (!immediate) return
87 : // Wait for next tick to ensure reactive updates from settings load have propagated
88 2355 : await nextTick()
89 :
90 2355 : stopWatcher = whenever(
91 : () => versionCompatibilityStore.shouldShowWarning,
92 : () => {
93 1 : showWarning()
94 : },
95 : { immediate: true }
96 : )
97 : })
98 :
99 2355 : onUnmounted(() => {
100 0 : stopWatcher?.()
101 0 : stopWatcher = null
102 : })
103 :
104 2355 : return {
105 : showWarning,
106 : shouldShowWarning: computed(
107 : () => versionCompatibilityStore.shouldShowWarning
108 : ),
109 : dismissWarning: versionCompatibilityStore.dismissWarning,
110 : hasVersionMismatch: computed(
111 : () => versionCompatibilityStore.hasVersionMismatch
112 : )
113 : }
114 : }
|