Line data Source code
1 : <template>
2 0 : <div
3 : data-testid="asset-card"
4 : data-component-id="AssetCard"
5 : :data-asset-id="asset.id"
6 : :aria-labelledby="titleId"
7 : :aria-describedby="descId"
8 : :tabindex="interactive ? 0 : -1"
9 : :class="
10 : cn(
11 : 'flex h-full flex-col gap-2 overflow-hidden rounded-2xl bg-secondary p-2 transition-all duration-200 select-none',
12 : interactive &&
13 : 'group m-0 appearance-none border-none bg-transparent text-left outline-4 outline-foreground outline-none hover:bg-secondary focus:bg-secondary focus:outline-solid',
14 : focused && 'bg-secondary outline-solid'
15 : )
16 : "
17 : @click.stop="interactive && $emit('focus', asset)"
18 : @focus="interactive && $emit('focus', asset)"
19 : @keydown.enter.self="interactive && $emit('select', asset)"
20 : >
21 : <div class="relative aspect-square w-full overflow-hidden rounded-xl">
22 : <div
23 : v-if="isLoading || error"
24 : class="flex size-full cursor-pointer items-center justify-center bg-linear-to-br from-smoke-400 via-smoke-800 to-charcoal-400"
25 : />
26 : <img
27 : v-else
28 : :src="asset.preview_url"
29 : :alt="displayName"
30 : class="size-full cursor-pointer object-cover"
31 : />
32 :
33 : <AssetBadgeGroup :badges="badges" />
34 : <IconGroup
35 : :class="
36 : cn(
37 : 'invisible absolute top-2 right-2 group-hover:visible',
38 : assetMenuOpen && 'visible'
39 : )
40 : "
41 : >
42 : <Button
43 : :tooltip="$t('assetBrowser.modelInfo.title')"
44 : tooltip-side="bottom"
45 : :aria-label="$t('assetBrowser.modelInfo.title')"
46 : variant="secondary"
47 : size="sm"
48 : @click.stop="$emit('showInfo', asset)"
49 : >
50 : <i class="icon-[lucide--info]" />
51 : </Button>
52 : <Menu
53 : v-if="showAssetOptions"
54 : v-model:open="assetMenuOpen"
55 : :items="assetMenuItems"
56 : align="end"
57 : >
58 : <template #trigger>
59 : <Button
60 : size="sm"
61 : variant="secondary"
62 : icon="icon-[lucide--ellipsis]"
63 : :aria-label="$t('g.more')"
64 : />
65 : </template>
66 : </Menu>
67 : </IconGroup>
68 : </div>
69 : <div class="flex max-h-32 flex-auto flex-col justify-between gap-2">
70 : <Tooltip :disabled="!displayName" :delay-duration="tooltipDelay">
71 : <TooltipTrigger as-child>
72 : <h3
73 : :id="titleId"
74 : :class="
75 : cn(
76 : 'm-0 line-clamp-2 text-sm font-semibold wrap-anywhere',
77 : 'text-foreground'
78 : )
79 : "
80 : >
81 : {{ displayName }}
82 : </h3>
83 : </TooltipTrigger>
84 : <TooltipContent>{{ displayName }}</TooltipContent>
85 : </Tooltip>
86 : <Tooltip :disabled="!asset.secondaryText" :delay-duration="tooltipDelay">
87 : <TooltipTrigger as-child>
88 : <p
89 : :id="descId"
90 : :class="
91 : cn(
92 : 'm-0 line-clamp-2 [display:-webkit-box] text-sm text-muted-foreground [-webkit-box-orient:vertical] [-webkit-line-clamp:2]'
93 : )
94 : "
95 : >
96 : {{ asset.secondaryText }}
97 : </p>
98 : </TooltipTrigger>
99 : <TooltipContent>{{ asset.secondaryText }}</TooltipContent>
100 : </Tooltip>
101 : <div class="mt-auto flex items-center justify-between gap-2">
102 : <div class="flex gap-3 text-xs text-muted-foreground">
103 : <span v-if="asset.stats.stars" class="flex items-center gap-1">
104 : <i class="icon-[lucide--star] size-3" />
105 : {{ asset.stats.stars }}
106 : </span>
107 : <span
108 : v-if="asset.stats.downloadCount"
109 : class="flex items-center gap-1"
110 : >
111 : <i class="icon-[lucide--download] size-3" />
112 : {{ asset.stats.downloadCount }}
113 : </span>
114 : <span v-if="formattedDate" class="flex items-center gap-1">
115 : <i class="icon-[lucide--clock] size-3" />
116 : {{ formattedDate }}
117 : </span>
118 : </div>
119 : <Button
120 : v-if="interactive"
121 : variant="secondary"
122 : size="lg"
123 : class="shrink-0"
124 : @click.stop="handleSelect"
125 : >
126 : {{ $t('g.use') }}
127 : <Badge
128 : v-if="isNewlyImported"
129 : variant="dot"
130 : severity="contrast"
131 : class="absolute -top-0.5 -right-0.5"
132 : />
133 : </Button>
134 : </div>
135 : </div>
136 : </div>
137 : </template>
138 :
139 : <script setup lang="ts">
140 : import { computed, ref, toValue, useId } from 'vue'
141 : import { useI18n } from 'vue-i18n'
142 :
143 : import { useImageQuiet } from '@/composables/useImageQuiet'
144 : import IconGroup from '@comfyorg/design-system/button-group/IconGroup'
145 : import Badge from '@comfyorg/design-system/badge/Badge'
146 : import { showConfirmDialog } from '@/components/dialog/confirm/confirmDialog'
147 : import Button from '@comfyorg/design-system/button/Button'
148 : import Menu from '@comfyorg/design-system/menu/Menu'
149 : import type { MenuItem } from '@comfyorg/design-system/lib/menu/types'
150 : import Tooltip from '@comfyorg/design-system/tooltip/Tooltip'
151 : import TooltipContent from '@comfyorg/design-system/tooltip/TooltipContent'
152 : import TooltipTrigger from '@comfyorg/design-system/tooltip/TooltipTrigger'
153 : import AssetBadgeGroup from '@/platform/assets/components/AssetBadgeGroup.vue'
154 : import type { AssetDisplayItem } from '@/platform/assets/composables/useAssetBrowser'
155 : import { assetService } from '@/platform/assets/services/assetService'
156 : import { getAssetCardTitle } from '@/platform/assets/utils/assetMetadataUtils'
157 : import { useSettingStore } from '@/platform/settings/settingStore'
158 : import { useAssetDownloadStore } from '@/stores/assetDownloadStore'
159 : import { useDialogStore } from '@/stores/dialogStore'
160 : import { cn } from '@comfyorg/tailwind-utils'
161 :
162 : const { asset, interactive, focused } = defineProps<{
163 : asset: AssetDisplayItem
164 : interactive?: boolean
165 : focused?: boolean
166 : }>()
167 :
168 0 : const emit = defineEmits<{
169 : focus: [asset: AssetDisplayItem]
170 : select: [asset: AssetDisplayItem]
171 : deleted: [asset: AssetDisplayItem]
172 : showInfo: [asset: AssetDisplayItem]
173 : }>()
174 :
175 0 : const { t, d } = useI18n()
176 0 : const settingStore = useSettingStore()
177 0 : const { closeDialog } = useDialogStore()
178 0 : const { isDownloadedThisSession, acknowledgeAsset } = useAssetDownloadStore()
179 :
180 0 : const badges = computed<AssetDisplayItem['badges']>(() =>
181 : asset.isWorkspaceLibrary
182 : ? [{ label: t('assetBrowser.library'), type: 'type' }, ...asset.badges]
183 : : asset.badges
184 : )
185 :
186 0 : const assetMenuOpen = ref(false)
187 :
188 0 : const titleId = useId()
189 0 : const descId = useId()
190 :
191 0 : const displayName = computed(() => getAssetCardTitle(asset))
192 :
193 : // Format at render so locale switches re-flow; the upstream WeakMap caches
194 : // AssetItem -> AssetDisplayItem by reference, which would otherwise pin the
195 : // formatted string to whichever locale was active when first transformed.
196 0 : const formattedDate = computed(() =>
197 : asset.created_at
198 : ? d(new Date(asset.created_at), { dateStyle: 'short' })
199 : : undefined
200 : )
201 :
202 0 : const isNewlyImported = computed(() => isDownloadedThisSession(asset.id))
203 :
204 0 : const showAssetOptions = computed(() => !(asset.is_immutable ?? true))
205 :
206 0 : const tooltipDelay = computed<number>(() =>
207 : settingStore.get('LiteGraph.Node.TooltipDelay')
208 : )
209 :
210 0 : const { isLoading, error } = useImageQuiet({
211 : src: asset.preview_url ?? '',
212 : alt: displayName.value
213 : })
214 :
215 0 : function handleSelect() {
216 0 : acknowledgeAsset(asset.id)
217 0 : emit('select', asset)
218 : }
219 :
220 0 : function confirmDeletion() {
221 0 : assetMenuOpen.value = false
222 0 : const assetName = toValue(displayName)
223 0 : const promptText = ref<string>(t('assetBrowser.deletion.body'))
224 0 : const optionsDisabled = ref(false)
225 0 : const confirmDialog = showConfirmDialog({
226 : headerProps: {
227 : title: t('assetBrowser.deletion.header')
228 : },
229 : props: {
230 : promptText
231 : },
232 : footerProps: {
233 : cancelText: t('g.cancel'),
234 : confirmText: t('g.delete'),
235 : confirmClass: cn(
236 : 'bg-destructive ring-foreground hover:bg-destructive/80 focus:bg-destructive/80 focus:ring'
237 : ),
238 : optionsDisabled,
239 : onCancel: () => {
240 0 : closeDialog(confirmDialog)
241 : },
242 : onConfirm: async () => {
243 0 : optionsDisabled.value = true
244 0 : try {
245 0 : promptText.value = t('assetBrowser.deletion.inProgress', {
246 : assetName
247 : })
248 0 : await assetService.deleteAsset(asset.id)
249 0 : promptText.value = t('assetBrowser.deletion.complete', {
250 : assetName
251 : })
252 0 : await new Promise((resolve) => setTimeout(resolve, 1_000))
253 0 : emit('deleted', asset)
254 : } catch (err: unknown) {
255 0 : console.error(err)
256 0 : promptText.value = t('assetBrowser.deletion.failed', {
257 : assetName
258 : })
259 0 : await new Promise((resolve) => setTimeout(resolve, 3_000))
260 : } finally {
261 0 : closeDialog(confirmDialog)
262 : }
263 : }
264 : }
265 : })
266 : }
267 :
268 0 : const assetMenuItems = computed<MenuItem[]>(() => [
269 : {
270 : label: () => t('g.delete'),
271 : icon: 'icon-[lucide--trash-2]',
272 : command: confirmDeletion
273 : }
274 : ])
275 : </script>
|