Skip to content

Commit d1ed6c1

Browse files
authored
Merge pull request #359 from FalkorDB/fix-collapse
Fix #357 fix collapse
2 parents e4f1d2b + 9799714 commit d1ed6c1

2 files changed

Lines changed: 25 additions & 40 deletions

File tree

app/components/code-graph.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,38 @@ export function CodeGraph({
167167
}
168168

169169
const deleteNeighbors = (nodes: Node[]) => {
170+
170171
if (nodes.length === 0) return;
171-
172+
173+
const expandedNodes: Node[] = []
174+
172175
graph.Elements = {
173-
nodes: graph.Elements.nodes.map(node => {
176+
nodes: graph.Elements.nodes.filter(node => {
177+
if (!node.collapsed) return true
178+
174179
const isTarget = graph.Elements.links.some(link => link.target.id === node.id && nodes.some(n => n.id === link.source.id));
180+
181+
debugger
182+
183+
if (!isTarget) return true
175184

176-
if (!isTarget || !node.collapsed) return node
185+
const deleted = graph.NodesMap.delete(Number(node.id))
177186

178-
if (node.expand) {
179-
node.expand = false
180-
deleteNeighbors([node])
187+
if (deleted && node.expand) {
188+
expandedNodes.push(node)
181189
}
182190

183-
graph.NodesMap.delete(Number(node.id))
184-
}).filter(node => node !== undefined),
191+
return false
192+
}),
185193
links: graph.Elements.links
186194
}
195+
196+
deleteNeighbors(expandedNodes)
187197

188198
graph.removeLinks()
189199
}
190200

191201
const handleExpand = async (nodes: Node[], expand: boolean) => {
192-
193202
if (expand) {
194203
const elements = await onFetchNode(nodes.map(n => n.id))
195204

@@ -219,7 +228,6 @@ export function CodeGraph({
219228
const chart = chartRef.current
220229

221230
if (chart) {
222-
const n = { name: node.properties.name, id: node.id }
223231

224232
let chartNode = graph.Elements.nodes.find(n => n.id == node.id)
225233

@@ -234,8 +242,8 @@ export function CodeGraph({
234242
graph.visibleLinks(true, [chartNode!.id])
235243
setData({ ...graph.Elements })
236244
}
237-
238-
setSearchNode(n)
245+
246+
setSearchNode(chartNode)
239247
setTimeout(() => {
240248
chart.zoomToFit(1000, 150, (n: NodeObject<Node>) => n.id === chartNode!.id);
241249
}, 0)
@@ -299,8 +307,7 @@ export function CodeGraph({
299307
<div className='flex gap-4'>
300308
<Input
301309
graph={graph}
302-
value={searchNode.name || ""}
303-
onValueChange={({ name }) => setSearchNode({ name })}
310+
onValueChange={(node) => setSearchNode(node)}
304311
icon={<Search />}
305312
handleSubmit={handleSearchSubmit}
306313
node={searchNode}
@@ -405,7 +412,7 @@ export function CodeGraph({
405412
setSelectedObjects={setSelectedObjects}
406413
setPosition={setPosition}
407414
onFetchNode={onFetchNode}
408-
deleteNeighbors={deleteNeighbors}
415+
handleExpand={handleExpand}
409416
isShowPath={isShowPath}
410417
setPath={setPath}
411418
isPathResponse={isPathResponse}

app/components/graphView.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import ForceGraph2D from 'react-force-graph-2d';
33
import { Graph, GraphData, Link, Node } from './model';
44
import { Dispatch, RefObject, SetStateAction, useEffect, useRef, useState } from 'react';
5-
import { toast } from '@/components/ui/use-toast';
65
import { Path } from '../page';
76

87
export interface Position {
@@ -21,7 +20,7 @@ interface Props {
2120
setSelectedObjects: Dispatch<SetStateAction<Node[]>>
2221
setPosition: Dispatch<SetStateAction<Position | undefined>>
2322
onFetchNode: (nodeIds: number[]) => Promise<GraphData>
24-
deleteNeighbors: (nodes: Node[]) => void
23+
handleExpand: (nodes: Node[], expand: boolean) => void
2524
isShowPath: boolean
2625
setPath: Dispatch<SetStateAction<Path | undefined>>
2726
isPathResponse: boolean | undefined
@@ -48,7 +47,7 @@ export default function GraphView({
4847
setSelectedObjects,
4948
setPosition,
5049
onFetchNode,
51-
deleteNeighbors,
50+
handleExpand,
5251
isShowPath,
5352
setPath,
5453
isPathResponse,
@@ -132,28 +131,7 @@ export default function GraphView({
132131
lastClick.current = { date: now, name: node.name }
133132

134133
if (isDoubleClick) {
135-
const expand = !node.expand
136-
137-
if (expand) {
138-
const elements = await onFetchNode([node.id])
139-
140-
if (elements.nodes.length === 0) {
141-
toast({
142-
title: `No neighbors found`,
143-
description: `No neighbors found`,
144-
})
145-
146-
return
147-
}
148-
} else {
149-
deleteNeighbors([node]);
150-
}
151-
152-
node.expand = expand
153-
154-
setSelectedObj(undefined)
155-
setData({ ...graph.Elements })
156-
134+
handleExpand([node], !node.expand)
157135
} else if (isShowPath) {
158136
setPath(prev => {
159137
if (!prev?.start?.name || (prev.end?.name && prev.end?.name !== "")) {

0 commit comments

Comments
 (0)