Line data Source code
1 : import { computed, readonly, watch } from 'vue'
2 :
3 : import { t } from '@/i18n'
4 : import { isCloud } from '@/platform/distribution/types'
5 : import { useToast } from '@/components/ui/toast/toastStore'
6 : import { api } from '@/scripts/api'
7 : import { useSystemStatsStore } from '@/stores/systemStatsStore'
8 :
9 2356 : export enum ManagerUIState {
10 2356 : DISABLED = 'disabled',
11 2356 : LEGACY_UI = 'legacy',
12 2356 : NEW_UI = 'new',
13 2356 : INCOMPATIBLE = 'incompatible'
14 : }
15 :
16 : /**
17 : * Module-level flag to ensure the INCOMPATIBLE upgrade toast fires exactly
18 : * once per app session, even when useManagerState() is invoked from many
19 : * components. Without this, every consumer would register its own watcher
20 : * and stack duplicate toasts on the first transition.
21 : */
22 2356 : let incompatibleToastShown = false
23 :
24 : /**
25 : * Shows the Manager upgrade toast. Guarded to once per session unless
26 : * `force` is set, which explicit user actions use to re-surface guidance.
27 : */
28 2356 : export const showIncompatibleManagerToast = ({ force = false } = {}): void => {
29 0 : if (incompatibleToastShown && !force) return
30 0 : incompatibleToastShown = true
31 0 : useToast().warning(t('manager.incompatibleVersion.title'), {
32 : description: t('manager.incompatibleVersion.message'),
33 : duration: 15000
34 : })
35 : }
36 :
37 23106 : export function getManagerUIState(): ManagerUIState {
38 23106 : const systemStatsStore = useSystemStatsStore()
39 23106 : if (!systemStatsStore.isInitialized) {
40 4710 : return ManagerUIState.DISABLED
41 : }
42 :
43 18396 : const clientSupportsV4 =
44 : api.getClientFeatureFlags().supports_manager_v4_ui === true
45 18396 : const serverSupportsV4 = api.getServerFeature('extension.manager.supports_v4')
46 18396 : const supportsCsrfPost = api.getServerFeature(
47 : 'extension.manager.supports_csrf_post'
48 : )
49 : const hasEnableManager =
50 : systemStatsStore.systemStats?.system.argv.includes('--enable-manager')
51 :
52 23106 : if (!hasEnableManager) {
53 18153 : return ManagerUIState.DISABLED
54 : }
55 :
56 23106 : if (
57 : systemStatsStore.systemStats?.system.argv.includes(
58 : '--enable-manager-legacy-ui'
59 : )
60 : ) {
61 0 : return ManagerUIState.LEGACY_UI
62 : }
63 :
64 23106 : if (serverSupportsV4 === true && supportsCsrfPost !== true) {
65 0 : return ManagerUIState.INCOMPATIBLE
66 : }
67 :
68 23106 : if (clientSupportsV4 && serverSupportsV4 === true) {
69 183 : return ManagerUIState.NEW_UI
70 : }
71 :
72 23106 : if (serverSupportsV4 === true && !clientSupportsV4) {
73 0 : return ManagerUIState.LEGACY_UI
74 : }
75 :
76 273 : if (serverSupportsV4 === false) {
77 0 : return ManagerUIState.LEGACY_UI
78 : }
79 :
80 60 : if (serverSupportsV4 === undefined) {
81 60 : return ManagerUIState.NEW_UI
82 : }
83 :
84 0 : return ManagerUIState.DISABLED
85 : }
86 :
87 6907 : export function useManagerState() {
88 6907 : const managerUIState = readonly(computed(getManagerUIState))
89 :
90 6907 : const isManagerEnabled = readonly(
91 : computed((): boolean => {
92 2053 : return (
93 : managerUIState.value !== ManagerUIState.DISABLED &&
94 : managerUIState.value !== ManagerUIState.INCOMPATIBLE
95 : )
96 : })
97 : )
98 :
99 6907 : const isNewManagerUI = readonly(
100 : computed((): boolean => {
101 4779 : return managerUIState.value === ManagerUIState.NEW_UI
102 : })
103 : )
104 :
105 6907 : const isLegacyManagerUI = readonly(
106 : computed((): boolean => {
107 0 : return managerUIState.value === ManagerUIState.LEGACY_UI
108 : })
109 : )
110 :
111 6907 : const isIncompatibleManager = readonly(
112 : computed((): boolean => {
113 0 : return managerUIState.value === ManagerUIState.INCOMPATIBLE
114 : })
115 : )
116 :
117 6907 : const shouldShowInstallButton = readonly(
118 : computed((): boolean => {
119 0 : return isNewManagerUI.value
120 : })
121 : )
122 :
123 6907 : const shouldShowManagerButtons = readonly(
124 : computed((): boolean => {
125 2053 : return isManagerEnabled.value
126 : })
127 : )
128 :
129 6907 : const shouldShowExtensionsButton = readonly(
130 : computed((): boolean => isCloud || shouldShowManagerButtons.value)
131 : )
132 :
133 6907 : watch(
134 : managerUIState,
135 : (state) => {
136 6922 : if (state === ManagerUIState.INCOMPATIBLE) {
137 0 : showIncompatibleManagerToast()
138 : }
139 : },
140 : { immediate: true }
141 : )
142 :
143 6907 : return {
144 : managerUIState,
145 : isManagerEnabled,
146 : isNewManagerUI,
147 : isLegacyManagerUI,
148 : isIncompatibleManager,
149 : shouldShowInstallButton,
150 : shouldShowManagerButtons,
151 : shouldShowExtensionsButton
152 : }
153 : }
154 :
155 : // Test-only export: resets the once-per-session toast guard so unit tests
156 : // can assert toast firing across multiple `useManagerState()` invocations.
157 : export const __resetIncompatibleToastGuard = (): void => {
158 : incompatibleToastShown = false
159 : }
|