|
import get from 'lodash/get'; |
|
import pick from 'lodash/pick'; |
|
import { useEffect, useMemo, useState } from 'react'; |
|
import { useUpdateNodeInternals } from 'reactflow'; |
|
import { Operator } from '../../constant'; |
|
import { IPosition, NodeData } from '../../interface'; |
|
import { |
|
buildNewPositionMap, |
|
generateSwitchHandleText, |
|
isKeysEqual, |
|
} from '../../utils'; |
|
|
|
export const useBuildCategorizeHandlePositions = ({ |
|
data, |
|
id, |
|
}: { |
|
id: string; |
|
data: NodeData; |
|
}) => { |
|
const operatorName = data.label as Operator; |
|
const updateNodeInternals = useUpdateNodeInternals(); |
|
const [positionMap, setPositionMap] = useState<Record<string, IPosition>>({}); |
|
|
|
const categoryData = useMemo(() => { |
|
if (operatorName === Operator.Categorize) { |
|
return get(data, `form.category_description`, {}); |
|
} else if (operatorName === Operator.Switch) { |
|
return get(data, 'form.conditions', []); |
|
} |
|
return {}; |
|
}, [data, operatorName]); |
|
|
|
const positions = useMemo(() => { |
|
return Object.keys(categoryData) |
|
.map((x, idx) => { |
|
const position = positionMap[x]; |
|
let text = x; |
|
if (operatorName === Operator.Switch) { |
|
text = generateSwitchHandleText(idx); |
|
} |
|
return { text, ...position }; |
|
}) |
|
.filter((x) => typeof x?.right === 'number'); |
|
}, [categoryData, positionMap, operatorName]); |
|
|
|
useEffect(() => { |
|
|
|
setPositionMap((state) => { |
|
const categoryDataKeys = Object.keys(categoryData); |
|
const stateKeys = Object.keys(state); |
|
if (!isKeysEqual(categoryDataKeys, stateKeys)) { |
|
const { newPositionMap, intersectionKeys } = buildNewPositionMap( |
|
categoryDataKeys, |
|
state, |
|
); |
|
|
|
const nextPositionMap = { |
|
...pick(state, intersectionKeys), |
|
...newPositionMap, |
|
}; |
|
|
|
return nextPositionMap; |
|
} |
|
return state; |
|
}); |
|
}, [categoryData]); |
|
|
|
useEffect(() => { |
|
updateNodeInternals(id); |
|
}, [id, updateNodeInternals, positionMap]); |
|
|
|
return { positions }; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|