Browse Source

[editor] Fix issue where the editor might not have an active DB when the assist DB panel isn't visible on load

Johan Åhlén 2 years ago
parent
commit
fdecc6fbb1

+ 4 - 1
desktop/core/src/desktop/js/ko/components/assist/ko.assistDbPanel.js

@@ -743,7 +743,10 @@ class AssistDbPanel {
     this.init(options.navigationSettings);
     this.init(options.navigationSettings);
     huePubSub.publish(ASSIST_DB_PANEL_IS_READY_EVENT);
     huePubSub.publish(ASSIST_DB_PANEL_IS_READY_EVENT);
 
 
-    huePubSub.subscribe(ASSIST_IS_DB_PANEL_READY_EVENT, () => {
+    huePubSub.subscribe(ASSIST_IS_DB_PANEL_READY_EVENT, callback => {
+      if (callback) {
+        callback();
+      }
       huePubSub.publish(ASSIST_DB_PANEL_IS_READY_EVENT);
       huePubSub.publish(ASSIST_DB_PANEL_IS_READY_EVENT);
     });
     });
   }
   }

+ 22 - 1
desktop/core/src/desktop/js/ko/components/ko.contextSelector.js

@@ -17,7 +17,7 @@
 import $ from 'jquery';
 import $ from 'jquery';
 import * as ko from 'knockout';
 import * as ko from 'knockout';
 
 
-import { ASSIST_SET_DATABASE_EVENT } from './assist/events';
+import { ASSIST_IS_DB_PANEL_READY_EVENT, ASSIST_SET_DATABASE_EVENT } from './assist/events';
 import componentUtils from './componentUtils';
 import componentUtils from './componentUtils';
 import contextCatalog from 'catalog/contextCatalog';
 import contextCatalog from 'catalog/contextCatalog';
 import dataCatalog from 'catalog/dataCatalog';
 import dataCatalog from 'catalog/dataCatalog';
@@ -427,6 +427,27 @@ HueContextSelector.prototype.reloadDatabases = function () {
                     databaseNames.push(databaseEntry.name);
                     databaseNames.push(databaseEntry.name);
                   });
                   });
                   self.availableDatabases(databaseNames);
                   self.availableDatabases(databaseNames);
+                  if (!self.database() && databaseNames.length) {
+                    /* The code below takes care of a corner case when the editor is loaded and the assist DB panel
+                       isn't open in which case an active DB might not be set.
+
+                       There's quite some related logic in the editor code (snippet.js) for when the assist DB panel is
+                       open on load. Ideally we should move all that logic here, however it's not trivial to untangle as
+                       it contains some editor specific integration. Perhaps it's cleaner to deal with if in Editor V2.
+                    */
+                    // TODO: Move the logic for setting active DB from assist panel here.
+                    let dbPanelReady = false;
+                    huePubSub.publish(ASSIST_IS_DB_PANEL_READY_EVENT, () => {
+                      dbPanelReady = true;
+                    });
+                    if (!dbPanelReady) {
+                      if (databaseNames.some(name => name === 'default')) {
+                        self.database('default');
+                      } else {
+                        self.database(databaseNames[0]);
+                      }
+                    }
+                  }
                 })
                 })
                 .catch(() => {
                 .catch(() => {
                   self.availableDatabases([]);
                   self.availableDatabases([]);