From b9ec783b6bd09b93d057986fcbc964a495652675 Mon Sep 17 00:00:00 2001 From: moklick Date: Tue, 7 May 2024 13:19:54 +0200 Subject: [PATCH] fix(react): dont show nodeTypes warning if not necessary #3923 --- .../GraphView/useNodeOrEdgeTypesWarning.ts | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/react/src/container/GraphView/useNodeOrEdgeTypesWarning.ts b/packages/react/src/container/GraphView/useNodeOrEdgeTypesWarning.ts index 1c2ef365..828b37ac 100644 --- a/packages/react/src/container/GraphView/useNodeOrEdgeTypesWarning.ts +++ b/packages/react/src/container/GraphView/useNodeOrEdgeTypesWarning.ts @@ -1,27 +1,36 @@ import { useEffect, useRef } from 'react'; import { errorMessages } from '@xyflow/system'; -import type { EdgeTypes, NodeTypes } from '../../types'; import { useStoreApi } from '../../hooks/useStore'; +import type { EdgeTypes, NodeTypes } from '../../types'; const emptyTypes = {}; -/* - * This hook warns the user if node or edgeTypes change. +/** + * This hook warns the user if nodeTypes or edgeTypes changed. + * It is only used in development mode. + * + * @internal */ export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: NodeTypes): void; export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: EdgeTypes): void; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes: any = emptyTypes): any { - const updateCount = useRef(0); + const typesRef = useRef(nodeOrEdgeTypes); const store = useStoreApi(); useEffect(() => { if (process.env.NODE_ENV === 'development') { - if (updateCount.current > 1) { - store.getState().onError?.('002', errorMessages['error002']()); + const usedKeys = new Set([...Object.keys(typesRef.current), ...Object.keys(nodeOrEdgeTypes)]); + + for (const key of usedKeys) { + if (typesRef.current[key] !== nodeOrEdgeTypes[key]) { + store.getState().onError?.('002', errorMessages['error002']()); + break; + } } - updateCount.current += 1; + + typesRef.current = nodeOrEdgeTypes; } }, [nodeOrEdgeTypes]); }