From 2915289b156f96287bfaa156332a632c1e2777c7 Mon Sep 17 00:00:00 2001 From: Tadayoshi Sato Date: Tue, 7 Jan 2025 17:55:54 +0900 Subject: [PATCH] feat(connect): make primary tab auto-connect to the first preset connection It also avoids failing to auto-connect to an authenticated preset connection in the primary tab by reloading the primary tab with ?con= parameter instead of resolving the conn ID asynchronously internally. hawtio/hawtio#3731 --- app/webpack.config.cjs | 2 ++ .../src/plugins/shared/connect-service.ts | 29 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/webpack.config.cjs b/app/webpack.config.cjs index 5aaa9240..b73e460f 100644 --- a/app/webpack.config.cjs +++ b/app/webpack.config.cjs @@ -218,6 +218,7 @@ module.exports = (_, args) => { devServer.app.get(`${publicPath}/keycloak/validate-subject-matches`, (_, res) => res.send('true')) // Testing preset connections + /* devServer.app.get(`${publicPath}/preset-connections`, (_, res) => { res.type('application/json') res.send( @@ -227,6 +228,7 @@ module.exports = (_, args) => { ]), ) }) + */ // Hawtio backend middleware should be run before other middlewares (thus 'unshift') // in order to handle GET requests to the proxied Jolokia endpoint. diff --git a/packages/hawtio/src/plugins/shared/connect-service.ts b/packages/hawtio/src/plugins/shared/connect-service.ts index 6424a330..840571cc 100644 --- a/packages/hawtio/src/plugins/shared/connect-service.ts +++ b/packages/hawtio/src/plugins/shared/connect-service.ts @@ -77,7 +77,7 @@ export interface IConnectService { connectionToUrl(connection: Connection): string checkReachable(connection: Connection): Promise testConnection(connection: Connection): Promise - connect(connection: Connection): void + connect(connection: Connection, current?: boolean): void login(username: string, password: string): Promise redirect(): void createJolokia(connection: Connection, checkCredentials?: boolean): IJolokiaSimple @@ -94,6 +94,13 @@ class ConnectService implements IConnectService { this.currentConnectionId = this.initCurrentConnection() } + /** + * The precedence of the current connection is as follows: + * 1. URL query parameter: {@link PARAM_KEY_CONNECTION} + * 2. Session storage: {@link SESSION_KEY_CURRENT_CONNECTION} + * 3. Preset connections from the backend API: {@link PATH_PRESET_CONNECTIONS} + * (after page reload or new tabs opened) + */ private initCurrentConnection(): string | null { // Check remote connection from URL query param const url = new URL(window.location.href) @@ -169,8 +176,13 @@ class ConnectService implements IConnectService { }) this.saveConnections(connections) - // Open connections in new tabs + // Open the first connection in the current tab + // and open the rest in new tabs + const first = toOpen.shift() toOpen.forEach(c => this.connect(c)) + if (first) { + this.connect(first, true) + } } catch (err) { // Silently ignore errors log.debug('Error loading preset connections:', err) @@ -394,13 +406,18 @@ class ConnectService implements IConnectService { }) } - connect(connection: Connection) { + connect(connection: Connection, current = false) { log.debug('Connecting with options:', toString(connection)) const basepath = hawtio.getBasePath() ?? '' const url = `${basepath}/?${PARAM_KEY_CONNECTION}=${connection.id}` - log.debug('Opening URL:', url) - // let's open the same connection in the same tab (2nd parameter) - window.open(url, connection.id) + if (current) { + log.debug('Redirecting to URL:', url) + window.location.href = url + } else { + log.debug('Opening URL:', url) + // let's open the same connection in the same tab (2nd parameter) + window.open(url, connection.id) + } } /**