Line data Source code
1 : import { computed, toValue, watch } from 'vue'
2 : import type { MaybeRefOrGetter } from 'vue'
3 :
4 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
5 : import { isCloud } from '@/platform/distribution/types'
6 : import { useAssetsStore } from '@/stores/assetsStore'
7 : import { useModelToNodeStore } from '@/stores/modelToNodeStore'
8 :
9 : /**
10 : * Composable for fetching and transforming asset data for Vue node widgets.
11 : * Provides reactive asset data based on node type with automatic category detection.
12 : * Uses store-based caching to avoid duplicate fetches across multiple instances.
13 : *
14 : * Cloud-only composable - returns empty data when not in cloud environment.
15 : *
16 : * @param nodeType - ComfyUI node type (ref, getter, or plain value). Can be undefined.
17 : * Accepts: ref('CheckpointLoaderSimple'), () => 'CheckpointLoaderSimple', or 'CheckpointLoaderSimple'
18 : * @returns Reactive data including category, assets, dropdown items, loading state, and errors
19 : */
20 0 : export function useAssetWidgetData(
21 : nodeType: MaybeRefOrGetter<string | undefined>
22 : ) {
23 : if (!isCloud)
24 0 : return {
25 : category: computed(() => undefined),
26 : assets: computed<AssetItem[]>(() => []),
27 : isLoading: computed(() => false),
28 : error: computed(() => null)
29 : }
30 :
31 : const assetsStore = useAssetsStore()
32 : const modelToNodeStore = useModelToNodeStore()
33 :
34 : const category = computed(() => {
35 : const resolvedType = toValue(nodeType)
36 : return resolvedType && modelToNodeStore.getCategoryForNodeType(resolvedType)
37 : })
38 :
39 : const assets = computed<AssetItem[]>(() => {
40 : const resolvedType = toValue(nodeType)
41 : return resolvedType ? assetsStore.getAssets(resolvedType) : []
42 : })
43 :
44 : const isLoading = computed(() => {
45 : const resolvedType = toValue(nodeType)
46 : return resolvedType ? assetsStore.isModelLoading(resolvedType) : false
47 : })
48 :
49 : const error = computed<Error | null>(() => {
50 : const resolvedType = toValue(nodeType)
51 : return resolvedType ? (assetsStore.getError(resolvedType) ?? null) : null
52 : })
53 :
54 : watch(
55 : () => toValue(nodeType),
56 : async (currentNodeType) => {
57 : if (!currentNodeType) {
58 : return
59 : }
60 :
61 : const isLoading = assetsStore.isModelLoading(currentNodeType)
62 : const hasBeenInitialized = assetsStore.hasAssetKey(currentNodeType)
63 :
64 : if (!isLoading && !hasBeenInitialized) {
65 : await assetsStore.updateModelsForNodeType(currentNodeType)
66 : }
67 : },
68 : { immediate: true }
69 : )
70 :
71 : return { category, assets, isLoading, error }
72 : }
|