feat(hooks): add useNodesData, useUpdateNodeData, simplify useHandleConnections

This commit is contained in:
moklick
2023-12-07 13:23:25 +01:00
parent a58f915697
commit 959935dfb5
12 changed files with 246 additions and 71 deletions
+33
View File
@@ -0,0 +1,33 @@
import { useCallback } from 'react';
import { shallow } from 'zustand/shallow';
import { useStore } from '../hooks/useStore';
export function useNodesData<NodeData = unknown>(nodeId: string): NodeData | null;
export function useNodesData<NodeData = unknown>(nodeIds: string[]): NodeData[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useNodesData(nodeIds: any): any {
const nodesData = useStore(
useCallback(
(s) => {
if (!Array.isArray(nodeIds)) {
return s.nodeLookup.get(nodeIds)?.data || null;
}
return nodeIds.reduce((res, id) => {
const node = s.nodeLookup.get(id);
if (node) {
res.push(node.data);
}
return res;
}, []);
},
[nodeIds]
),
shallow
);
return nodesData;
}