@@ -22,6 +22,35 @@ class NeoDash extends React.Component {
2222
2323 constructor ( props ) {
2424 super ( props ) ;
25+
26+ // Neo4j Desktop integration
27+ let neo4jDesktopApi = window . neo4jDesktopApi ;
28+ if ( neo4jDesktopApi ) {
29+ let promise = neo4jDesktopApi . getContext ( ) ;
30+ let a = this ;
31+ promise . then ( function ( context ) {
32+ let desktopIntegration = new Neo4jDesktopIntegration ( context ) ;
33+ let neo4j = desktopIntegration . getActiveDatabase ( ) ;
34+ if ( neo4j ) {
35+
36+ a . connection = {
37+ url : neo4j . connection . configuration . protocols . bolt . url ,
38+ database :"" ,
39+ username : neo4j . connection . configuration . protocols . bolt . username ,
40+ password : neo4j . connection . configuration . protocols . bolt . password ,
41+ encryption : neo4j . connection . configuration . protocols . bolt . tlsLevel === "REQUIRED"
42+ }
43+ a . connect ( )
44+
45+ } else {
46+ a . connect ( )
47+ a . updateConnectionModal ( a . connect , true ) ;
48+ }
49+ } ) ;
50+
51+ }
52+
53+ // check the browser cache or use default values.
2554 this . connection = {
2655 url : ( localStorage . getItem ( 'neodash-url' ) ) ? localStorage . getItem ( 'neodash-url' ) : 'neo4j://localhost:7687' ,
2756 database : ( localStorage . getItem ( 'neodash-database' ) ) ? localStorage . getItem ( 'neodash-database' ) : '' ,
@@ -38,16 +67,22 @@ class NeoDash extends React.Component {
3867 this . stateChanged = this . stateChanged . bind ( this ) ;
3968 this . loadJson = this . loadJson . bind ( this ) ;
4069 this . connect = this . connect . bind ( this ) ;
41- this . updateConnectionModal ( this . connect , true ) ;
70+ if ( neo4jDesktopApi ) {
71+ // this.updateConnectionModal(this.connect, true);
72+ this . stateChanged ( { label : "CreateError" , value : "Connecting to " + this . connection . url + "..." } )
73+ // this.errorModal = null;
74+ } else {
75+ this . updateConnectionModal ( this . connect , true ) ;
76+ }
4277 }
4378
4479 componentDidMount ( ) {
4580 this . loadJson ( )
4681 }
4782
83+
4884 connect ( e ) {
4985 try {
50-
5186 var url = this . connection . url ;
5287 if ( ! ( url . startsWith ( "bolt://" ) || url . startsWith ( "bolt+routing://" ) || url . startsWith ( "neo4j://" ) ) ) {
5388 url = "neo4j://" + url ;
@@ -64,6 +99,7 @@ class NeoDash extends React.Component {
6499 this . session
65100 . run ( 'return true;' )
66101 . then ( result => {
102+ this . errorModal = null ;
67103 this . connected = true ;
68104
69105
@@ -75,6 +111,8 @@ class NeoDash extends React.Component {
75111 localStorage . setItem ( 'neodash-username' , this . connection . username ) ;
76112 localStorage . setItem ( 'neodash-password' , this . connection . password . toString ( ) ) ;
77113 localStorage . setItem ( 'neodash-encryption' , this . connection . encryption ) ;
114+
115+
78116 } )
79117 . catch ( error => {
80118 this . stateChanged ( {
@@ -177,18 +215,19 @@ class NeoDash extends React.Component {
177215 if ( content . startsWith ( "Could not perform discovery. No routing servers available." ) ) {
178216 let encryption = this . connection . encryption ;
179217 content = "Unable to connect to the specified Neo4j database. " +
180- "The database might be unreachable, or it does not accept " + ( ( encryption === "on" ) ? "encrypted" : "unencrypted" ) + " connections. " + content ;
218+ "The database might be unreachable, or it does not accept " + ( ( encryption === "on" ) ? "encrypted" : "unencrypted" ) + " connections. " + content ;
181219
182220 }
183- this . errorModal = < NeoModal header = { "Error" }
184- style = { { 'maxWidth' : '550px' } }
185- open = { true }
186- trigger = { null }
187- content = { < p > { content } </ p > }
188- key = { this . state . count }
189- id = { this . state . count }
190- root = { document . getElementById ( "root" ) }
191- actions = { [
221+ let header = ( content . startsWith ( "Connecting to" ) ) ? "Connecting..." : "Error" ;
222+ this . errorModal = < NeoModal header = { header }
223+ style = { { 'maxWidth' : '550px' } }
224+ open = { true }
225+ trigger = { null }
226+ content = { < p > { content } </ p > }
227+ key = { this . state . count }
228+ id = { this . state . count }
229+ root = { document . getElementById ( "root" ) }
230+ actions = { [
192231 < Button flat modal = "close"
193232 node = "button"
194233 waves = "red" > Close</ Button >
@@ -432,5 +471,24 @@ class NeoDash extends React.Component {
432471 }
433472}
434473
474+ class Neo4jDesktopIntegration {
475+ constructor ( context ) {
476+ this . desktopContext = context ;
477+ }
478+
479+ getActiveDatabase ( ) {
480+
481+ for ( let pi = 0 ; pi < this . desktopContext . projects . length ; pi ++ ) {
482+ let prj = this . desktopContext . projects [ pi ] ;
483+ for ( let gi = 0 ; gi < prj . graphs . length ; gi ++ ) {
484+ let grf = prj . graphs [ gi ] ;
485+ if ( grf . status == 'ACTIVE' ) {
486+ return grf ;
487+ }
488+ }
489+ }
490+ return null ;
491+ }
492+ }
435493
436494export default ( NeoDash ) ;
0 commit comments