Line data Source code
1 : import { whenever } from '@vueuse/core'
2 : import { orderBy } from 'es-toolkit/compat'
3 : import { computed, toValue } from 'vue'
4 : import type { MaybeRefOrGetter } from 'vue'
5 :
6 : import type { components } from '@/types/comfyRegistryTypes'
7 : import { useInstalledPacks } from '@/platform/customNodes/manager/composables/nodePack/useInstalledPacks'
8 : import { useWorkflowPacks } from '@/platform/customNodes/manager/composables/nodePack/useWorkflowPacks'
9 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
10 : import { useConflictDetectionStore } from '@/platform/customNodes/manager/stores/conflictDetectionStore'
11 : import { ManagerTab } from '@/platform/customNodes/manager/types/comfyManagerTypes'
12 : import {
13 : PACK_SORTABLE_FIELDS,
14 : getPackSortValue
15 : } from '@/platform/customNodes/manager/utils/nodePackSort'
16 : import { getPackUpdateStatus } from '@/platform/customNodes/manager/composables/nodePack/packUpdateStatus'
17 :
18 : type NodePack = components['schemas']['Node']
19 :
20 15 : export function useManagerDisplayPacks(
21 : selectedTabId: MaybeRefOrGetter<string | null>,
22 : searchResults: MaybeRefOrGetter<NodePack[]>,
23 : searchQuery: MaybeRefOrGetter<string>,
24 : sortField: MaybeRefOrGetter<string>
25 : ) {
26 15 : const comfyManagerStore = useComfyManagerStore()
27 15 : const conflictDetectionStore = useConflictDetectionStore()
28 :
29 15 : const {
30 : startFetchInstalled,
31 : filterInstalledPack,
32 : installedPacks,
33 : isLoading: isLoadingInstalled,
34 : isReady: installedPacksReady
35 : } = useInstalledPacks()
36 :
37 15 : const {
38 : startFetchWorkflowPacks,
39 : filterWorkflowPack,
40 : workflowPacks,
41 : isLoading: isLoadingWorkflow,
42 : isReady: workflowPacksReady
43 : } = useWorkflowPacks()
44 :
45 15 : const tabType = computed(() => toValue(selectedTabId) as ManagerTab | null)
46 15 : const isEmptySearch = computed(() => toValue(searchQuery) === '')
47 :
48 15 : const sortPacks = (packs: NodePack[]) => {
49 1 : const field = toValue(sortField)
50 1 : if (!field || packs.length === 0) return packs
51 :
52 : const fieldConfig = PACK_SORTABLE_FIELDS.find((f) => f.id === field)
53 1 : const direction = fieldConfig?.direction || 'desc'
54 :
55 1 : return orderBy(
56 : packs,
57 : [(pack) => getPackSortValue(pack, field)],
58 : [direction]
59 : )
60 : }
61 :
62 : // Filter functions
63 15 : const filterNotInstalled = (packs: NodePack[]) =>
64 : packs.filter((p) => !comfyManagerStore.isPackInstalled(p.id))
65 :
66 15 : const filterConflicting = (packs: NodePack[]) =>
67 : packs.filter(
68 : (p) =>
69 : !!p.id &&
70 : conflictDetectionStore.conflictedPackages.some(
71 : (c) => c.package_id === p.id
72 : )
73 : )
74 :
75 15 : const filterOutdated = (packs: NodePack[]) =>
76 : packs.filter(
77 : (p) => getPackUpdateStatus(p, comfyManagerStore).isUpdateAvailable
78 : )
79 :
80 : // Data fetching triggers using whenever
81 15 : const needsInstalledPacks = computed(() =>
82 : [
83 : ManagerTab.AllInstalled,
84 : ManagerTab.UpdateAvailable,
85 : ManagerTab.Conflicting
86 : ].includes(tabType.value as ManagerTab)
87 : )
88 :
89 15 : const needsWorkflowPacks = computed(() =>
90 : [ManagerTab.Workflow, ManagerTab.Missing].includes(
91 : tabType.value as ManagerTab
92 : )
93 : )
94 :
95 : // Sorting only applies to fully-fetched tabs with no active search; paged
96 : // search/listing results (All, NotInstalled) are shown in the API's order.
97 15 : const isSortable = computed(
98 : () =>
99 : isEmptySearch.value &&
100 : (needsInstalledPacks.value || needsWorkflowPacks.value)
101 : )
102 :
103 15 : whenever(
104 : () =>
105 : needsInstalledPacks.value &&
106 : !installedPacksReady.value &&
107 : !isLoadingInstalled.value,
108 : () => startFetchInstalled()
109 : )
110 :
111 15 : whenever(
112 : () =>
113 : needsWorkflowPacks.value &&
114 : !workflowPacksReady.value &&
115 : !isLoadingWorkflow.value,
116 : () => startFetchWorkflowPacks()
117 : )
118 :
119 : // For Missing tab, also need installed packs to determine what's missing
120 15 : whenever(
121 : () =>
122 : tabType.value === ManagerTab.Missing &&
123 : !installedPacksReady.value &&
124 : !isLoadingInstalled.value,
125 : () => startFetchInstalled()
126 : )
127 :
128 : // Single computed for display packs - replaces 7 watches
129 : const displayPacks = computed(() => {
130 23 : const tab = tabType.value
131 23 : const hasSearch = !isEmptySearch.value
132 23 : const results = toValue(searchResults)
133 :
134 23 : switch (tab) {
135 : case ManagerTab.All:
136 22 : return results
137 :
138 : case ManagerTab.NotInstalled:
139 0 : return filterNotInstalled(results)
140 :
141 : case ManagerTab.AllInstalled:
142 1 : return hasSearch
143 : ? filterInstalledPack(results)
144 : : sortPacks(installedPacks.value)
145 :
146 : case ManagerTab.UpdateAvailable:
147 0 : return hasSearch
148 : ? filterOutdated(filterInstalledPack(results))
149 : : sortPacks(filterOutdated(installedPacks.value))
150 :
151 : case ManagerTab.Conflicting:
152 0 : return hasSearch
153 : ? filterConflicting(filterInstalledPack(results))
154 : : sortPacks(filterConflicting(installedPacks.value))
155 :
156 : case ManagerTab.Workflow:
157 0 : return hasSearch
158 : ? filterWorkflowPack(results)
159 : : sortPacks(workflowPacks.value)
160 :
161 : case ManagerTab.Missing:
162 0 : return hasSearch
163 : ? filterNotInstalled(filterWorkflowPack(results))
164 : : sortPacks(filterNotInstalled(workflowPacks.value))
165 :
166 : case ManagerTab.Unresolved:
167 0 : return []
168 :
169 : default:
170 0 : return results
171 : }
172 : })
173 :
174 : // Loading state - single computed
175 : const isLoading = computed(() => {
176 17 : const tab = tabType.value
177 17 : if (
178 : [
179 : ManagerTab.AllInstalled,
180 : ManagerTab.UpdateAvailable,
181 : ManagerTab.Conflicting
182 : ].includes(tab as ManagerTab)
183 : ) {
184 2 : return isLoadingInstalled.value
185 : }
186 17 : if ([ManagerTab.Workflow, ManagerTab.Missing].includes(tab as ManagerTab)) {
187 0 : return isLoadingWorkflow.value
188 : }
189 15 : return false
190 : })
191 :
192 : const missingNodePacks = computed(() =>
193 : filterNotInstalled(workflowPacks.value)
194 : )
195 :
196 15 : return {
197 : displayPacks,
198 : isSortable,
199 : isLoading,
200 : isLoadingInstalled,
201 : isLoadingWorkflow,
202 : installedPacks,
203 : workflowPacks,
204 : filterInstalledPack,
205 : filterWorkflowPack,
206 : missingNodePacks
207 : }
208 : }
|