1
- import React , { createContext , PropsWithChildren , useCallback , useContext , useMemo , useReducer } from "react" ;
1
+ import type { PropsWithChildren } from "react" ;
2
+ import React , { createContext , useCallback , useContext , useMemo , useReducer } from "react" ;
2
3
import { useSelector } from "react-redux" ;
4
+
5
+ import type { TransitionResults } from "../../../../common/TestResultUtils" ;
3
6
import TestResultUtils from "../../../../common/TestResultUtils" ;
4
- import { getProcessName , getScenarioGraph , getTestResults } from "../../../../reducers/selectors/graph" ;
7
+ import { getScenarioGraph , getTestResults } from "../../../../reducers/selectors/graph" ;
5
8
import NodeUtils from "../../NodeUtils" ;
6
- import { VariableContextType } from "./VariableContextTree" ;
9
+ import type { VariableContextType } from "./VariableContextTree" ;
7
10
8
11
export type InputOutputState = {
9
12
inputDataSetId ?: string | null ;
@@ -23,13 +26,13 @@ type Action =
23
26
context : VariableContextType ;
24
27
} ;
25
28
29
+ type Created = TransitionResults & { id : string } ;
26
30
type ContextType = {
27
31
state : InputOutputState ;
28
32
dispatch : React . Dispatch < Action > ;
29
- getAvailableContexts : ( nodeIds : string [ ] , direction ?: "input" | "output" ) => VariableContextType [ ] ;
30
- prevNodes : string [ ] ;
31
- inputNodes : string [ ] ;
32
- outputNodes : string [ ] ;
33
+ getAvailableContexts : ( direction ?: "input" | "output" ) => VariableContextType [ ] ;
34
+ inputNodesIds : Created [ ] ;
35
+ outputNodesIds : Created [ ] ;
33
36
} ;
34
37
35
38
const InputOutputContext = createContext < ContextType > ( null ) ;
@@ -67,18 +70,33 @@ export const InputOutputContextProvider = ({
67
70
} : PropsWithChildren < {
68
71
nodeId : string ;
69
72
} > ) => {
70
- const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
71
-
72
73
const scenario = useSelector ( getScenarioGraph ) ;
74
+ const testResults = useSelector ( getTestResults ) ;
73
75
74
- const [ inputNodes , outputNodes , prevNodes ] = useMemo ( ( ) => {
75
- if ( ! nodeId ) throw "no NodeId provided!" ;
76
- return [
77
- [ nodeId ] ,
78
- NodeUtils . getNodesConnectedToOutput ( nodeId , scenario ) . map ( ( n ) => n . id ) ,
79
- NodeUtils . getNodesConnectedToInput ( nodeId , scenario ) . map ( ( n ) => n . id ) ,
80
- ] ;
81
- } , [ nodeId , scenario ] ) ;
76
+ const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
77
+
78
+ const nodeTransitionResults = useMemo (
79
+ ( ) => testResults ?. nodeTransitionResults ?. filter ( ( r ) => r . destinationNodeId === nodeId || r . sourceNodeId === nodeId ) ,
80
+ [ nodeId , testResults ?. nodeTransitionResults ] ,
81
+ ) ;
82
+ const inputs = useMemo ( ( ) => {
83
+ return NodeUtils . getNodesConnectedToInput ( nodeId , scenario ) . map ( ( { id } ) => {
84
+ const transitionResults = nodeTransitionResults ?. find ( ( r ) => r . destinationNodeId === nodeId && r . sourceNodeId === id ) ;
85
+ return {
86
+ id,
87
+ ...transitionResults ,
88
+ } ;
89
+ } ) ;
90
+ } , [ nodeId , nodeTransitionResults , scenario ] ) ;
91
+ const outputs = useMemo ( ( ) => {
92
+ return NodeUtils . getNodesConnectedToOutput ( nodeId , scenario ) . map ( ( { id } ) => {
93
+ const transitionResults = nodeTransitionResults ?. find ( ( r ) => r . sourceNodeId === nodeId && r . destinationNodeId === id ) ;
94
+ return {
95
+ id,
96
+ ...transitionResults ,
97
+ } ;
98
+ } ) ;
99
+ } , [ nodeId , nodeTransitionResults , scenario ] ) ;
82
100
83
101
const isContextDisabled = useCallback (
84
102
( id : string , direction : "input" | "output" = "input" ) => {
@@ -92,46 +110,49 @@ export const InputOutputContextProvider = ({
92
110
[ state . inputDataSetId ] ,
93
111
) ;
94
112
95
- const results = useSelector ( getTestResults ) ;
113
+ const getError = useCallback (
114
+ ( destinationNodeId : string , contextId : string ) =>
115
+ TestResultUtils . resultsForNode ( testResults , destinationNodeId ) ?. errors ?. find ( ( { context } ) => context . id === contextId ) ,
116
+ [ testResults ] ,
117
+ ) ;
118
+
96
119
const getAvailableContexts = useCallback (
97
- ( nodeIds : string [ ] , direction : "input" | "output" = "input" ) => {
120
+ ( direction : "input" | "output" = "input" ) => {
121
+ const transitionResults = direction === "input" ? inputs : outputs ;
98
122
const contexts : VariableContextType [ ] = [ ] ;
99
-
100
- nodeIds . forEach ( ( nodeId ) => {
101
- const testResults = TestResultUtils . resultsForNode ( results , nodeId ) ;
102
- testResults . nodeResults . forEach ( ( { id, variables } ) => {
123
+ transitionResults . forEach ( ( { id : contextNodeId , destinationNodeId, results } ) => {
124
+ results ?. forEach ( ( { id, variables } ) => {
103
125
const foundContext = contexts . find ( ( context ) => context . id === id ) ;
104
126
if ( foundContext ) {
105
- foundContext . nodeIds . push ( nodeId ) ;
127
+ foundContext . nodeIds . push ( contextNodeId ) ;
106
128
return ;
107
129
}
108
130
109
- const error = testResults . errors ?. find ( ( { context } ) => context . id === id ) ;
131
+ const error = direction === "input" && getError ( destinationNodeId , id ) ;
110
132
111
133
contexts . push ( {
112
134
id,
113
135
variables,
114
136
disabled : isContextDisabled ( id , direction ) ,
115
- nodeIds : [ nodeId ] ,
137
+ nodeIds : [ contextNodeId ] ,
116
138
error : error ?. throwable ,
117
139
} ) ;
118
140
} ) ;
119
141
} ) ;
120
142
return contexts ;
121
143
} ,
122
- [ isContextDisabled , results ] ,
144
+ [ inputs , outputs , getError , isContextDisabled ] ,
123
145
) ;
124
146
125
- const value = useMemo (
147
+ const value = useMemo < ContextType > (
126
148
( ) => ( {
127
149
state,
128
150
dispatch,
129
151
getAvailableContexts,
130
- prevNodes,
131
- inputNodes,
132
- outputNodes,
152
+ inputNodesIds : inputs ,
153
+ outputNodesIds : outputs ,
133
154
} ) ,
134
- [ getAvailableContexts , inputNodes , outputNodes , prevNodes , state ] ,
155
+ [ getAvailableContexts , inputs , outputs , state ] ,
135
156
) ;
136
157
return < InputOutputContext . Provider value = { value } > { children } </ InputOutputContext . Provider > ;
137
158
} ;
0 commit comments