2024-08-19 19:00:04 +08:00
|
|
|
import get from 'lodash/get';
|
|
|
|
|
import pick from 'lodash/pick';
|
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import { useUpdateNodeInternals } from 'reactflow';
|
2024-08-20 16:08:53 +08:00
|
|
|
import { Operator } from '../../constant';
|
2024-08-19 19:00:04 +08:00
|
|
|
import { IPosition, NodeData } from '../../interface';
|
2024-08-20 19:27:49 +08:00
|
|
|
import {
|
|
|
|
|
buildNewPositionMap,
|
|
|
|
|
generateSwitchHandleText,
|
|
|
|
|
isKeysEqual,
|
|
|
|
|
} from '../../utils';
|
2024-08-19 19:00:04 +08:00
|
|
|
|
|
|
|
|
export const useBuildCategorizeHandlePositions = ({
|
|
|
|
|
data,
|
|
|
|
|
id,
|
|
|
|
|
}: {
|
|
|
|
|
id: string;
|
|
|
|
|
data: NodeData;
|
|
|
|
|
}) => {
|
2024-08-20 16:08:53 +08:00
|
|
|
const operatorName = data.label as Operator;
|
2024-08-19 19:00:04 +08:00
|
|
|
const updateNodeInternals = useUpdateNodeInternals();
|
|
|
|
|
const [positionMap, setPositionMap] = useState<Record<string, IPosition>>({});
|
2024-08-20 16:08:53 +08:00
|
|
|
|
|
|
|
|
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]);
|
2024-08-19 19:00:04 +08:00
|
|
|
|
|
|
|
|
const positions = useMemo(() => {
|
|
|
|
|
return Object.keys(categoryData)
|
2024-08-20 16:08:53 +08:00
|
|
|
.map((x, idx) => {
|
2024-08-19 19:00:04 +08:00
|
|
|
const position = positionMap[x];
|
2024-08-20 16:08:53 +08:00
|
|
|
let text = x;
|
|
|
|
|
if (operatorName === Operator.Switch) {
|
2024-08-20 19:27:49 +08:00
|
|
|
text = generateSwitchHandleText(idx);
|
2024-08-20 16:08:53 +08:00
|
|
|
}
|
|
|
|
|
return { text, ...position };
|
2024-08-19 19:00:04 +08:00
|
|
|
})
|
|
|
|
|
.filter((x) => typeof x?.right === 'number');
|
2024-08-20 16:08:53 +08:00
|
|
|
}, [categoryData, positionMap, operatorName]);
|
2024-08-19 19:00:04 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Cache used coordinates
|
|
|
|
|
setPositionMap((state) => {
|
|
|
|
|
const categoryDataKeys = Object.keys(categoryData);
|
|
|
|
|
const stateKeys = Object.keys(state);
|
2024-08-20 16:08:53 +08:00
|
|
|
if (!isKeysEqual(categoryDataKeys, stateKeys)) {
|
|
|
|
|
const { newPositionMap, intersectionKeys } = buildNewPositionMap(
|
2024-08-19 19:00:04 +08:00
|
|
|
categoryDataKeys,
|
2024-08-20 16:08:53 +08:00
|
|
|
state,
|
2024-08-19 19:00:04 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const nextPositionMap = {
|
|
|
|
|
...pick(state, intersectionKeys),
|
|
|
|
|
...newPositionMap,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return nextPositionMap;
|
|
|
|
|
}
|
|
|
|
|
return state;
|
|
|
|
|
});
|
|
|
|
|
}, [categoryData]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
updateNodeInternals(id);
|
|
|
|
|
}, [id, updateNodeInternals, positionMap]);
|
|
|
|
|
|
|
|
|
|
return { positions };
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-20 16:08:53 +08:00
|
|
|
// export const useBuildSwitchHandlePositions = ({
|
|
|
|
|
// data,
|
|
|
|
|
// id,
|
|
|
|
|
// }: {
|
|
|
|
|
// id: string;
|
|
|
|
|
// data: NodeData;
|
|
|
|
|
// }) => {
|
|
|
|
|
// const [positionMap, setPositionMap] = useState<Record<string, IPosition>>({});
|
|
|
|
|
// const conditions = useMemo(() => get(data, 'form.conditions', []), [data]);
|
|
|
|
|
// const updateNodeInternals = useUpdateNodeInternals();
|
|
|
|
|
|
|
|
|
|
// const positions = useMemo(() => {
|
|
|
|
|
// return conditions
|
|
|
|
|
// .map((x, idx) => {
|
|
|
|
|
// const text = `Item ${idx}`;
|
|
|
|
|
// const position = positionMap[text];
|
|
|
|
|
// return { text: text, ...position };
|
|
|
|
|
// })
|
|
|
|
|
// .filter((x) => typeof x?.right === 'number');
|
|
|
|
|
// }, [conditions, positionMap]);
|
|
|
|
|
|
|
|
|
|
// useEffect(() => {
|
|
|
|
|
// updateNodeInternals(id);
|
|
|
|
|
// }, [id, updateNodeInternals, positionMap]);
|
|
|
|
|
|
|
|
|
|
// return { positions };
|
|
|
|
|
// };
|