diff options
Diffstat (limited to 'packages/ui/memory-graph/memory-graph.tsx')
| -rw-r--r-- | packages/ui/memory-graph/memory-graph.tsx | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/packages/ui/memory-graph/memory-graph.tsx b/packages/ui/memory-graph/memory-graph.tsx index 912a741a..72ddf090 100644 --- a/packages/ui/memory-graph/memory-graph.tsx +++ b/packages/ui/memory-graph/memory-graph.tsx @@ -157,8 +157,8 @@ export const MemoryGraph = ({ const spaceSet = new Set<string>(); const counts: Record<string, number> = {}; - data.documents.forEach((doc) => { - doc.memoryEntries.forEach((memory) => { + (data.documents || []).forEach((doc) => { + (doc.memoryEntries || []).forEach((memory) => { const spaceId = memory.spaceContainerTag || memory.spaceId || "default"; spaceSet.add(spaceId); counts[spaceId] = (counts[spaceId] || 0) + 1; @@ -199,32 +199,47 @@ export const MemoryGraph = ({ const handleCenter = useCallback(() => { if (nodes.length > 0) { // Calculate center of all nodes - let sumX = 0 - let sumY = 0 - let count = 0 - + let sumX = 0; + let sumY = 0; + let count = 0; + nodes.forEach((node) => { - sumX += node.x - sumY += node.y - count++ - }) - + sumX += node.x; + sumY += node.y; + count++; + }); + if (count > 0) { - const centerX = sumX / count - const centerY = sumY / count - centerViewportOn(centerX, centerY, containerSize.width, containerSize.height) + const centerX = sumX / count; + const centerY = sumY / count; + centerViewportOn( + centerX, + centerY, + containerSize.width, + containerSize.height, + ); } } - }, [nodes, centerViewportOn, containerSize.width, containerSize.height]) + }, [nodes, centerViewportOn, containerSize.width, containerSize.height]); const handleAutoFit = useCallback(() => { - if (nodes.length > 0 && containerSize.width > 0 && containerSize.height > 0) { + if ( + nodes.length > 0 && + containerSize.width > 0 && + containerSize.height > 0 + ) { autoFitToViewport(nodes, containerSize.width, containerSize.height, { occludedRightPx, animate: true, - }) + }); } - }, [nodes, containerSize.width, containerSize.height, occludedRightPx, autoFitToViewport]) + }, [ + nodes, + containerSize.width, + containerSize.height, + occludedRightPx, + autoFitToViewport, + ]); // Get selected node data const selectedNodeData = useMemo(() => { @@ -251,7 +266,7 @@ export const MemoryGraph = ({ }; // Count visible documents - const visibleDocuments = data.documents.filter((doc) => { + const visibleDocuments = (data.documents || []).filter((doc) => { const docNodes = nodes.filter( (node) => node.type === "document" && node.data.id === doc.id, ); @@ -265,7 +280,8 @@ export const MemoryGraph = ({ }); // If 80% or more of documents are visible, load more - const visibilityRatio = visibleDocuments.length / data.documents.length; + const visibilityRatio = + visibleDocuments.length / (data.documents || []).length; if (visibilityRatio >= 0.8) { loadMoreDocuments(); } @@ -421,8 +437,12 @@ export const MemoryGraph = ({ {containerSize.width > 0 && ( <NavigationControls onCenter={handleCenter} - onZoomIn={() => zoomIn(containerSize.width / 2, containerSize.height / 2)} - onZoomOut={() => zoomOut(containerSize.width / 2, containerSize.height / 2)} + onZoomIn={() => + zoomIn(containerSize.width / 2, containerSize.height / 2) + } + onZoomOut={() => + zoomOut(containerSize.width / 2, containerSize.height / 2) + } onAutoFit={handleAutoFit} nodes={nodes} className="absolute bottom-4 left-4" |