Line data Source code
1 : <template>
2 : <div
3 2009 : v-if="isOffered && isVisible"
4 : class="flex shrink-0 items-center"
5 : data-testid="deployment-switcher"
6 : >
7 : <Menu
8 : v-model:open="isOpen"
9 : :modal="false"
10 : :side-offset="8"
11 : :class="cn(switcherMenuWidthClass, switcherMenuMotionClass)"
12 : >
13 : <template #trigger>
14 : <Button
15 : variant="secondary"
16 : size="unset"
17 : :disabled="isSwitching"
18 : :tooltip="hoverTooltip"
19 : tooltip-side="bottom"
20 : :aria-label="triggerLabel"
21 : class="group h-8 gap-1 rounded-lg pr-1.5 pl-2 data-[state=open]:bg-accent"
22 : data-testid="deployment-switcher-trigger"
23 : >
24 : <span class="relative flex">
25 : <DeploymentStatusIcon
26 : :tone="currentTone"
27 : icon-class="size-4"
28 : ring-class="ring-2 ring-secondary group-hover:ring-accent group-data-[state=open]:ring-accent"
29 : />
30 : <span
31 : v-if="isNew"
32 : class="absolute -top-0.75 -right-0.75 size-2 rounded-full bg-foreground ring-2 ring-secondary group-hover:ring-accent group-data-[state=open]:ring-accent"
33 : aria-hidden="true"
34 : data-testid="deployment-switcher-new"
35 : />
36 : </span>
37 : <i
38 : class="icon-[lucide--chevron-down] size-3 text-muted-foreground transition-transform duration-150 group-data-[state=open]:rotate-180"
39 : aria-hidden="true"
40 : />
41 : </Button>
42 : </template>
43 : <DeploymentSwitcherList
44 : :switching-to
45 : @choose="choose"
46 : @follow="follow"
47 : @set-default="changeDefault"
48 : @reload="reload"
49 : @create-custom-deployment="createCustomDeployment"
50 : />
51 : </Menu>
52 : </div>
53 : </template>
54 :
55 : <script setup lang="ts">
56 : import { cn } from '@comfyorg/tailwind-utils'
57 : import { useEventListener, useIntervalFn } from '@vueuse/core'
58 : import { storeToRefs } from 'pinia'
59 : import { computed, onMounted, ref, watch } from 'vue'
60 : import { useI18n } from 'vue-i18n'
61 :
62 : import Button from '@comfyorg/design-system/button/Button'
63 : import Menu from '@comfyorg/design-system/menu/Menu'
64 : import { useToast } from '@/components/ui/toast/toastStore'
65 : import { useSettingStore } from '@/platform/settings/settingStore'
66 : import { reportError } from '@comfyorg/telemetry/reportError'
67 : import {
68 : DEPLOYMENT_LISTING_FOCUS_RETRY_MS,
69 : DEPLOYMENT_START_POLL_MS
70 : } from '@/platform/workspace/api/deploymentListingTimeouts'
71 : import DeploymentStatusIcon from '@/platform/workspace/components/DeploymentStatusIcon.vue'
72 : import DeploymentSwitcherList from '@/platform/workspace/components/DeploymentSwitcherList.vue'
73 : import {
74 : switcherMenuMotionClass,
75 : switcherMenuWidthClass
76 : } from '@/platform/workspace/components/deploymentSwitcherStyles'
77 : import {
78 : COMFY_CLOUD_ROW,
79 : useDeploymentLabels
80 : } from '@/platform/workspace/composables/useDeploymentLabels'
81 : import { useDeploymentRows } from '@/platform/workspace/composables/useDeploymentRows'
82 : import { useDeploymentSwitchGuard } from '@/platform/workspace/composables/useDeploymentSwitchGuard'
83 : import {
84 : confirmUnsavedWorkBeforeSwitch,
85 : unsavedWorkflows
86 : } from '@/platform/workspace/confirmUnsavedWorkBeforeSwitch'
87 : import { openCreateCustomDeploymentDialog } from '@/platform/workspace/openCustomDeploymentDialog'
88 : import { useDeploymentSwitcherOffered } from '@/platform/workspace/composables/useDeploymentSwitcherOffered'
89 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
90 :
91 2009 : const { t } = useI18n()
92 2009 : const store = useDeploymentPickStore()
93 2009 : const {
94 : pickedDeployment,
95 : isVisible,
96 : isSwitching,
97 : listingRefused,
98 : switcherOpenAsks
99 : } = storeToRefs(store)
100 2009 : const settingStore = useSettingStore()
101 2009 : const isOffered = useDeploymentSwitcherOffered()
102 2009 : const { currentLabel } = useDeploymentLabels()
103 2009 : const { targetLabel, withoutLeavePrompt, reloadOntoCurrent } =
104 : useDeploymentSwitchGuard()
105 2009 : const { pickedRow, isAnyStarting } = useDeploymentRows()
106 :
107 2009 : const isOpen = ref(false)
108 2009 : const switchingTo = ref<string | null>(null)
109 :
110 2009 : watch(switcherOpenAsks, () => {
111 0 : if (!isOffered.value || !isVisible.value) return
112 0 : isOpen.value = true
113 : })
114 :
115 2009 : let pendingRetry: Promise<void> | undefined
116 2009 : let lastFocusRetryAt = -Infinity
117 :
118 2009 : onMounted(() => {
119 2009 : if (!isOffered.value) return
120 0 : pendingRetry = store.loadOnce().finally(() => {
121 0 : pendingRetry = undefined
122 : })
123 : })
124 :
125 2009 : useEventListener(window, 'focus', () => {
126 0 : if (pendingRetry || !isOffered.value) return
127 0 : if (isVisible.value || listingRefused.value) return
128 0 : if (Date.now() - lastFocusRetryAt < DEPLOYMENT_LISTING_FOCUS_RETRY_MS) return
129 0 : lastFocusRetryAt = Date.now()
130 0 : pendingRetry = store.load().finally(() => {
131 0 : pendingRetry = undefined
132 : })
133 : })
134 :
135 2009 : const markedUntilClose = ref(false)
136 2009 : const isNew = computed(
137 : () =>
138 : markedUntilClose.value || !settingStore.get('Comfy.DeploymentSwitcher.Seen')
139 : )
140 :
141 0 : function rememberSeen() {
142 0 : markedUntilClose.value = true
143 0 : settingStore
144 : .set('Comfy.DeploymentSwitcher.Seen', true)
145 : .catch((error: unknown) =>
146 : reportError(error, {
147 : errorType: 'error_saving_deployment_switcher_seen',
148 : surface: 'platform'
149 : })
150 : )
151 : }
152 :
153 2009 : watch(isOpen, (open) => {
154 0 : if (!open) {
155 0 : markedUntilClose.value = false
156 0 : return
157 : }
158 0 : void store.load()
159 0 : if (!settingStore.get('Comfy.DeploymentSwitcher.Seen')) rememberSeen()
160 : })
161 :
162 2009 : let pendingPoll: Promise<void> | undefined
163 2009 : const startPoll = useIntervalFn(
164 : () => {
165 0 : if (pendingPoll) return
166 0 : pendingPoll = store.load().finally(() => {
167 0 : pendingPoll = undefined
168 : })
169 : },
170 : DEPLOYMENT_START_POLL_MS,
171 : { immediate: false }
172 : )
173 2009 : watch(
174 : () => isOpen.value && isAnyStarting.value,
175 : (polling) => (polling ? startPoll.resume() : startPoll.pause())
176 : )
177 :
178 2009 : const currentTone = computed(() => pickedRow.value?.tone)
179 2009 : const isReady = computed(
180 : () => currentTone.value === undefined || currentTone.value === 'ready'
181 : )
182 2009 : const statusLabel = computed(() =>
183 : isReady.value
184 : ? t(
185 : 'deploymentSwitcher.triggerLabel',
186 : { deployment: currentLabel.value },
187 : { escapeParameter: false }
188 : )
189 : : t(
190 : 'deploymentSwitcher.triggerLabelNotReady',
191 : { status: hoverTooltip.value },
192 : { escapeParameter: false }
193 : )
194 : )
195 2009 : const triggerLabel = computed(() =>
196 : isNew.value
197 : ? t(
198 : 'deploymentSwitcher.triggerLabelNew',
199 : { label: statusLabel.value },
200 : { escapeParameter: false }
201 : )
202 : : statusLabel.value
203 : )
204 0 : function tooltipKey(): string {
205 0 : if (isReady.value) return 'deploymentSwitcher.currentTooltip'
206 0 : const action = pickedRow.value?.action
207 0 : if (action === 'start' || action === 'retry') {
208 0 : return currentTone.value === 'failed'
209 : ? 'deploymentSwitcher.failedStartTooltip'
210 : : 'deploymentSwitcher.stoppedStartTooltip'
211 : }
212 0 : return currentTone.value === 'stopped' &&
213 : pickedDeployment.value?.status === 'stopped'
214 : ? 'deploymentSwitcher.stoppedTooltip'
215 : : 'deploymentSwitcher.notReadyTooltip'
216 : }
217 2009 : const hoverTooltip = computed(() =>
218 : t(
219 : tooltipKey(),
220 : { deployment: currentLabel.value },
221 : { escapeParameter: false }
222 : )
223 : )
224 :
225 0 : function report(summary: string, refusal: string | null) {
226 0 : if (refusal === null) return
227 0 : switchingTo.value = null
228 0 : isOpen.value = false
229 0 : useToast().error(summary, { description: refusal, duration: 8000 })
230 : }
231 :
232 0 : async function confirmSwitchTo(deployment: string): Promise<boolean> {
233 0 : if (unsavedWorkflows().length > 0) isOpen.value = false
234 0 : return confirmUnsavedWorkBeforeSwitch(deployment)
235 : }
236 :
237 0 : async function choose(deploymentId: string | null) {
238 0 : if (!(await confirmSwitchTo(targetLabel(deploymentId)))) return
239 0 : switchingTo.value = deploymentId ?? COMFY_CLOUD_ROW
240 0 : report(
241 : t('deploymentSwitcher.failedToSwitch'),
242 : await withoutLeavePrompt(() => store.pick(deploymentId))
243 : )
244 : }
245 :
246 0 : async function follow(deploymentId: string) {
247 : const reloads = deploymentId !== pickedDeployment.value?.deployment_id
248 0 : if (reloads && !(await confirmSwitchTo(targetLabel(deploymentId)))) return
249 0 : switchingTo.value = deploymentId
250 0 : report(
251 : t('deploymentSwitcher.failedToSwitch'),
252 : await withoutLeavePrompt(() => store.followWorkspace())
253 : )
254 : }
255 :
256 0 : async function changeDefault(deploymentId: string | null) {
257 : const reloads = store.defaultMovesThisBrowser(deploymentId)
258 0 : if (reloads && !(await confirmSwitchTo(targetLabel(deploymentId)))) return
259 0 : switchingTo.value = null
260 0 : report(
261 : t('deploymentSwitcher.failedToSetDefault'),
262 : await withoutLeavePrompt(() => store.setDefault(deploymentId))
263 : )
264 : }
265 :
266 0 : function createCustomDeployment() {
267 0 : void openCreateCustomDeploymentDialog()
268 : }
269 :
270 0 : function reload() {
271 0 : return reloadOntoCurrent(confirmSwitchTo)
272 : }
273 : </script>
|