Skip to content

Commit 3394297

Browse files
committed
Fix add-to-graph
1 parent 71ed5db commit 3394297

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/components/graph/RelationsGraphProvider.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ export function RelationsGraphProvider(
3131

3232
const openOrAddToRelationsGraph = useCallback((nodes: GraphNode[], options?: RelationsGraphOptions) => {
3333
setState((prev) => ({
34-
nodes: prev.nodes
35-
.concat(nodes)
36-
.reduce<GraphNode[]>((acc, node) => (acc.find((inner) => inner.id === node.id) ? acc : acc.concat(node)), []),
34+
nodes: prev.nodes.concat(nodes).reduce<GraphNode[]>((acc, node) => {
35+
const existingIndex = acc.findIndex((inner) => inner.id === node.id)
36+
const existing = existingIndex >= 0 ? acc[existingIndex] : null
37+
if (existing) {
38+
existing.in = existing.in.concat(node.in)
39+
existing.out = existing.out.concat(node.out)
40+
existing.data = node.data
41+
42+
acc[existingIndex] = { ...existing }
43+
return acc
44+
} else return acc.concat(node)
45+
}, []),
3746
isOpen: true,
3847
options: options ?? prev.options ?? {}
3948
}))
4049
}, [])
4150

4251
const onRelationsGraphOpenChange = useCallback((isOpen: boolean) => {
43-
setState((prev) => ({ ...prev, isOpen }))
52+
setState(() => ({ isOpen, nodes: [], options: {} }))
4453
}, [])
4554

4655
const closeRelationsGraph = useCallback(() => {

src/components/result/GenericResultView.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function GenericResultView({
132132
showOpenInFairDoScope = true,
133133
showInspectFDO = true
134134
}: GenericResultViewProps) {
135-
const { openRelationsGraph } = useContext(RelationsGraphContext)
135+
const { openOrAddToRelationsGraph } = useContext(RelationsGraphContext)
136136
const { searchTerm, elasticConnector, searchFor, config } = useContext(FairDOSearchContext)
137137
const addToResultCache = useStore(resultCache, (s) => s.set)
138138
const [loadingRelatedItems, setLoadingRelatedItems] = useState(false)
@@ -300,10 +300,10 @@ export function GenericResultView({
300300
setLoadingRelatedItems(false)
301301

302302
const nodes = GraphNodeUtils.buildSequentialGraphFromIds("result", hasMetadata ?? [], pid, isMetadataFor ?? [])
303-
openRelationsGraph(nodes, {
303+
openOrAddToRelationsGraph(nodes, {
304304
focusedNodes: [pid]
305305
})
306-
}, [fetchRelatedItems, hasMetadata, isMetadataFor, openRelationsGraph, pid])
306+
}, [fetchRelatedItems, hasMetadata, isMetadataFor, openOrAddToRelationsGraph, pid])
307307

308308
const showRelatedItemsButton = useMemo(() => {
309309
return (hasMetadata && hasMetadata.length > 0) || (isMetadataFor && isMetadataFor.length > 0)

0 commit comments

Comments
 (0)