Line data Source code
1 : import { computed } from 'vue'
2 : import type { Ref } from 'vue'
3 :
4 : import type { components } from '@/types/comfyRegistryTypes'
5 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
6 : import { isNightlyVersion } from '@/platform/customNodes/manager/composables/nodePack/packUpdateStatus'
7 :
8 : type NodePack = components['schemas']['Node']
9 :
10 : type SelectionState = 'all-installed' | 'none-installed' | 'mixed'
11 :
12 : /**
13 : * Composable for managing multi-package selection states
14 : * Handles installation status tracking and selection state determination
15 : */
16 15 : export function usePacksSelection(nodePacks: Ref<NodePack[]>) {
17 15 : const managerStore = useComfyManagerStore()
18 :
19 15 : const installedPacks = computed(() =>
20 : nodePacks.value.filter((pack) => managerStore.isPackInstalled(pack.id))
21 : )
22 :
23 15 : const notInstalledPacks = computed(() =>
24 : nodePacks.value.filter((pack) => !managerStore.isPackInstalled(pack.id))
25 : )
26 :
27 15 : const isAllInstalled = computed(
28 : () => installedPacks.value.length === nodePacks.value.length
29 : )
30 :
31 15 : const isNoneInstalled = computed(
32 : () => notInstalledPacks.value.length === nodePacks.value.length
33 : )
34 :
35 15 : const isMixed = computed(
36 : () => installedPacks.value.length > 0 && notInstalledPacks.value.length > 0
37 : )
38 :
39 15 : const selectionState = computed<SelectionState>(() => {
40 0 : if (isAllInstalled.value) return 'all-installed'
41 0 : if (isNoneInstalled.value) return 'none-installed'
42 0 : return 'mixed'
43 : })
44 :
45 : /**
46 : * Nightly packs are installed packs with a non-semver version (git hash)
47 : * that are also enabled
48 : */
49 15 : const nightlyPacks = computed(() =>
50 : installedPacks.value.filter((pack) => {
51 0 : if (!pack.id) return false
52 0 : const version = managerStore.getInstalledPackVersion(pack.id)
53 0 : const isEnabled = managerStore.isPackEnabled(pack.id)
54 0 : return isNightlyVersion(version) && isEnabled
55 : })
56 : )
57 :
58 : const hasNightlyPacks = computed(() => nightlyPacks.value.length > 0)
59 :
60 15 : return {
61 : installedPacks,
62 : notInstalledPacks,
63 : isAllInstalled,
64 : isNoneInstalled,
65 : isMixed,
66 : selectionState,
67 : nightlyPacks,
68 : hasNightlyPacks
69 : }
70 : }
|