Line data Source code
1 : import { storeToRefs } from 'pinia'
2 : import { computed, toValue } from 'vue'
3 : import type { MaybeRefOrGetter } from 'vue'
4 :
5 : import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph'
6 : import { SubgraphNode } from '@/lib/litegraph/src/litegraph'
7 : import { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
8 : import { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
9 : import { useMissingNodesErrorStore } from '@/platform/workflowErrors/nodeReplacement/missingNodesErrorStore'
10 : import { useApp } from '@/scripts/appInstance'
11 : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
12 : import {
13 : getActiveGraphNodeIds,
14 : getExecutionIdByNode
15 : } from '@/utils/graphTraversalUtil'
16 :
17 : import { useHasBlockingError } from './useHasBlockingError'
18 :
19 : interface ErrorsTabSelection {
20 : hasSelection: MaybeRefOrGetter<boolean>
21 : selectedNodes: MaybeRefOrGetter<readonly LGraphNode[]>
22 : currentGraph: MaybeRefOrGetter<LGraph | null | undefined>
23 : }
24 :
25 : /**
26 : * Whether the errors tab has issues relevant to a selection (or to the whole
27 : * workflow when nothing is selected), and whether any of them block a run.
28 : */
29 201 : export function useErrorsTabIndicator({
30 : hasSelection,
31 : selectedNodes,
32 : currentGraph
33 : }: ErrorsTabSelection) {
34 201 : const executionErrorStore = useExecutionErrorStore()
35 201 : const missingNodesErrorStore = useMissingNodesErrorStore()
36 201 : const { activeMissingModelGraphIds } = storeToRefs(useMissingModelStore())
37 201 : const { activeMissingMediaGraphIds } = storeToRefs(useMissingMediaStore())
38 201 : const { hasAnyError, allErrorExecutionIds } = storeToRefs(executionErrorStore)
39 :
40 201 : const activeMissingNodeGraphIds = computed<Set<string>>(() => {
41 25 : const app = useApp()
42 25 : if (!app.isGraphReady) return new Set()
43 25 : return getActiveGraphNodeIds(
44 : app.rootGraph,
45 : toValue(currentGraph) ?? app.rootGraph,
46 : missingNodesErrorStore.missingAncestorExecutionIds
47 : )
48 : })
49 :
50 191 : function someSelectedNode(predicate: (node: LGraphNode) => boolean) {
51 191 : return toValue(selectedNodes).some(predicate)
52 : }
53 :
54 153 : function selectedNodeIn(ids: Set<string>) {
55 153 : return someSelectedNode((node) => ids.has(String(node.id)))
56 : }
57 :
58 201 : const hasContainerInternalError = computed(
59 : () =>
60 : allErrorExecutionIds.value.length > 0 &&
61 : someSelectedNode(
62 : (node) =>
63 : node instanceof SubgraphNode &&
64 : executionErrorStore.isContainerWithInternalError(node)
65 : )
66 : )
67 :
68 : const hasRelevantErrors = computed(() => {
69 177 : if (!toValue(hasSelection)) return hasAnyError.value
70 177 : return (
71 : selectedNodeIn(executionErrorStore.activeGraphErrorNodeIds) ||
72 : hasContainerInternalError.value ||
73 : selectedNodeIn(activeMissingNodeGraphIds.value) ||
74 : selectedNodeIn(activeMissingModelGraphIds.value) ||
75 : selectedNodeIn(activeMissingMediaGraphIds.value)
76 : )
77 : })
78 :
79 : const hasPendingErrorScanSelected = computed(() => {
80 38 : const app = useApp()
81 38 : if (!app.isGraphReady) return false
82 38 : const rootGraph = app.rootGraph
83 38 : return someSelectedNode((node) => {
84 9 : const executionId = getExecutionIdByNode(rootGraph, node)
85 9 : return (
86 : executionId !== null &&
87 : executionErrorStore.hasPendingAddedNodeErrorScan(rootGraph, executionId)
88 : )
89 : })
90 : })
91 :
92 201 : return {
93 : hasRelevantErrors,
94 : hasPendingErrorScanSelected,
95 : hasBlockingError: useHasBlockingError()
96 : }
97 : }
|