Skip to content

Commit 42cfc54

Browse files
committed
robust isSaveNeeded conditions for flow canvas
1 parent c237f4e commit 42cfc54

3 files changed

Lines changed: 50 additions & 17 deletions

File tree

src/components/atoms/Tabs.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,16 @@ import { useTabStore } from 'stores/TabStore';
44
import ConfirmActionModal from 'components/molecules/modals/ConfirmActionModal';
55
import { isEqual } from 'lodash';
66
import { OBJ_TYPES } from 'constants/Common';
7-
import { compare } from './util';
7+
import { isSaveNeeded } from './util';
88
import { saveHandle } from 'components/molecules/modals/SaveFlowModal';
99

1010
const tabUnsavedChanges = (tab) => {
1111
if (tab.type === OBJ_TYPES.flowtest && tab.flowDataDraft) {
12-
const draftNodesSansOutput = tab.flowDataDraft.nodes.map((node) => {
13-
if (node.type === 'outputNode' && node.data.output) {
14-
const { ['output']: _, ...data } = node.data;
15-
return {
16-
...node,
17-
data,
18-
};
19-
}
20-
return node;
21-
});
22-
if (!isEqual(tab.flowData.nodes, draftNodesSansOutput) || !isEqual(tab.flowData.edges, tab.flowDataDraft.edges)) {
23-
console.log('Detected unsaved changes: ', compare(tab.flowData, tab.flowDataDraft));
24-
return true;
25-
}
12+
return isSaveNeeded(tab.flowData, tab.flowDataDraft);
2613
} else if (tab.type === OBJ_TYPES.environment && tab.variablesDraft && !isEqual(tab.variables, tab.variablesDraft)) {
2714
return true;
2815
} else {
29-
false;
16+
return false;
3017
}
3118
};
3219

src/components/atoms/util.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,50 @@ export const compare = (a, b) => {
6363

6464
return result;
6565
};
66+
67+
export const isSaveNeeded = (flowData, flowDataDraft) => {
68+
if (flowData.nodes.length != flowDataDraft.nodes.length) {
69+
console.log('Detected unsaved changes: node count');
70+
return true;
71+
}
72+
73+
if (flowData.edges.length != flowDataDraft.edges.length) {
74+
console.log('Detected unsaved changes: edges count');
75+
return true;
76+
}
77+
78+
const unwrapEdge = function ({ source, sourceHandle, target, targetHandle }) {
79+
return { source, sourceHandle, target, targetHandle };
80+
};
81+
82+
const edges = flowData.edges.map((e) => unwrapEdge(e));
83+
const edgesDraft = flowDataDraft.edges.map((e) => unwrapEdge(e));
84+
85+
if (!isEqual(edges, edgesDraft)) {
86+
console.log('Detected unsaved changes: ', compare(edges, edgesDraft));
87+
return true;
88+
}
89+
90+
const unwrapNode = function ({ id, type, data, position }) {
91+
return { id, type, data, position };
92+
};
93+
94+
const nodes = flowData.nodes.map((e) => unwrapNode(e));
95+
const nodesDraft = flowDataDraft.nodes.map((node) => {
96+
if (node.type === 'outputNode' && node.data.output) {
97+
const { ['output']: _, ...data } = node.data;
98+
return unwrapNode({
99+
...node,
100+
data,
101+
});
102+
}
103+
return unwrapNode(node);
104+
});
105+
106+
if (!isEqual(nodes, nodesDraft)) {
107+
console.log('Detected unsaved changes: ', compare(nodes, nodesDraft));
108+
return true;
109+
}
110+
111+
return false;
112+
};

src/service/collection.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ export const deleteEnvironmentFile = (name, collectionId) => {
216216

217217
export const addOrUpdateDotEnvironmentFile = (collectionId, variables) => {
218218
const { ipcRenderer } = window;
219-
console.log(variables);
220219

221220
const collection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
222221

0 commit comments

Comments
 (0)