From 3fc3b7e6038a19e6b8010161d2bffd322c13a928 Mon Sep 17 00:00:00 2001 From: moklick Date: Mon, 26 Feb 2024 14:13:12 +0100 Subject: [PATCH] chore(useNodesData): cleanup return type --- .../src/examples/UseNodesData/ResultNode.tsx | 6 ++---- .../react/src/examples/UseNodesData/index.tsx | 4 ++-- .../routes/examples/usenodesdata/+page.svelte | 21 ++++++++++++------- .../examples/usenodesdata/ResultNode.svelte | 4 ++-- packages/react/src/hooks/useNodesData.ts | 14 ++----------- packages/svelte/src/lib/hooks/useNodesData.ts | 18 +++------------- 6 files changed, 24 insertions(+), 43 deletions(-) diff --git a/examples/react/src/examples/UseNodesData/ResultNode.tsx b/examples/react/src/examples/UseNodesData/ResultNode.tsx index 006af8b7..bf8f7a35 100644 --- a/examples/react/src/examples/UseNodesData/ResultNode.tsx +++ b/examples/react/src/examples/UseNodesData/ResultNode.tsx @@ -1,15 +1,13 @@ import { memo } from 'react'; import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react'; -import { TextNode, isTextNode, type MyNode } from '.'; +import { isTextNode, type MyNode } from '.'; function ResultNode() { const connections = useHandleConnections({ type: 'target', }); const nodesData = useNodesData(connections.map((connection) => connection.source)); - const textNodes = nodesData.filter(isTextNode) as TextNode[]; - - textNodes[0].data; + const textNodes = nodesData.filter(isTextNode); return (
diff --git a/examples/react/src/examples/UseNodesData/index.tsx b/examples/react/src/examples/UseNodesData/index.tsx index dc0b43dd..91cc1f94 100644 --- a/examples/react/src/examples/UseNodesData/index.tsx +++ b/examples/react/src/examples/UseNodesData/index.tsx @@ -20,8 +20,8 @@ export type ResultNode = Node<{}, 'result'>; export type UppercaseNode = Node<{ text: string }, 'uppercase'>; export type MyNode = TextNode | ResultNode | UppercaseNode; -export function isTextNode(node: any): node is TextNode | UppercaseNode { - return node.type === 'text' || node.type === 'uppercase'; +export function isTextNode(node: any): node is TextNode { + return node.type === 'text'; } const nodeTypes = { diff --git a/examples/svelte/src/routes/examples/usenodesdata/+page.svelte b/examples/svelte/src/routes/examples/usenodesdata/+page.svelte index a92acbe4..fc85ee26 100644 --- a/examples/svelte/src/routes/examples/usenodesdata/+page.svelte +++ b/examples/svelte/src/routes/examples/usenodesdata/+page.svelte @@ -1,13 +1,15 @@ diff --git a/packages/react/src/hooks/useNodesData.ts b/packages/react/src/hooks/useNodesData.ts index 6d9586ed..c43a60cc 100644 --- a/packages/react/src/hooks/useNodesData.ts +++ b/packages/react/src/hooks/useNodesData.ts @@ -20,18 +20,8 @@ export interface NodeDataReturn { */ export function useNodesData( nodeId: string -): { - id: string; - type: NodeType['type']; - data: NodeType['data']; -} | null; -export function useNodesData( - nodeIds: string[] -): { - id: string; - type: NodeType['type']; - data: NodeType['data']; -}[]; +): Pick | null; +export function useNodesData(nodeIds: string[]): Pick[]; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function useNodesData(nodeIds: any): any { const nodesData = useStore( diff --git a/packages/svelte/src/lib/hooks/useNodesData.ts b/packages/svelte/src/lib/hooks/useNodesData.ts index 1ec53f87..c11ef308 100644 --- a/packages/svelte/src/lib/hooks/useNodesData.ts +++ b/packages/svelte/src/lib/hooks/useNodesData.ts @@ -22,33 +22,21 @@ function areNodesDataEqual(a: any[], b: any[]) { * * @public * @param nodeId - The id (or ids) of the node to get the data from - * @param guard - Optional guard function to narrow down the node type * @returns A readable store with an array of data objects */ export function useNodesData( nodeId: string -): Readable<{ - id: string; - type: NodeType['type']; - data: NodeType['data']; -} | null>; +): Readable | null>; export function useNodesData( nodeIds: string[] -): Readable< - { - id: string; - type: NodeType['type']; - data: NodeType['data']; - }[] ->; +): Readable[]>; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function useNodesData(nodeIds: any): any { const { nodes, nodeLookup } = useStore(); let prevNodesData: any = []; return derived([nodes, nodeLookup], ([, nodeLookup], set) => { - let nextNodesData = []; - + const nextNodesData = []; const isArrayOfIds = Array.isArray(nodeIds); const _nodeIds = isArrayOfIds ? nodeIds : [nodeIds];