77//
88//////////////////////////////////////////////////////////////
99
10- import React , { useMemo , useState } from 'react' ;
10+ import React , { useEffect , useMemo , useState } from 'react' ;
1111import gettext from 'sources/gettext' ;
1212import url_for from 'sources/url_for' ;
1313import _ from 'lodash' ;
@@ -27,8 +27,10 @@ import * as commonUtils from 'sources/utils';
2727import * as showQueryTool from '../../../../tools/sqleditor/static/js/show_query_tool' ;
2828import { getTitle , generateTitle } from '../../../../tools/sqleditor/static/js/sqleditor_title' ;
2929import usePreferences from '../../../../preferences/static/js/store' ;
30- import { BROWSER_PANELS } from '../../../../browser/static/js/constants' ;
30+ import { BROWSER_PANELS , WORKSPACES } from '../../../../browser/static/js/constants' ;
3131import { isEmptyString } from '../../../../static/js/validators' ;
32+ import { getRandomInt } from '../../../../static/js/utils' ;
33+ import { useWorkspace } from './WorkspaceProvider' ;
3234
3335class AdHocConnectionSchema extends BaseUISchema {
3436 constructor ( connectExistingServer , initValues = { } ) {
@@ -49,6 +51,7 @@ class AdHocConnectionSchema extends BaseUISchema {
4951 { 'name' : 'sslmode' , 'value' : 'prefer' , 'keyword' : 'sslmode' } ,
5052 { 'name' : 'connect_timeout' , 'value' : 10 , 'keyword' : 'connect_timeout' } ] ,
5153 ...initValues ,
54+ connection_refresh : 0 ,
5255 } ) ;
5356 this . flatServers = [ ] ;
5457 this . groupedServers = [ ] ;
@@ -58,6 +61,12 @@ class AdHocConnectionSchema extends BaseUISchema {
5861 this . paramSchema = new VariableSchema ( getConnectionParameters , null , null , [ 'name' , 'keyword' , 'value' ] ) ;
5962 }
6063
64+ refreshServerList ( ) {
65+ // its better to refresh the server list than monkey patching server connected status.
66+ this . groupedServers = [ ] ;
67+ this . state . setUnpreparedData ( [ 'connection_refresh' ] , getRandomInt ( 1 , 9999 ) ) ;
68+ }
69+
6170 setServerConnected ( sid , icon ) {
6271 for ( const group of this . groupedServers ) {
6372 for ( const opt of group . options ) {
@@ -132,12 +141,12 @@ class AdHocConnectionSchema extends BaseUISchema {
132141 let self = this ;
133142 return [
134143 {
135- id : 'sid' , label : gettext ( 'Existing Server (Optional)' ) , deps : [ 'connected' ] ,
136- type : ( ) => ( {
144+ id : 'sid' , label : gettext ( 'Existing Server (Optional)' ) , deps : [ 'connected' , 'connection_refresh' ] ,
145+ type : ( state ) => ( {
137146 type : 'select' ,
138147 options : ( ) => self . getServerList ( ) ,
139148 optionsLoaded : ( res ) => self . flatServers = flattenSelectOptions ( res ) ,
140- optionsReloadBasis : self . flatServers . map ( ( s ) => s . connected ) . join ( '' ) ,
149+ optionsReloadBasis : ` ${ self . flatServers . map ( ( s ) => s . connected ) . join ( '' ) } ${ state . connection_refresh } ` ,
141150 } ) ,
142151 depChange : ( state ) => {
143152 /* Once the option is selected get the name */
@@ -160,6 +169,7 @@ class AdHocConnectionSchema extends BaseUISchema {
160169 } ;
161170 } ,
162171 deferredDepChange : ( state , source , topState , actionObj ) => {
172+ if ( source . includes ( 'connection_refresh' ) ) return ;
163173 return new Promise ( ( resolve ) => {
164174 let sid = actionObj . value ;
165175 let selectedServer = _ . find ( self . flatServers , ( s ) => s . value == sid ) ;
@@ -323,6 +333,7 @@ export default function AdHocConnection({mode}) {
323333 const modal = useModal ( ) ;
324334 const pgAdmin = usePgAdmin ( ) ;
325335 const preferencesStore = usePreferences ( ) ;
336+ const { currentWorkspace} = useWorkspace ( ) ;
326337
327338 const connectExistingServer = async ( sid , user , formData , connectCallback ) => {
328339 setConnecting ( true ) ;
@@ -447,9 +458,9 @@ export default function AdHocConnection({mode}) {
447458 data : JSON . stringify ( formData )
448459 } ) ;
449460 setConnecting ( false ) ;
450- if ( mode == 'Query Tool' ) {
461+ if ( mode == WORKSPACES . QUERY_TOOL ) {
451462 openQueryTool ( respData , formData ) ;
452- } else if ( mode == 'PSQL' ) {
463+ } else if ( mode == WORKSPACES . PSQL_TOOL ) {
453464 openPSQLTool ( respData , formData ) ;
454465 }
455466 } catch ( error ) {
@@ -478,12 +489,16 @@ export default function AdHocConnection({mode}) {
478489 } ;
479490
480491 let saveBtnName = gettext ( 'Connect & Open Query Tool' ) ;
481- if ( mode == 'PSQL' ) {
492+ if ( mode == WORKSPACES . PSQL_TOOL ) {
482493 saveBtnName = gettext ( 'Connect & Open PSQL' ) ;
483494 }
484495
485496 let adHocConObj = useMemo ( ( ) => new AdHocConnectionSchema ( connectExistingServer ) , [ ] ) ;
486497
498+ useEffect ( ( ) => {
499+ if ( currentWorkspace == mode ) adHocConObj . refreshServerList ( ) ;
500+ } , [ currentWorkspace ] ) ;
501+
487502 return < SchemaView
488503 formType = { 'dialog' }
489504 getInitData = { ( ) => { /*This is intentional (SonarQube)*/ } }
0 commit comments