Line data Source code
1 : <script setup lang="ts">
2 : import { cn } from '@comfyorg/tailwind-utils'
3 : import { useScroll, whenever } from '@vueuse/core'
4 : import type { ComponentPublicInstance } from 'vue'
5 : import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
6 : import { useI18n } from 'vue-i18n'
7 :
8 : import DotSpinner from '@comfyorg/design-system/common/DotSpinner'
9 : import ToastPanel from '@comfyorg/design-system/toast/ToastPanel'
10 : import Button from '@comfyorg/design-system/button/Button'
11 : import Tabs from '@comfyorg/design-system/tabs/Tabs'
12 : import TabsContent from '@comfyorg/design-system/tabs/TabsContent'
13 : import TabsList from '@comfyorg/design-system/tabs/TabsList'
14 : import TabsTrigger from '@comfyorg/design-system/tabs/TabsTrigger'
15 : import { useApplyChanges } from '@/platform/customNodes/manager/composables/useApplyChanges'
16 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
17 :
18 2355 : const { t } = useI18n()
19 2355 : const comfyManagerStore = useComfyManagerStore()
20 2355 : const { isRestarting, isRestartCompleted, applyChanges } = useApplyChanges()
21 :
22 2355 : const isExpanded = ref(false)
23 2355 : const activeTab = ref('installation')
24 :
25 2355 : const tabs = computed(() => [
26 : {
27 : value: 'installation',
28 : label: t('manager.installationQueue'),
29 : logs: comfyManagerStore.succeededTasksLogs
30 : },
31 : {
32 : value: 'failed',
33 : label: t('manager.failed', {
34 : count: comfyManagerStore.failedTasksIds.length
35 : }),
36 : logs: comfyManagerStore.failedTasksLogs
37 : }
38 : ])
39 :
40 2355 : const visible = computed(() => comfyManagerStore.taskLogs.length > 0)
41 :
42 2355 : const isInProgress = computed(
43 : () => comfyManagerStore.isProcessingTasks || isRestarting.value
44 : )
45 2355 : const hasSuccessfulTasks = computed(
46 : () => comfyManagerStore.succeededTasksIds.length > 0
47 : )
48 :
49 2355 : const completedTasksCount = computed(() => {
50 1 : return (
51 : comfyManagerStore.succeededTasksIds.length +
52 : comfyManagerStore.failedTasksIds.length
53 : )
54 : })
55 :
56 2355 : const totalTasksCount = computed(() => {
57 1 : const completedTasks = Object.keys(comfyManagerStore.taskHistory).length
58 1 : const taskQueue = comfyManagerStore.taskQueue
59 : const queuedTasks = taskQueue
60 : ? (taskQueue.running_queue?.length || 0) +
61 : (taskQueue.pending_queue?.length || 0)
62 : : 0
63 : return completedTasks + queuedTasks
64 : })
65 :
66 2355 : const currentTaskName = computed(() => {
67 1 : if (isRestarting.value) {
68 0 : return t('manager.restartingBackend')
69 : }
70 1 : if (isRestartCompleted.value) {
71 0 : return t('manager.extensionsSuccessfullyInstalled')
72 : }
73 1 : if (!comfyManagerStore.taskLogs.length)
74 0 : return t('manager.installingDependencies')
75 : const task = comfyManagerStore.taskLogs.at(-1)
76 1 : return task?.taskName ?? t('manager.installingDependencies')
77 : })
78 :
79 2355 : const announcement = computed(() => {
80 2365 : if (comfyManagerStore.queueError && isInProgress.value)
81 0 : return t('manager.queueWaitingToContinue')
82 2365 : if (isRestarting.value || isRestartCompleted.value)
83 0 : return currentTaskName.value
84 2365 : if (isInProgress.value) return t('manager.installingDependencies')
85 2365 : if (hasSuccessfulTasks.value) return t('manager.restartToApplyChanges')
86 2365 : if (comfyManagerStore.failedTasksIds.length) return t('g.failed')
87 2363 : return t('g.completed')
88 : })
89 :
90 2355 : const sectionsContainerRef = ref<HTMLElement | null>(null)
91 2355 : const { y: scrollY } = useScroll(sectionsContainerRef, {
92 : eventListenerOptions: { passive: true }
93 : })
94 :
95 2355 : const latestLogContainerRef = ref<HTMLElement | null>(null)
96 2355 : const isUserScrolling = ref(false)
97 2355 : const latestTaskLogLines = computed(
98 : () =>
99 : tabs.value.find((tab) => tab.value === activeTab.value)?.logs.at(-1)?.logs
100 : )
101 :
102 0 : function setSectionsContainer(el: Element | ComponentPublicInstance | null) {
103 0 : sectionsContainerRef.value = el instanceof HTMLElement ? el : null
104 : }
105 :
106 0 : function setLatestLogContainer(el: Element | ComponentPublicInstance | null) {
107 0 : latestLogContainerRef.value = el instanceof HTMLElement ? el : null
108 : }
109 :
110 0 : function isAtBottom(el: HTMLElement | null) {
111 0 : if (!el) return false
112 : const threshold = 20
113 0 : return Math.abs(el.scrollHeight - el.scrollTop - el.clientHeight) < threshold
114 : }
115 :
116 1 : function scrollLatestLogToBottom() {
117 1 : if (!latestLogContainerRef.value || isUserScrolling.value) return
118 0 : latestLogContainerRef.value.scrollTop =
119 : latestLogContainerRef.value.scrollHeight
120 : }
121 :
122 2355 : function scrollContentToBottom() {
123 2355 : scrollY.value = sectionsContainerRef.value?.scrollHeight ?? 0
124 : }
125 :
126 0 : function resetUserScrolling() {
127 0 : isUserScrolling.value = false
128 : }
129 :
130 0 : function handleScroll(e: Event) {
131 0 : if (!(e.target instanceof HTMLElement)) return
132 0 : const target = e.target
133 0 : if (target !== latestLogContainerRef.value) return
134 0 : isUserScrolling.value = !isAtBottom(target)
135 : }
136 :
137 1 : function onLogsAdded() {
138 1 : if (isUserScrolling.value) return
139 1 : scrollLatestLogToBottom()
140 : }
141 :
142 2355 : whenever(latestTaskLogLines, onLogsAdded, { flush: 'post', deep: true })
143 2355 : whenever(
144 : () => isExpanded.value,
145 : () => {
146 0 : scrollContentToBottom()
147 0 : scrollLatestLogToBottom()
148 : },
149 : { flush: 'post' }
150 : )
151 2355 : whenever(() => !isExpanded.value, resetUserScrolling)
152 :
153 0 : function closeToast() {
154 0 : comfyManagerStore.resetTaskState()
155 0 : isRestartCompleted.value = false
156 0 : isExpanded.value = false
157 : }
158 :
159 0 : async function handleRestart() {
160 0 : try {
161 0 : await applyChanges(closeToast)
162 : } catch (err) {
163 0 : console.error('[ManagerProgressToast] Restart failed:', err)
164 : }
165 : }
166 :
167 2355 : onMounted(() => {
168 2355 : scrollContentToBottom()
169 : })
170 :
171 2355 : onBeforeUnmount(() => {
172 0 : isExpanded.value = false
173 : })
174 2355 : </script>
175 :
176 : <template>
177 2365 : <ToastPanel v-model:expanded="isExpanded" :visible :announcement>
178 : <template #default>
179 : <Tabs
180 : v-if="isExpanded"
181 : v-model="activeTab"
182 : class="gap-0"
183 : orientation="horizontal"
184 : >
185 : <div class="flex items-center px-4 py-2">
186 : <TabsList variant="flush" class="flex w-full">
187 0 : <TabsTrigger
188 : v-for="tab in tabs"
189 : :key="tab.value"
190 : :value="tab.value"
191 : variant="flush"
192 : >
193 : {{ tab.label }}
194 : </TabsTrigger>
195 : </TabsList>
196 : </div>
197 :
198 0 : <TabsContent
199 : v-for="tab in tabs"
200 : :key="tab.value"
201 : :value="tab.value"
202 : class="mt-0"
203 : >
204 : <div
205 : :ref="setSectionsContainer"
206 : role="region"
207 : :aria-label="tab.label"
208 : class="scroll-container max-h-[450px] overflow-y-auto px-6 py-4"
209 : :style="{
210 : scrollbarWidth: 'thin',
211 : scrollbarColor: 'rgba(156, 163, 175, 0.5) transparent'
212 : }"
213 : >
214 0 : <details
215 : v-for="(log, index) in tab.logs"
216 : :key="log.taskId"
217 : open
218 : class="group/log mt-2 rounded-lg border bg-background shadow-md"
219 : >
220 : <summary
221 : class="flex w-full cursor-pointer list-none items-center justify-between px-4 py-2 [&::-webkit-details-marker]:hidden"
222 : >
223 : <span class="flex flex-col text-sm/normal font-medium">
224 : <span>{{ log.taskName }}</span>
225 : <span class="text-muted-foreground">
226 : {{
227 : comfyManagerStore.isTaskFailed(log.taskId)
228 : ? t('g.failed')
229 : : comfyManagerStore.isTaskInProgress(log.taskId)
230 : ? t('g.inProgress')
231 : : t('g.completedWithCheckmark')
232 : }}
233 : </span>
234 : </span>
235 : <i
236 : aria-hidden="true"
237 : class="icon-[lucide--chevron-right] text-neutral-300 group-open/log:rotate-90"
238 : />
239 : </summary>
240 : <div
241 : :ref="
242 : index === tab.logs.length - 1
243 : ? setLatestLogContainer
244 : : undefined
245 : "
246 : role="log"
247 : :aria-label="log.taskName"
248 : :class="
249 : cn(
250 : 'h-64 overflow-y-auto rounded-lg bg-black',
251 : index === tab.logs.length - 1 && 'grow'
252 : )
253 : "
254 : @scroll="handleScroll"
255 : >
256 : <div class="h-full">
257 0 : <div
258 : v-for="(logLine, logIndex) in log.logs"
259 : :key="logIndex"
260 : class="text-muted-foreground"
261 : >
262 : <pre class="wrap-break-word whitespace-pre-wrap">{{
263 : logLine
264 : }}</pre>
265 : </div>
266 : </div>
267 : </div>
268 : </details>
269 : </div>
270 : </TabsContent>
271 : </Tabs>
272 : </template>
273 :
274 : <template #footer="{ toggle }">
275 : <div
276 : class="flex w-full items-center justify-between gap-4 px-6 py-2 shadow-lg"
277 : >
278 : <div class="flex min-w-0 items-center text-base leading-none">
279 : <div class="flex items-center">
280 : <span v-if="comfyManagerStore.queueError && isInProgress">
281 : {{ t('manager.queueWaitingToContinue') }}
282 : </span>
283 : <template v-else-if="isInProgress">
284 : <DotSpinner duration="1s" class="mr-2" />
285 : <span>{{ currentTaskName }}</span>
286 : </template>
287 : <template v-else-if="isRestartCompleted">
288 : <span class="mr-2">🎉</span>
289 : <span>{{ currentTaskName }}</span>
290 : </template>
291 : <template v-else-if="hasSuccessfulTasks">
292 : <span class="mr-2">✅</span>
293 : <span>{{ t('manager.restartToApplyChanges') }}</span>
294 : </template>
295 : <span
296 : v-else-if="comfyManagerStore.failedTasksIds.length"
297 : class="text-destructive"
298 : >{{ t('g.failed') }}</span
299 : >
300 : <span v-else>{{ t('g.completed') }}</span>
301 : </div>
302 : </div>
303 : <div class="flex shrink-0 items-center gap-4">
304 : <span
305 : v-if="isInProgress && !comfyManagerStore.queueError"
306 : class="text-sm text-muted-foreground"
307 : >
308 : {{ completedTasksCount }} {{ t('g.progressCountOf') }}
309 : {{ totalTasksCount }}
310 : </span>
311 : <div class="flex items-center">
312 : <Button
313 : v-if="comfyManagerStore.queueError && isInProgress"
314 : variant="secondary"
315 : @click="comfyManagerStore.startQueue"
316 : >
317 : {{ t('manager.retryQueueStart') }}
318 : </Button>
319 : <Button
320 : v-if="!isInProgress && !isRestartCompleted && hasSuccessfulTasks"
321 : variant="secondary"
322 : class="mr-4 rounded-full border-2 border-foreground px-3"
323 : @click="handleRestart"
324 : >
325 : {{ t('manager.applyChanges') }}
326 : </Button>
327 : <Button
328 : v-if="!isRestartCompleted"
329 : variant="muted-textonly"
330 : size="sm"
331 : class="rounded-full font-bold"
332 : :aria-label="
333 : t(isExpanded ? 'contextMenu.Collapse' : 'contextMenu.Expand')
334 : "
335 : @click.stop="toggle"
336 : >
337 : <i
338 : :class="
339 : isExpanded
340 : ? 'icon-[lucide--chevron-up]'
341 : : 'icon-[lucide--chevron-down]'
342 : "
343 : />
344 : </Button>
345 : <Button
346 : variant="muted-textonly"
347 : size="sm"
348 : class="rounded-full font-bold"
349 : :aria-label="t('g.close')"
350 : :disabled="isInProgress"
351 : @click.stop="closeToast"
352 : >
353 : <i class="icon-[lucide--x]" />
354 : </Button>
355 : </div>
356 : </div>
357 : </div>
358 : </template>
359 : </ToastPanel>
360 : </template>
|