From 216f6495c467aa8e3bd0ab809a1a50120eeeaef4 Mon Sep 17 00:00:00 2001 From: balibabu Date: Sat, 3 Aug 2024 06:42:48 +0800 Subject: [PATCH] feat: Fixed the issue where the page reports an error when the graph returned by the interface is empty #162 (#1795) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …returned by the interface is empty #162 ### What problem does this PR solve? feat: Fixed the issue where the page reports an error when the graph returned by the interface is empty #162 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- .../components/knowledge-graph/force-graph.tsx | 5 +++-- .../knowledge-chunk/components/knowledge-graph/util.ts | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/force-graph.tsx b/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/force-graph.tsx index 45bfb82ce0..4c6c427583 100644 --- a/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/force-graph.tsx +++ b/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/force-graph.tsx @@ -1,4 +1,5 @@ import { ElementDatum, Graph, IElementEvent } from '@antv/g6'; +import isEmpty from 'lodash/isEmpty'; import { useCallback, useEffect, useMemo, useRef } from 'react'; import { buildNodesAndCombos } from './util'; @@ -20,7 +21,7 @@ const ForceGraph = ({ data, show }: IProps) => { const graphRef = useRef(null); const nextData = useMemo(() => { - if (data) { + if (!isEmpty(data)) { const graphData = data; const mi = buildNodesAndCombos(graphData.nodes); return { edges: graphData.links, ...mi }; @@ -116,7 +117,7 @@ const ForceGraph = ({ data, show }: IProps) => { }, [nextData]); useEffect(() => { - if (data) { + if (!isEmpty(data)) { render(); } }, [data, render]); diff --git a/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/util.ts b/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/util.ts index 7aa41f2626..eff77479e5 100644 --- a/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/util.ts +++ b/web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/util.ts @@ -1,3 +1,5 @@ +import { isEmpty } from 'lodash'; + class KeyGenerator { idx = 0; chars: string[] = []; @@ -55,7 +57,9 @@ export class Converter { } export const isDataExist = (data: any) => { - return data?.data && typeof data?.data !== 'boolean'; + return ( + data?.data && typeof data?.data !== 'boolean' && !isEmpty(data?.data?.graph) + ); }; export const buildNodesAndCombos = (nodes: any[]) => {