1- import { Button , Dialog , Flex } from '@neo4j-ndl/react' ;
1+ import { Button , Dialog , LoadingSpinner } from '@neo4j-ndl/react' ;
22import { useEffect , useRef , useState } from 'react' ;
33import { GraphViewModalProps } from '../types' ;
44import { InteractiveNvlWrapper } from '@neo4j-nvl/react' ;
55import NVL , { NvlOptions } from '@neo4j-nvl/core' ;
66import { driver } from '../utils/Driver' ;
77import GraphDropdown from '../HOC/CustomDropdown' ;
8- import { useFileContext } from '../context/UsersFiles' ;
98
109const uniqueElementsForDocQuery = `
1110// Finds a document with chunks & entities. Transforms path objects to return a list of unique nodes and relationships.
@@ -19,6 +18,55 @@ UNWIND rels as rel
1918WITH nodes, collect(DISTINCT rel) as rels
2019RETURN nodes, rels`
2120
21+ const pureDocument = `
22+ MATCH (d:Document)
23+ WITH d ORDER BY d.createdAt DESC
24+ MATCH files = (d)<-[part:PART_OF]-(c:Chunk)
25+ WITH nodes(files) as nodes, relationships(files) as rels
26+ UNWIND nodes as node
27+ WITH collect(DISTINCT node) as nodes, collect(rels) as relslist
28+ UNWIND relslist as rels
29+ UNWIND rels as rel
30+ WITH nodes, collect(DISTINCT rel) as rels
31+ RETURN nodes, rels,
32+ collect { MATCH p=(c)-[:NEXT_CHUNK]-() RETURN p } as chain,
33+ collect { MATCH p=(c)-[:SIMILAR]-() RETURN p } as chunks;
34+ `
35+
36+
37+ const docEntitiesGraph = `
38+ MATCH (d:Document)
39+ WITH d ORDER BY d.createdAt DESC
40+ MATCH files = (d)<-[:PART_OF]-(c:Chunk)
41+ OPTIONAL MATCH p=(d)<-[:PART_OF]-(c:Chunk)-[:HAS_ENTITY]->(e)--(:!Chunk)
42+ WITH nodes(p) as nodes, relationships(p) as rels
43+ UNWIND nodes as node
44+ WITH collect(DISTINCT node) as nodes, collect(rels) as relslist
45+ UNWIND relslist as rels
46+ UNWIND rels as rel
47+ WITH nodes, collect(DISTINCT rel) as rels
48+ RETURN nodes, rels,
49+ collect { MATCH p=(c)-[:NEXT_CHUNK]-() RETURN p } as chain,
50+ collect { MATCH p=(c)-[:SIMILAR]-() RETURN p } as chunks,
51+ collect { OPTIONAL MATCH p=(c)-[:HAS_ENTITY]->(e)--(:!Chunk) RETURN p } as entities;`
52+
53+
54+ const knowledgeGraph = `
55+ MATCH (d:Document)
56+ WITH d ORDER BY d.createdAt DESC
57+ LIMIT 50
58+ MATCH files = (d)<-[:PART_OF]-(c:Chunk)
59+ RETURN
60+ collect { OPTIONAL MATCH p=(c)-[:HAS_ENTITY]->(e)--(:!Chunk) RETURN p } as entities`
61+
62+
63+ const queryMap : any = {
64+ 'Document Structure' : pureDocument ,
65+ 'Document & Knowledge Graph' : docEntitiesGraph ,
66+ 'Knowledge Graph Entities' : knowledgeGraph
67+ } ;
68+
69+
2270const colors = [
2371 '#588c7e' ,
2472 '#f2e394' ,
@@ -77,7 +125,8 @@ const GraphViewModal: React.FunctionComponent<GraphViewModalProps> = ({
77125 const nvlRef = useRef < NVL > ( null ) ;
78126 const [ nodes , setNodes ] = useState ( [ ] ) ;
79127 const [ relationships , setRelationships ] = useState ( [ ] ) ;
80- const { setGraphType } = useFileContext ( ) ;
128+ const [ graphType , setGraphType ] = useState < string > ( 'Document Structure' ) ;
129+ const [ loading , setLoading ] = useState < boolean > ( false ) ;
81130
82131 const handleDropdownChange = ( option : any ) => {
83132 setGraphType ( option . value ) ;
@@ -87,12 +136,20 @@ const GraphViewModal: React.FunctionComponent<GraphViewModalProps> = ({
87136 if ( open ) {
88137 setNodes ( [ ] ) ;
89138 setRelationships ( [ ] ) ;
139+ let queryToRun = '' ;
140+ if ( viewPoint === 'tableView' ) {
141+ queryToRun = uniqueElementsForDocQuery ;
142+ }
143+ else {
144+ queryToRun = queryMap [ graphType ]
145+ }
90146
91147 const session = driver . session ( ) ;
92- session . run ( uniqueElementsForDocQuery , { 'document_name' : inspectedName } ) . then (
148+ session . run ( queryToRun , { 'document_name' : inspectedName } ) . then (
93149 ( results ) => {
94150 // If this doc exists in the graph, the result length will be one.
95- if ( results . records . length == 1 ) {
151+ setLoading ( true ) ;
152+ if ( results . records . length >= 1 ) {
96153 const neo4jNodes = results . records [ 0 ] . _fields [ 0 ] ;
97154 const neo4jRels = results . records [ 0 ] . _fields [ 1 ] ;
98155
@@ -120,13 +177,16 @@ const GraphViewModal: React.FunctionComponent<GraphViewModalProps> = ({
120177
121178 setNodes ( newNodes ) ;
122179 setRelationships ( newRels ) ;
123- } else {
180+ setLoading ( false ) ;
181+
182+ }
183+ else {
124184 console . error ( "Unable to retrieve document graph for " + inspectedName , results ) ;
125185 }
126186 }
127187 ) ;
128188 }
129- } , [ open ] ) ;
189+ } , [ open , graphType ] ) ;
130190
131191 // If the modal is closed, render nothing
132192 if ( ! open ) {
@@ -152,13 +212,12 @@ const GraphViewModal: React.FunctionComponent<GraphViewModalProps> = ({
152212 return (
153213 < >
154214 < Dialog size = 'unset' open = { open } aria-labelledby = 'form-dialog-title' disableCloseButton >
155- < Flex className = 'w-full' alignItems = 'center' justifyContent = 'space-between' style = { { flexFlow : 'row' } } >
156- < Dialog . Header id = 'form-dialog-title' > Inspect Generated Graph from { inspectedName } . </ Dialog . Header >
157- { true && < GraphDropdown onSelect = { handleDropdownChange } isDisabled = { false } /> }
158- </ Flex >
215+ < Dialog . Header id = 'form-dialog-title' > Inspect Generated Graph from { inspectedName } . </ Dialog . Header >
216+ { viewPoint === 'showGraphView' && < GraphDropdown onSelect = { handleDropdownChange } isDisabled = { false } /> }
159217 < Dialog . Content className = 'n-flex n-flex-col n-gap-token-4' >
160218 < > </ >
161- < div style = { { width : '100%' , height : '600px' } } >
219+ < div style = { { width : '100%' , height : '600px' , border : '1px solid red' } } >
220+ { /* {loading && <LoadingSpinner size="large"/>} */ }
162221 < InteractiveNvlWrapper
163222 ref = { nvlRef }
164223 nodes = { nodes }
0 commit comments