Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/shared/modules/connections/connectionsDuck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type ConnectionReduxState = {
connectionState: ConnectionState
lastUpdate: number
useDb: string | null
lastUseDb: string | null
}
type ConnectionState =
| typeof DISCONNECTED_STATE
Expand Down Expand Up @@ -103,7 +104,8 @@ const initialState: ConnectionReduxState = {
activeConnection: null,
connectionState: DISCONNECTED_STATE,
lastUpdate: 0,
useDb: null
useDb: null,
lastUseDb: null
}
/**
* Selectors
Expand All @@ -119,6 +121,10 @@ export function getConnection(
)
}

export function getLastUseDb(state: GlobalState): string | null {
return (state[NAME] || {}).lastUseDb
}

export function getUseDb(state: GlobalState): string | null {
return (state[NAME] || {}).useDb
}
Expand Down Expand Up @@ -286,7 +292,12 @@ export default function(state = initialState, action: any) {
case UPDATE_AUTH_ENABLED:
return updateAuthEnabledHelper(state, action.authEnabled)
case USE_DB:
return { ...state, useDb: action.useDb }
const { useDb } = action
let lastUseDb = useDb
if (useDb === null) {
lastUseDb = state.useDb || state.lastUseDb
}
return { ...state, lastUseDb, useDb }
case USER_CLEAR:
return initialState
default:
Expand Down Expand Up @@ -356,6 +367,8 @@ export const setAuthEnabled = (authEnabled: any) => {

export const useDb = (db: any = null) => ({ type: USE_DB, useDb: db })

export const resetUseDb = () => ({ type: USE_DB, useDb: null })

// Epics
export const useDbEpic = (action$: any) => {
return action$
Expand Down Expand Up @@ -449,7 +462,7 @@ function shouldTryAutoconnecting(conn: Connection | null): boolean {
export const startupConnectEpic = (action$: any, store: any) => {
return action$
.ofType(discovery.DONE)
.do(() => store.dispatch(useDb(null)))
.do(() => store.dispatch(resetUseDb()))
.mergeMap(async ({ discovered }: DiscoverDataAction) => {
const connectionTimeout = getConnectionTimeout(store.getState())
const savedConnection = getConnection(
Expand Down Expand Up @@ -559,7 +572,7 @@ export const disconnectEpic = (action$: any, store: any) => {
.ofType(DISCONNECT)
.merge(action$.ofType(USER_CLEAR))
.do(() => bolt.closeConnection())
.do(() => store.dispatch(useDb(null)))
.do(() => store.dispatch(resetUseDb()))
.do((action: any) =>
store.dispatch(updateConnection({ id: action.id, password: '' }))
)
Expand All @@ -569,7 +582,7 @@ export const silentDisconnectEpic = (action$: any, store: any) => {
return action$
.ofType(SILENT_DISCONNECT)
.do(() => bolt.closeConnection())
.do(() => store.dispatch(useDb(null)))
.do(() => store.dispatch(resetUseDb()))
.do(() => store.dispatch({ type: CLEAR_META }))
.mapTo(setActiveConnection(null, true))
}
Expand Down
18 changes: 12 additions & 6 deletions src/shared/modules/dbMeta/dbMetaDuck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
setAuthEnabled,
onLostConnection,
getUseDb,
getLastUseDb,
useDb,
getActiveConnectionData,
updateConnection
Expand Down Expand Up @@ -452,10 +453,15 @@ const switchToRequestedDb = (store: any) => {
const activeConnection = getActiveConnectionData(store.getState())
const requestedUseDb = activeConnection?.requestedUseDb

const switchToDefaultDb = () => {
const defaultDb = databases.find((db: any) => db.default)
if (defaultDb) {
store.dispatch(useDb(defaultDb.name))
const switchToLastUsedOrDefaultDb = () => {
const lastUsedDb = getLastUseDb(store.getState())
if (lastUsedDb && databases.some((db: any) => db.name === lastUsedDb)) {
store.dispatch(useDb(lastUsedDb))
} else {
const defaultDb = databases.find((db: any) => db.default)
if (defaultDb) {
store.dispatch(useDb(defaultDb.name))
}
}
}

Expand All @@ -478,10 +484,10 @@ const switchToRequestedDb = (store: any) => {
store.dispatch(executeCommand(`:use ${requestedUseDb}`), {
source: commandSources.auto
})
switchToDefaultDb()
switchToLastUsedOrDefaultDb()
}
} else {
switchToDefaultDb()
switchToLastUsedOrDefaultDb()
}
return Rx.Observable.of(null)
}
Expand Down