File size: 2,163 Bytes
16f8eca |
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 |
import get from 'lodash/get';
import intersectionWith from 'lodash/intersectionWith';
import isEqual from 'lodash/isEqual';
import pick from 'lodash/pick';
import { useEffect, useMemo, useState } from 'react';
import { useUpdateNodeInternals } from 'reactflow';
import { IPosition, NodeData } from '../../interface';
import { buildNewPositionMap } from '../../utils';
export const useBuildCategorizeHandlePositions = ({
data,
id,
}: {
id: string;
data: NodeData;
}) => {
const updateNodeInternals = useUpdateNodeInternals();
const [positionMap, setPositionMap] = useState<Record<string, IPosition>>({});
const categoryData = useMemo(
() => get(data, 'form.category_description') ?? {},
[data],
);
const positions = useMemo(() => {
return Object.keys(categoryData)
.map((x) => {
const position = positionMap[x];
return { text: x, ...position };
})
.filter((x) => typeof x?.right === 'number');
}, [categoryData, positionMap]);
useEffect(() => {
// Cache used coordinates
setPositionMap((state) => {
// index in use
const indexesInUse = Object.values(state).map((x) => x.idx);
const categoryDataKeys = Object.keys(categoryData);
const stateKeys = Object.keys(state);
if (!isEqual(categoryDataKeys.sort(), stateKeys.sort())) {
const intersectionKeys = intersectionWith(
stateKeys,
categoryDataKeys,
(categoryDataKey, postionMapKey) => categoryDataKey === postionMapKey,
);
const newPositionMap = buildNewPositionMap(
categoryDataKeys.filter(
(x) => !intersectionKeys.some((y) => y === x),
),
indexesInUse,
);
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;
}) => {};
|