refactor(react): shorter useHandleConnections params
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import { memo, useEffect } from 'react';
|
import { memo } from 'react';
|
||||||
import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react';
|
import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react';
|
||||||
|
|
||||||
function ResultNode() {
|
function ResultNode() {
|
||||||
const connections = useHandleConnections({
|
const connections = useHandleConnections({
|
||||||
handleType: 'target',
|
type: 'target',
|
||||||
});
|
});
|
||||||
const nodesData = useNodesData(connections.map((connection) => connection.source));
|
const nodesData = useNodesData(connections.map((connection) => connection.source));
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Position, NodeProps, useUpdateNodeData, Handle, useHandleConnections, u
|
|||||||
|
|
||||||
function UppercaseNode({ id }: NodeProps) {
|
function UppercaseNode({ id }: NodeProps) {
|
||||||
const connections = useHandleConnections({
|
const connections = useHandleConnections({
|
||||||
handleType: 'target',
|
type: 'target',
|
||||||
});
|
});
|
||||||
const nodeData = useNodesData(connections[0]?.source);
|
const nodeData = useNodesData(connections[0]?.source);
|
||||||
const updateNodeData = useUpdateNodeData();
|
const updateNodeData = useUpdateNodeData();
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import { useStore } from './useStore';
|
|||||||
import { useNodeId } from '../contexts/NodeIdContext';
|
import { useNodeId } from '../contexts/NodeIdContext';
|
||||||
|
|
||||||
type useHandleConnectionsParams = {
|
type useHandleConnectionsParams = {
|
||||||
handleType: HandleType;
|
type: HandleType;
|
||||||
|
id?: string | null;
|
||||||
nodeId?: string;
|
nodeId?: string;
|
||||||
handleId?: string | null;
|
|
||||||
onConnect?: (connections: Connection[]) => void;
|
onConnect?: (connections: Connection[]) => void;
|
||||||
onDisconnect?: (connections: Connection[]) => void;
|
onDisconnect?: (connections: Connection[]) => void;
|
||||||
};
|
};
|
||||||
@@ -16,16 +16,16 @@ type useHandleConnectionsParams = {
|
|||||||
* Hook to check if a <Handle /> is connected to another <Handle /> and get the connections.
|
* Hook to check if a <Handle /> is connected to another <Handle /> and get the connections.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @param param.handleType - 'source' or 'target'
|
* @param param.type - handle type 'source' or 'target'
|
||||||
* @param param.handleId - the handle id (this is only needed if the node has multiple handles of the same type)
|
* @param param.id - the handle id (this is only needed if the node has multiple handles of the same type)
|
||||||
* @param param.nodeId - node id - if not provided, the node id from the NodeIdContext is used
|
* @param param.nodeId - node id - if not provided, the node id from the NodeIdContext is used
|
||||||
* @param param.onConnect - gets called when a connection is established
|
* @param param.onConnect - gets called when a connection is established
|
||||||
* @param param.onDisconnect - gets called when a connection is removed
|
* @param param.onDisconnect - gets called when a connection is removed
|
||||||
* @returns an array with connections
|
* @returns an array with connections
|
||||||
*/
|
*/
|
||||||
export function useHandleConnections({
|
export function useHandleConnections({
|
||||||
handleType,
|
type,
|
||||||
handleId = null,
|
id = null,
|
||||||
nodeId,
|
nodeId,
|
||||||
onConnect,
|
onConnect,
|
||||||
onDisconnect,
|
onDisconnect,
|
||||||
@@ -35,7 +35,7 @@ export function useHandleConnections({
|
|||||||
const currentNodeId = nodeId || _nodeId;
|
const currentNodeId = nodeId || _nodeId;
|
||||||
|
|
||||||
const connections = useStore(
|
const connections = useStore(
|
||||||
(state) => state.connectionLookup.get(`${currentNodeId}-${handleType}-${handleId}`),
|
(state) => state.connectionLookup.get(`${currentNodeId}-${type}-${id}`),
|
||||||
areConnectionMapsEqual
|
areConnectionMapsEqual
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user