Skip to content

Commit c8e553b

Browse files
committed
extract out graph run as separate component
1 parent a9b32fa commit c8e553b

3 files changed

Lines changed: 113 additions & 36 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import useCollectionStore from 'stores/CollectionStore';
2+
import GraphLogger, { LogLevel } from './GraphLogger';
3+
import Graph from './Graph';
4+
import { useTabStore } from 'stores/TabStore';
5+
import { cloneDeep } from 'lodash';
6+
7+
export const graphRun = async (tab, nodes, edges, collectionPath, selectedEnv) => {
8+
useTabStore.getState().updateFlowTestRunStatus(tab.id, true);
9+
10+
//runnableEdges(true);
11+
const startTime = Date.now();
12+
const logger = new GraphLogger();
13+
try {
14+
let envVariables = {};
15+
16+
//const activeCollection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
17+
//const activeEnv = activeCollection?.environments.find((e) => e.name === useTabStore.getState().selectedEnv);
18+
if (selectedEnv) {
19+
envVariables = cloneDeep(selectedEnv.variables);
20+
}
21+
22+
// ============= flow =====================
23+
const g = new Graph(
24+
nodes, //cloneDeep(reactFlowInstance.getNodes()),
25+
edges, //cloneDeep(reactFlowInstance.getEdges()),
26+
startTime,
27+
envVariables,
28+
logger,
29+
'main',
30+
collectionPath,
31+
);
32+
const result = await g.run();
33+
const time = Date.now() - startTime;
34+
logger.add(LogLevel.INFO, `Total time: ${time} ms`);
35+
useTabStore.getState().updateFlowTestRunStatus(tab.id, false);
36+
//await onGraphComplete(result.status, time, logger.get());
37+
} catch (error) {
38+
const time = Date.now() - startTime;
39+
logger.add(LogLevel.INFO, `Total time: ${time} ms`);
40+
useTabStore.getState().updateFlowTestRunStatus(tab.id, false);
41+
//await onGraphComplete('Failed', time, logger.get());
42+
//toast.error(`Internal error running graph`);
43+
//runnableEdges(false);
44+
}
45+
};

src/components/molecules/flow/index.js

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { BUTTON_INTENT_TYPES, BUTTON_TYPES } from 'constants/Common';
2828
import GraphLogger, { LogLevel } from './graph/GraphLogger';
2929
import Mousetrap from 'mousetrap';
3030
import { uploadGraphRunLogs } from 'service/collection';
31+
import { graphRun } from './graph/GraphRun';
3132

3233
const StartNode = () => (
3334
<FlowNode title='Start' handleLeft={false} handleRight={true} handleRightData={{ type: 'source' }}></FlowNode>
@@ -112,6 +113,18 @@ const Flow = ({ tab, collectionId }) => {
112113
[],
113114
);
114115

116+
useMemo(() => {
117+
if (reactFlowInstance) {
118+
const updatedEdges = reactFlowInstance.getEdges().map((edge) => {
119+
return {
120+
...edge,
121+
animated: tab.running,
122+
};
123+
});
124+
setEdges(updatedEdges);
125+
}
126+
}, [tab.running]);
127+
115128
const runnableEdges = (runnable) => {
116129
const updatedEdges = reactFlowInstance.getEdges().map((edge) => {
117130
return {
@@ -251,43 +264,51 @@ const Flow = ({ tab, collectionId }) => {
251264
classes={'absolute bottom-4 right-20 z-[2000] text-xl'}
252265
btnType={BUTTON_TYPES.primary}
253266
isDisabled={false}
254-
onClickHandle={async () => {
255-
runnableEdges(true);
256-
const startTime = Date.now();
257-
const logger = new GraphLogger();
258-
try {
259-
let envVariables = {};
260-
261-
const activeCollection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
262-
const activeEnv = activeCollection?.environments.find(
263-
(e) => e.name === useTabStore.getState().selectedEnv,
264-
);
265-
if (activeEnv) {
266-
envVariables = cloneDeep(activeEnv.variables);
267-
}
268-
269-
// ============= flow =====================
270-
const g = new Graph(
271-
cloneDeep(reactFlowInstance.getNodes()),
272-
cloneDeep(reactFlowInstance.getEdges()),
273-
startTime,
274-
envVariables,
275-
logger,
276-
'main',
277-
activeCollection.pathname,
278-
);
279-
const result = await g.run();
280-
const time = Date.now() - startTime;
281-
logger.add(LogLevel.INFO, `Total time: ${time} ms`);
282-
await onGraphComplete(result.status, time, logger.get());
283-
} catch (error) {
284-
const time = Date.now() - startTime;
285-
logger.add(LogLevel.INFO, `Total time: ${time} ms`);
286-
await onGraphComplete('Failed', time, logger.get());
287-
toast.error(`Internal error running graph`);
288-
runnableEdges(false);
289-
}
267+
onClickHandle={() => {
268+
const activeCollection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
269+
const activeEnv = activeCollection?.environments.find((e) => e.name === useTabStore.getState().selectedEnv);
270+
const nodes = cloneDeep(reactFlowInstance.getNodes());
271+
const edges = cloneDeep(reactFlowInstance.getEdges());
272+
273+
graphRun(tab, nodes, edges, activeCollection?.pathname, activeEnv);
290274
}}
275+
// onClickHandle={async () => {
276+
// runnableEdges(true);
277+
// const startTime = Date.now();
278+
// const logger = new GraphLogger();
279+
// try {
280+
// let envVariables = {};
281+
282+
// const activeCollection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
283+
// const activeEnv = activeCollection?.environments.find(
284+
// (e) => e.name === useTabStore.getState().selectedEnv,
285+
// );
286+
// if (activeEnv) {
287+
// envVariables = cloneDeep(activeEnv.variables);
288+
// }
289+
290+
// // ============= flow =====================
291+
// const g = new Graph(
292+
// cloneDeep(reactFlowInstance.getNodes()),
293+
// cloneDeep(reactFlowInstance.getEdges()),
294+
// startTime,
295+
// envVariables,
296+
// logger,
297+
// 'main',
298+
// activeCollection.pathname,
299+
// );
300+
// const result = await g.run();
301+
// const time = Date.now() - startTime;
302+
// logger.add(LogLevel.INFO, `Total time: ${time} ms`);
303+
// await onGraphComplete(result.status, time, logger.get());
304+
// } catch (error) {
305+
// const time = Date.now() - startTime;
306+
// logger.add(LogLevel.INFO, `Total time: ${time} ms`);
307+
// await onGraphComplete('Failed', time, logger.get());
308+
// toast.error(`Internal error running graph`);
309+
// runnableEdges(false);
310+
// }
311+
// }}
291312
fullWidth={false}
292313
>
293314
Run

src/stores/TabStore.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const useTabStore = create((set, get) => ({
2727
name: flowtest.name,
2828
pathname: flowtest.pathname,
2929
flowData: flowtest.flowData,
30+
running: false,
3031
run: {},
3132
};
3233

@@ -113,6 +114,16 @@ export const useTabStore = create((set, get) => ({
113114
}),
114115
);
115116
},
117+
updateFlowTestRunStatus: (tabId, running) => {
118+
set(
119+
produce((state) => {
120+
const existingTab = state.tabs.find((t) => t.id === tabId);
121+
if (existingTab) {
122+
existingTab.running = running;
123+
}
124+
}),
125+
);
126+
},
116127
addEnvTab: (env, collectionId) => {
117128
const existingTab = get().tabs.find(
118129
(t) =>

0 commit comments

Comments
 (0)