File size: 2,892 Bytes
16f8eca 9cded99 16f8eca 8828184 16f8eca 9cded99 16f8eca 9cded99 16f8eca 9cded99 16f8eca 9cded99 8828184 9cded99 16f8eca 9cded99 16f8eca 9cded99 16f8eca 9cded99 16f8eca 9cded99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
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(() => {
// Cache used coordinates
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 };
};
// 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 };
// };
|