|
import pick from 'lodash/pick'; |
|
import { useCallback, useEffect } from 'react'; |
|
import { Edge } from 'reactflow'; |
|
import { IOperatorForm } from '../../interface'; |
|
import useGraphStore from '../../store'; |
|
|
|
export const useBuildRelevantOptions = () => { |
|
const nodes = useGraphStore((state) => state.nodes); |
|
|
|
const buildRelevantOptions = useCallback( |
|
(toList: string[]) => { |
|
return nodes |
|
.filter( |
|
(x) => !toList.some((y) => y === x.id), |
|
) |
|
.map((x) => ({ label: x.data.name, value: x.id })); |
|
}, |
|
[nodes], |
|
); |
|
|
|
return buildRelevantOptions; |
|
}; |
|
|
|
const getTargetOfEdge = (edges: Edge[], sourceHandle: string) => |
|
edges.find((x) => x.sourceHandle === sourceHandle)?.target; |
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useWatchConnectionChanges = ({ nodeId, form }: IOperatorForm) => { |
|
const edges = useGraphStore((state) => state.edges); |
|
const getNode = useGraphStore((state) => state.getNode); |
|
const node = getNode(nodeId); |
|
|
|
const watchFormChanges = useCallback(() => { |
|
if (node) { |
|
form?.setFieldsValue(pick(node, ['yes', 'no'])); |
|
} |
|
}, [node, form]); |
|
|
|
const watchConnectionChanges = useCallback(() => { |
|
const edgeList = edges.filter((x) => x.source === nodeId); |
|
const yes = getTargetOfEdge(edgeList, 'yes'); |
|
const no = getTargetOfEdge(edgeList, 'no'); |
|
form?.setFieldsValue({ yes, no }); |
|
}, [edges, nodeId, form]); |
|
|
|
useEffect(() => { |
|
watchFormChanges(); |
|
}, [watchFormChanges]); |
|
}; |
|
|