From bfbcdcb06abd1973e9ae943d03b7f77871f8870e Mon Sep 17 00:00:00 2001 From: yangdx Date: Sat, 15 Mar 2025 20:40:22 +0800 Subject: [PATCH] fix: improve graph zoom control and node expansion functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Fix container padding in zoom calculation • Extract node size update into helper fn • Update node sizes for empty expansions --- .../src/components/graph/ZoomControl.tsx | 5 +- lightrag_webui/src/hooks/useLightragGraph.tsx | 48 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lightrag_webui/src/components/graph/ZoomControl.tsx b/lightrag_webui/src/components/graph/ZoomControl.tsx index ec313045..63e9ea6a 100644 --- a/lightrag_webui/src/components/graph/ZoomControl.tsx +++ b/lightrag_webui/src/components/graph/ZoomControl.tsx @@ -4,7 +4,6 @@ import Button from '@/components/ui/Button' import { ZoomInIcon, ZoomOutIcon, FullscreenIcon } from 'lucide-react' import { controlButtonVariant } from '@/lib/constants' import { useTranslation } from 'react-i18next'; -import { combine } from 'zustand/middleware' /** * Component that provides zoom controls for the graph viewer. @@ -67,8 +66,8 @@ const ZoomControl = () => { // Calculate base scale const scale = Math.min( - (containerWidth - containerPadding) / width, - (containerHeight - containerPadding) / height + (containerWidth - containerPadding * 2) / width, + (containerHeight - containerPadding * 2) / height ) // Apply scaling factor (just don't know why) const ratio = (1 / scale) * 10 diff --git a/lightrag_webui/src/hooks/useLightragGraph.tsx b/lightrag_webui/src/hooks/useLightragGraph.tsx index e9440471..3268b04b 100644 --- a/lightrag_webui/src/hooks/useLightragGraph.tsx +++ b/lightrag_webui/src/hooks/useLightragGraph.tsx @@ -475,8 +475,35 @@ const useLightrangeGraph = () => { } } + // Helper function to update node sizes + const updateNodeSizes = ( + sigmaGraph: DirectedGraph, + nodesWithDiscardedEdges: Set, + minDegree: number, + range: number, + scale: number + ) => { + for (const nodeId of nodesWithDiscardedEdges) { + if (sigmaGraph.hasNode(nodeId)) { + let newDegree = sigmaGraph.degree(nodeId); + newDegree += 1; // Add +1 for discarded edges + + const newSize = Math.round( + Constants.minNodeSize + scale * Math.pow((newDegree - minDegree) / range, 0.5) + ); + + const currentSize = sigmaGraph.getNodeAttribute(nodeId, 'size'); + + if (newSize > currentSize) { + sigmaGraph.setNodeAttribute(nodeId, 'size', newSize); + } + } + } + }; + // If no new connectable nodes found, show toast and return if (nodesToAdd.size === 0) { + updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, range, scale); toast.info(t('graphPanel.propertiesView.node.noNewNodes')); return; } @@ -577,26 +604,7 @@ const useLightrangeGraph = () => { searchCache.searchEngine = null; // Update sizes for all nodes with discarded edges - for (const nodeId of nodesWithDiscardedEdges) { - if (sigmaGraph.hasNode(nodeId)) { - // Get the new degree of the node - let newDegree = sigmaGraph.degree(nodeId); - newDegree += 1; // Add +1 for discarded edges - - // Calculate new size for the node - const newSize = Math.round( - Constants.minNodeSize + scale * Math.pow((newDegree - minDegree) / range, 0.5) - ); - - // Get current size - const currentSize = sigmaGraph.getNodeAttribute(nodeId, 'size'); - - // Only update if new size is larger - if (newSize > currentSize) { - sigmaGraph.setNodeAttribute(nodeId, 'size', newSize); - } - } - } + updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, range, scale); } catch (error) { console.error('Error expanding node:', error);