Line data Source code
1 : <script setup lang="ts">
2 : import { unrefElement, whenever } from '@vueuse/core'
3 : import { PopoverRoot, PopoverTrigger } from 'reka-ui'
4 : import PopoverContent from '@comfyorg/design-system/popover/PopoverContent'
5 : import { computed, ref, useTemplateRef } from 'vue'
6 : import { useI18n } from 'vue-i18n'
7 :
8 : import Loader from '@/components/loader/Loader.vue'
9 : import ToastPanel from '@comfyorg/design-system/toast/ToastPanel'
10 : import ProgressToastItem from '@/components/toast/ProgressToastItem.vue'
11 : import Button from '@comfyorg/design-system/button/Button'
12 : import { usePopoverViewportDismiss } from '@comfyorg/design-system/lib/popover/usePopoverViewportDismiss'
13 : import { useErrorHandling } from '@/composables/useErrorHandling'
14 : import { reportError } from '@comfyorg/telemetry/reportError'
15 : import type { TaskId } from '@/platform/tasks/services/taskService'
16 : import {
17 : isDownloadCancelled,
18 : useAssetDownloadStore
19 : } from '@/stores/assetDownloadStore'
20 : import { cn } from '@comfyorg/tailwind-utils'
21 :
22 2355 : const { t } = useI18n()
23 2355 : const assetDownloadStore = useAssetDownloadStore()
24 2355 : const { toastErrorHandler } = useErrorHandling()
25 :
26 2355 : const visible = computed(() => assetDownloadStore.hasDownloads)
27 :
28 2355 : const isExpanded = ref(false)
29 2355 : const activeFilter = ref<'all' | 'completed' | 'failed'>('all')
30 2355 : const isFilterOpen = ref(false)
31 2355 : const filterAnchor = useTemplateRef<InstanceType<typeof Button>>('filterAnchor')
32 :
33 2355 : usePopoverViewportDismiss(isFilterOpen, () => unrefElement(filterAnchor))
34 :
35 2355 : whenever(
36 : () => !isExpanded.value,
37 : () => {
38 2 : isFilterOpen.value = false
39 : }
40 : )
41 :
42 2355 : const filterOptions = [
43 : { value: 'all', label: 'all' },
44 : { value: 'completed', label: 'completed' },
45 : { value: 'failed', label: 'failed' }
46 : ] as const
47 :
48 2 : function setFilter(filter: typeof activeFilter.value) {
49 2 : activeFilter.value = filter
50 2 : isFilterOpen.value = false
51 : }
52 :
53 2355 : const downloadJobs = computed(() => assetDownloadStore.downloadList)
54 2355 : const completedJobs = computed(() =>
55 : assetDownloadStore.finishedDownloads.filter((d) => d.status === 'completed')
56 : )
57 2355 : const failedJobs = computed(() =>
58 : assetDownloadStore.finishedDownloads.filter((d) => d.status === 'failed')
59 : )
60 2355 : const cancelledJobs = computed(() =>
61 : assetDownloadStore.downloadList.filter((download) =>
62 : isDownloadCancelled(download.status)
63 : )
64 : )
65 :
66 2355 : const isInProgress = computed(() => assetDownloadStore.hasActiveDownloads)
67 2355 : const currentJobName = computed(() => {
68 : const activeJob = downloadJobs.value.find((job) => job.status === 'running')
69 5 : return activeJob?.assetName || t('progressToast.downloadingModel')
70 : })
71 :
72 2355 : const outcomeLabel = computed(() => {
73 2380 : if (failedJobs.value.length > 0)
74 5 : return t('progressToast.downloadsFailed', {
75 : count: failedJobs.value.length
76 : })
77 2380 : if (cancelledJobs.value.length > 0) return t('electronFileDownload.cancelled')
78 2373 : if (downloadJobs.value.every((job) => job.source === 'upload'))
79 2367 : return t(
80 : 'progressToast.uploadsInLibrary',
81 : {
82 : name: downloadJobs.value[0]?.assetName,
83 : count: downloadJobs.value.length
84 : },
85 : downloadJobs.value.length
86 : )
87 3 : return t('progressToast.allDownloadsCompleted')
88 : })
89 :
90 2355 : const announcement = computed(() =>
91 : isInProgress.value ? t('progressToast.importingModels') : outcomeLabel.value
92 : )
93 :
94 2355 : const completedCount = computed(
95 : () =>
96 : completedJobs.value.length +
97 : failedJobs.value.length +
98 : cancelledJobs.value.length
99 : )
100 2355 : const totalCount = computed(() => downloadJobs.value.length)
101 :
102 2355 : const filteredJobs = computed(() => {
103 16 : switch (activeFilter.value) {
104 : case 'completed':
105 0 : return completedJobs.value
106 : case 'failed':
107 2 : return failedJobs.value
108 : default:
109 14 : return downloadJobs.value
110 : }
111 : })
112 :
113 2355 : const activeFilterLabel = computed(() => {
114 10 : const option = filterOptions.find((f) => f.value === activeFilter.value)
115 10 : return option
116 : ? t(`progressToast.filter.${option.label}`)
117 : : t('progressToast.filter.all')
118 : })
119 :
120 4 : function closeDialog() {
121 4 : assetDownloadStore.clearDismissibleDownloads()
122 4 : isExpanded.value = false
123 : }
124 :
125 3 : async function cancelDownload(taskId: TaskId) {
126 3 : const result = await assetDownloadStore.cancelDownload(taskId)
127 3 : if (result.ok) return
128 :
129 0 : reportError(result.error, {
130 : surface: 'assets',
131 : errorType: 'asset_download_cancellation_failure',
132 : logToConsole: false
133 : })
134 0 : toastErrorHandler(result.error)
135 : }
136 2355 : </script>
137 :
138 : <template>
139 2388 : <ToastPanel v-model:expanded="isExpanded" :visible :announcement>
140 : <template #default>
141 : <div class="flex h-12 items-center justify-between border-b px-4">
142 : <h3 class="text-sm font-bold">
143 : {{ t('progressToast.importingModels') }}
144 : </h3>
145 : <div class="flex items-center gap-2">
146 : <PopoverRoot v-model:open="isFilterOpen">
147 : <PopoverTrigger as-child>
148 : <Button
149 : ref="filterAnchor"
150 : variant="secondary"
151 : size="md"
152 : class="gap-1.5 px-2"
153 : >
154 : <i class="icon-[lucide--list-filter]" />
155 : <span>{{ activeFilterLabel }}</span>
156 : <i class="icon-[lucide--chevron-down] size-3" />
157 : </Button>
158 : </PopoverTrigger>
159 : <PopoverContent
160 : align="end"
161 : class="w-auto rounded-lg border-none bg-transparent p-0 pt-2 shadow-lg data-[state=closed]:animate-none data-[state=open]:animate-none"
162 : >
163 : <div
164 : class="flex min-w-30 flex-col rounded-lg border bg-background px-2 py-3"
165 : >
166 36 : <Button
167 : v-for="option in filterOptions"
168 : :key="option.value"
169 : variant="textonly"
170 : size="sm"
171 : :class="
172 : cn(
173 : 'w-full justify-start',
174 : activeFilter === option.value && 'bg-muted'
175 : )
176 : "
177 : @click="setFilter(option.value)"
178 : >
179 : {{ t(`progressToast.filter.${option.label}`) }}
180 : </Button>
181 : </div>
182 : </PopoverContent>
183 : </PopoverRoot>
184 : </div>
185 : </div>
186 :
187 : <div class="relative max-h-75 overflow-y-auto p-4">
188 : <div
189 : v-if="filteredJobs.length > 3"
190 : class="absolute top-4 right-1 h-12 w-1 rounded-full bg-muted-foreground"
191 : />
192 :
193 : <div class="flex flex-col gap-2">
194 36 : <ProgressToastItem
195 : v-for="job in filteredJobs"
196 : :key="job.taskId"
197 : :job="job"
198 : :is-cancelling="
199 : assetDownloadStore.cancellingTaskIds.has(job.taskId)
200 : "
201 : @cancel="cancelDownload"
202 : />
203 : </div>
204 :
205 : <div
206 : v-if="filteredJobs.length === 0"
207 : class="flex flex-col items-center justify-center py-6 text-center"
208 : >
209 : <span class="text-sm text-muted-foreground">
210 : {{
211 : t('progressToast.noImportsInQueue', {
212 : filter: activeFilterLabel
213 : })
214 : }}
215 : </span>
216 : </div>
217 : </div>
218 : </template>
219 :
220 : <template #footer="{ toggle }">
221 : <div class="flex h-12 items-center justify-between gap-2 border-t px-4">
222 : <div class="flex min-w-0 flex-1 items-center gap-2 text-sm">
223 : <template v-if="isInProgress">
224 : <Loader size="sm" class="shrink-0 text-muted-foreground" />
225 : <span class="min-w-0 flex-1 truncate font-bold">
226 : {{ currentJobName }}
227 : </span>
228 : </template>
229 : <template v-else-if="failedJobs.length > 0">
230 : <i class="icon-[lucide--circle-alert] text-destructive" />
231 : <span class="min-w-0 truncate font-bold">
232 : {{ outcomeLabel }}
233 : </span>
234 : </template>
235 : <template v-else-if="cancelledJobs.length > 0">
236 : <i class="icon-[lucide--circle-x] text-muted-foreground" />
237 : <span class="min-w-0 truncate font-bold">
238 : {{ outcomeLabel }}
239 : </span>
240 : </template>
241 : <template v-else>
242 : <i class="icon-[lucide--check-circle] text-success" />
243 : <span class="min-w-0 truncate font-bold">
244 : {{ outcomeLabel }}
245 : </span>
246 : </template>
247 : </div>
248 :
249 : <div class="flex shrink-0 items-center gap-2">
250 : <span
251 : v-if="isInProgress"
252 : class="text-sm whitespace-nowrap text-muted-foreground"
253 : >
254 : {{
255 : t('progressToast.progressCount', {
256 : completed: completedCount,
257 : total: totalCount
258 : })
259 : }}
260 : </span>
261 :
262 : <div class="flex items-center">
263 : <Button
264 : variant="muted-textonly"
265 : size="icon"
266 : :aria-label="
267 : isExpanded ? t('contextMenu.Collapse') : t('contextMenu.Expand')
268 : "
269 : @click.stop="toggle"
270 : >
271 : <i
272 : :class="
273 : isExpanded
274 : ? 'icon-[lucide--chevron-down]'
275 : : 'icon-[lucide--chevron-up]'
276 : "
277 : />
278 : </Button>
279 :
280 : <Button
281 : v-if="!isInProgress"
282 : variant="muted-textonly"
283 : size="icon"
284 : :aria-label="t('g.close')"
285 : @click.stop="closeDialog"
286 : >
287 : <i class="icon-[lucide--x]" />
288 : </Button>
289 : </div>
290 : </div>
291 : </div>
292 : </template>
293 : </ToastPanel>
294 : </template>
|