Line data Source code
1 : import { computed, unref } from 'vue'
2 : import type { ComputedRef } from 'vue'
3 :
4 : import { useImportFailedNodeDialog } from '@/platform/customNodes/manager/composables/dialogs/useImportFailedNodeDialog'
5 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
6 : import { useConflictDetectionStore } from '@/platform/customNodes/manager/stores/conflictDetectionStore'
7 : import type {
8 : ConflictDetail,
9 : ConflictDetectionResult
10 : } from '@/platform/customNodes/manager/types/conflictDetectionTypes'
11 :
12 : /**
13 : * Extracting import failed conflicts from conflict list
14 : */
15 3 : function extractImportFailedConflicts(conflicts?: ConflictDetail[] | null) {
16 3 : if (!conflicts) return null
17 :
18 0 : const importFailedConflicts = conflicts.filter(
19 : (item): item is ConflictDetail => item.type === 'import_failed'
20 : )
21 :
22 3 : return importFailedConflicts.length > 0 ? importFailedConflicts : null
23 : }
24 :
25 : /**
26 : * Creating import failed dialog
27 : */
28 30 : function createImportFailedDialog() {
29 30 : const { show } = useImportFailedNodeDialog()
30 :
31 30 : return (
32 : conflictedPackages: ConflictDetectionResult[] | null,
33 : onClose?: () => void
34 : ) => {
35 0 : if (conflictedPackages && conflictedPackages.length > 0) {
36 0 : show({
37 : conflictedPackages,
38 : dialogComponentProps: {
39 : onClose
40 : }
41 : })
42 : }
43 : }
44 : }
45 :
46 : /**
47 : * Composable for detecting and handling import failed conflicts
48 : * @param packageId - Package ID string or computed ref
49 : * @returns Object with import failed detection and dialog handler
50 : */
51 30 : export function useImportFailedDetection(
52 : packageId?: string | ComputedRef<string> | null
53 : ) {
54 30 : const { isPackInstalled } = useComfyManagerStore()
55 30 : const { getConflictsForPackageByID } = useConflictDetectionStore()
56 :
57 30 : const isInstalled = computed(() =>
58 : packageId ? isPackInstalled(unref(packageId)) : false
59 : )
60 :
61 30 : const conflicts = computed(() => {
62 3 : const currentPackageId = unref(packageId)
63 3 : if (!currentPackageId || !isInstalled.value) return null
64 3 : return getConflictsForPackageByID(currentPackageId) || null
65 : })
66 :
67 30 : const importFailedInfo = computed(() => {
68 3 : return extractImportFailedConflicts(conflicts.value?.conflicts)
69 : })
70 :
71 30 : const importFailed = computed(() => {
72 3 : return importFailedInfo.value !== null
73 : })
74 :
75 30 : const openDialog = createImportFailedDialog()
76 :
77 30 : return {
78 : importFailedInfo,
79 : importFailed,
80 : showImportFailedDialog: (onClose?: () => void) => {
81 0 : if (conflicts.value) {
82 0 : openDialog([conflicts.value], onClose)
83 : }
84 : },
85 : isInstalled
86 : }
87 : }
|