Skip to content

Commit

Permalink
feat(connect): make primary tab auto-connect to the first preset conn…
Browse files Browse the repository at this point in the history
…ection

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
  • Loading branch information
tadayosi committed Jan 8, 2025
1 parent 1b78eb9 commit 2915289
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Expand Down
29 changes: 23 additions & 6 deletions packages/hawtio/src/plugins/shared/connect-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface IConnectService {
connectionToUrl(connection: Connection): string
checkReachable(connection: Connection): Promise<ConnectStatus>
testConnection(connection: Connection): Promise<ConnectionTestResult>
connect(connection: Connection): void
connect(connection: Connection, current?: boolean): void
login(username: string, password: string): Promise<LoginResult>
redirect(): void
createJolokia(connection: Connection, checkCredentials?: boolean): IJolokiaSimple
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

/**
Expand Down

0 comments on commit 2915289

Please sign in to comment.