|
| 1 | +import { computed, onMounted } from 'vue' |
| 2 | + |
| 3 | +import { useInstalledPacks } from '@/composables/nodePack/useInstalledPacks' |
| 4 | +import { useComfyManagerStore } from '@/stores/comfyManagerStore' |
| 5 | +import type { components } from '@/types/comfyRegistryTypes' |
| 6 | +import { compareVersions, isSemVer } from '@/utils/formatUtil' |
| 7 | + |
| 8 | +/** |
| 9 | + * Composable to find NodePacks that have updates available |
| 10 | + * Uses the same filtering approach as ManagerDialogContent.vue |
| 11 | + * Automatically fetches installed pack data when initialized |
| 12 | + */ |
| 13 | +export const useUpdateAvailableNodes = () => { |
| 14 | + const comfyManagerStore = useComfyManagerStore() |
| 15 | + const { installedPacks, isLoading, error, startFetchInstalled } = |
| 16 | + useInstalledPacks() |
| 17 | + |
| 18 | + // Check if a pack has updates available (same logic as usePackUpdateStatus) |
| 19 | + const isOutdatedPack = (pack: components['schemas']['Node']) => { |
| 20 | + const isInstalled = comfyManagerStore.isPackInstalled(pack?.id) |
| 21 | + if (!isInstalled) return false |
| 22 | + |
| 23 | + const installedVersion = comfyManagerStore.getInstalledPackVersion( |
| 24 | + pack.id ?? '' |
| 25 | + ) |
| 26 | + const latestVersion = pack.latest_version?.version |
| 27 | + |
| 28 | + const isNightlyPack = !!installedVersion && !isSemVer(installedVersion) |
| 29 | + |
| 30 | + if (isNightlyPack || !latestVersion) { |
| 31 | + return false |
| 32 | + } |
| 33 | + |
| 34 | + return compareVersions(latestVersion, installedVersion) > 0 |
| 35 | + } |
| 36 | + |
| 37 | + // Same filtering logic as ManagerDialogContent.vue |
| 38 | + const filterOutdatedPacks = (packs: components['schemas']['Node'][]) => |
| 39 | + packs.filter(isOutdatedPack) |
| 40 | + |
| 41 | + // Filter only outdated packs from installed packs |
| 42 | + const updateAvailableNodePacks = computed(() => { |
| 43 | + if (!installedPacks.value.length) return [] |
| 44 | + return filterOutdatedPacks(installedPacks.value) |
| 45 | + }) |
| 46 | + |
| 47 | + // Check if there are any outdated packs |
| 48 | + const hasUpdateAvailable = computed(() => { |
| 49 | + return updateAvailableNodePacks.value.length > 0 |
| 50 | + }) |
| 51 | + |
| 52 | + // Automatically fetch installed pack data when composable is used |
| 53 | + onMounted(async () => { |
| 54 | + if (!installedPacks.value.length && !isLoading.value) { |
| 55 | + await startFetchInstalled() |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + return { |
| 60 | + updateAvailableNodePacks, |
| 61 | + hasUpdateAvailable, |
| 62 | + isLoading, |
| 63 | + error |
| 64 | + } |
| 65 | +} |
0 commit comments