Skip to content

Commit 17ed953

Browse files
authored
Merge pull request #71 from FlowTestAI/use-relative-paths
use relative path in complex node
2 parents 824e01a + 7b26616 commit 17ed953

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const { ipcRenderer, contextBridge } = require('electron');
2+
const path = require('path');
23

34
contextBridge.exposeInMainWorld('ipcRenderer', {
45
invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
56
on: (channel, handler) => ipcRenderer.on(channel, (event, ...args) => handler(...args)),
7+
join: (...args) => path.join(...args),
8+
relative: (...args) => path.relative(...args),
69
});

src/components/molecules/flow/graph/Graph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Graph {
123123
}
124124

125125
if (node.type === 'complexNode') {
126-
const flowData = await readFlowTestSync(node.data.pathname);
126+
const flowData = await readFlowTestSync(node.data.relativePath);
127127
if (flowData) {
128128
const cNode = new complexNode(
129129
cloneDeep(flowData.nodes),

src/components/molecules/flow/nodes/ComplexNode.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import { getAllFlowTests } from 'stores/utils';
66
import useCollectionStore from 'stores/CollectionStore';
77

88
const ComplexNode = ({ id, data }) => {
9+
const { ipcRenderer } = window;
10+
911
const setFlowForComplexNode = useCanvasStore((state) => state.setFlowForComplexNode);
10-
const collectionId = useCanvasStore((state) => state.collectionId);
11-
const flowTests = getAllFlowTests(useCollectionStore.getState().collections.find((c) => c.id === collectionId));
12+
const collectionId = useCanvasStore.getState().collectionId;
13+
const collection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
14+
const flowTests = getAllFlowTests(collection).map((fullPath) => {
15+
return ipcRenderer.relative(collection.pathname, fullPath);
16+
});
1217

13-
const setFlow = (pathname) => {
14-
setFlowForComplexNode(id, pathname);
18+
const setFlow = (relativePath) => {
19+
setFlowForComplexNode(id, relativePath);
1520
};
1621

17-
if (data.pathname) {
18-
if (!flowTests.find((f) => f === data.pathname)) {
22+
if (data.relativePath) {
23+
if (!flowTests.find((f) => f === data.relativePath)) {
1924
setFlow('');
2025
}
2126
}
@@ -32,7 +37,7 @@ const ComplexNode = ({ id, data }) => {
3237
<select
3338
onChange={(e) => setFlow(e.target.value)}
3439
name='flow'
35-
value={data.pathname ? data.pathname : ''}
40+
value={data.relativePath ? data.relativePath : ''}
3641
className='h-12 outline-none max-w-32'
3742
>
3843
<option key='None' value=''>

src/service/collection.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useEventStore } from 'stores/EventListenerStore';
55
import { OBJ_TYPES } from 'constants/Common';
66
import { toast } from 'react-toastify';
77
import { useTabStore } from 'stores/TabStore';
8+
import useCanvasStore from 'stores/CanvasStore';
89

910
export const createCollection = (openAPISpecFilePath, collectionFolderPath) => {
1011
const { ipcRenderer } = window;
@@ -293,14 +294,22 @@ export const readFlowTest = (pathname, collectionId) => {
293294
}
294295
};
295296

296-
export const readFlowTestSync = (pathname) => {
297+
export const readFlowTestSync = (relativePath) => {
297298
try {
298-
if (pathname.trim() != '') {
299+
if (relativePath.trim() != '') {
299300
const { ipcRenderer } = window;
300301

301-
return new Promise((resolve, reject) => {
302-
return ipcRenderer.invoke('renderer:read-flowtest-sync', pathname).then(resolve).catch(reject);
303-
});
302+
// assumes that this function is triggered from graph run
303+
const collectionId = useCanvasStore.getState().collectionId;
304+
const collection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
305+
if (collection) {
306+
return new Promise((resolve, reject) => {
307+
return ipcRenderer
308+
.invoke('renderer:read-flowtest-sync', ipcRenderer.join(collection.pathname, relativePath))
309+
.then(resolve)
310+
.catch(reject);
311+
});
312+
}
304313
}
305314
} catch (error) {
306315
console.log(`Error reading flowtest: ${error}`);

src/stores/CanvasStore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,13 @@ const useCanvasStore = create((set, get) => ({
327327
}),
328328
});
329329
},
330-
setFlowForComplexNode: (nodeId, pathname) => {
330+
setFlowForComplexNode: (nodeId, relativePath) => {
331331
set({
332332
nodes: get().nodes.map((node) => {
333333
if (node.id === nodeId) {
334334
// it's important to create a new object here, to inform React Flow about the cahnges
335335
node.data = {
336-
pathname,
336+
relativePath,
337337
};
338338
}
339339

src/stores/utils.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ export const getAllFlowTests = (collection) => {
3030
if (collection) {
3131
let flattenedItems = flattenItems(collection.items);
3232

33-
return flattenedItems.map((i) => {
34-
if (i.type === OBJ_TYPES.flowtest) {
35-
return i.pathname;
36-
}
37-
});
33+
return flattenedItems.filter((i) => i.type === OBJ_TYPES.flowtest).map((i) => i.pathname);
3834
}
3935
};
4036

0 commit comments

Comments
 (0)