From 13de275623ad3217dde8e995ae74fb5bc87e816d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Thu, 16 May 2024 16:59:52 +0200 Subject: [PATCH] Look within the webpage for the initial configuration --- src/jupyterlab/index.ts | 4 ++-- src/jupyterlab/tokens.ts | 4 ++-- src/state/index.ts | 14 +++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/jupyterlab/index.ts b/src/jupyterlab/index.ts index 075dc0f..3d17ff9 100644 --- a/src/jupyterlab/index.ts +++ b/src/jupyterlab/index.ts @@ -68,7 +68,7 @@ const plugin: JupyterFrontEndPlugin = { .then(data => { console.log('Received Datalayer configuration', data); datalayerStore.getState().setVersion(data.version); - const configuration = { + const configuration: IDatalayerConfig = { apiServerUrl: data.settings.api_server_url, launcher: data.settings.launcher, whiteLabel: data.settings.white_label @@ -107,7 +107,7 @@ const plugin: JupyterFrontEndPlugin = { tracker.add(widget); } }); - const category = configuration.launcher.category; + const category = configuration.launcher?.category ?? 'Datalayer'; palette.addItem({ command, category }); const settingsUpdated = (settings: ISettingRegistry.ISettings) => { const showInLauncher = settings.get('showInLauncher') diff --git a/src/jupyterlab/tokens.ts b/src/jupyterlab/tokens.ts index 42b16ac..8b5b017 100644 --- a/src/jupyterlab/tokens.ts +++ b/src/jupyterlab/tokens.ts @@ -9,7 +9,7 @@ export type IDatalayerConfig = { /** * Launcher card customization */ - launcher: { + launcher?: { /** * Card category */ @@ -56,7 +56,7 @@ export class DatalayerConfiguration { this._configuration = configuration; this._configurationChanged.emit(configuration); } - get configuration() { + get configuration(): IDatalayerConfig | undefined { return this._configuration; } get configurationChanged(): ISignal< diff --git a/src/state/index.ts b/src/state/index.ts index 932c991..6e12c9e 100644 --- a/src/state/index.ts +++ b/src/state/index.ts @@ -22,11 +22,23 @@ export type DatalayerState = { setVersion: (version: string) => void; }; +let initialConfiguration: IDatalayerConfig | undefined = undefined; + +// Try loading initial state from datalayer-config-data element +try { + const rawConfig = document.getElementById('datalayer-config-data'); + if (rawConfig?.innerText) { + initialConfiguration = JSON.parse(rawConfig?.innerText); + } +} catch (error) { + console.debug('No configuration found in the webpage.', error); +} + export const datalayerStore = createStore((set, get) => ({ tab: 0.0, getIntTab: () => Math.floor(get().tab), setTab: (tab: number) => set((state: DatalayerState) => ({ tab })), - configuration: undefined, + configuration: initialConfiguration, setConfiguration: (configuration?: IDatalayerConfig) => { set(state => ({ configuration })); },