diff --git a/packages/web/src/stores.ts b/packages/web/src/stores.ts index 847b6168c..3ffc1a50f 100644 --- a/packages/web/src/stores.ts +++ b/packages/web/src/stores.ts @@ -92,7 +92,7 @@ export const openedConnections = writable([]); export const temporaryOpenedConnections = writable([]); export const openedSingleDatabaseConnections = writable([]); export const expandedConnections = writable([]); -export const currentDatabase = writable(null); +export const currentDatabase = writableWithForage(null, 'currentDatabase'); export const openedTabs = writableWithForage([], getOpenedTabsStorageName(), x => [...(x || [])]); export const copyRowsFormat = writableWithStorage('textWithoutHeaders', 'copyRowsFormat'); export const extensions = writable(null); diff --git a/packages/web/src/tabpanel/TabsPanel.svelte b/packages/web/src/tabpanel/TabsPanel.svelte index 684e7c3af..514dfb9a7 100644 --- a/packages/web/src/tabpanel/TabsPanel.svelte +++ b/packages/web/src/tabpanel/TabsPanel.svelte @@ -295,6 +295,7 @@ import CloseTabModal from '../modals/CloseTabModal.svelte'; import SwitchDatabaseModal from '../modals/SwitchDatabaseModal.svelte'; import { getConnectionLabel } from 'dbgate-tools'; + import { changeDatabaseByCurrentTab } from '../utility/changeCurrentDbByTab'; export let multiTabIndex; export let shownTab; @@ -334,6 +335,7 @@ return; } setSelectedTab(tabid); + changeDatabaseByCurrentTab(); }; const handleMouseDown = (e, tabid) => { diff --git a/packages/web/src/utility/changeCurrentDbByTab.ts b/packages/web/src/utility/changeCurrentDbByTab.ts index 7a8183643..864abd206 100644 --- a/packages/web/src/utility/changeCurrentDbByTab.ts +++ b/packages/web/src/utility/changeCurrentDbByTab.ts @@ -1,35 +1,51 @@ import _ from 'lodash'; -import { currentDatabase, getCurrentDatabase, getLockedDatabaseMode, openedTabs } from '../stores'; +import { currentDatabase, getActiveTab, getCurrentDatabase, getLockedDatabaseMode, openedTabs } from '../stores'; import { shouldShowTab } from '../tabpanel/TabsPanel.svelte'; import { callWhenAppLoaded, getAppLoaded } from './appLoadManager'; import { getConnectionInfo } from './metadataLoaders'; import { switchCurrentDatabase } from './common'; -let lastCurrentTab = null; +// let lastCurrentTab = null; -openedTabs.subscribe(value => { - const newCurrentTab = (value || []).find(x => x.selected); - if (newCurrentTab == lastCurrentTab) return; - if (getLockedDatabaseMode() && getCurrentDatabase()) return; +// openedTabs.subscribe(value => { +// const newCurrentTab = (value || []).find(x => x.selected); +// if (newCurrentTab == lastCurrentTab) return; +// if (getLockedDatabaseMode() && getCurrentDatabase()) return; - const lastTab = lastCurrentTab; - lastCurrentTab = newCurrentTab; - // if (lastTab?.tabComponent == 'ConnectionTab') return; +// const lastTab = lastCurrentTab; +// lastCurrentTab = newCurrentTab; +// // if (lastTab?.tabComponent == 'ConnectionTab') return; - if (newCurrentTab) { - const { conid, database } = newCurrentTab.props || {}; - if (conid && database && (conid != lastTab?.props?.conid || database != lastTab?.props?.database)) { - const doWork = async () => { - const connection = await getConnectionInfo({ conid }); - switchCurrentDatabase({ - connection, - name: database, - }); - }; - callWhenAppLoaded(doWork); - } +// if (newCurrentTab) { +// const { conid, database } = newCurrentTab.props || {}; +// if (conid && database && (conid != lastTab?.props?.conid || database != lastTab?.props?.database)) { +// const doWork = async () => { +// const connection = await getConnectionInfo({ conid }); +// switchCurrentDatabase({ +// connection, +// name: database, +// }); +// }; +// callWhenAppLoaded(doWork); +// } +// } +// }); + +export function changeDatabaseByCurrentTab() { + const currentTab = getActiveTab(); + const { conid, database } = currentTab?.props || {}; + const db = getCurrentDatabase(); + if (conid && database && (conid != db?.connection?._id || database != db?.name)) { + const doWork = async () => { + const connection = await getConnectionInfo({ conid }); + switchCurrentDatabase({ + connection, + name: database, + }); + }; + callWhenAppLoaded(doWork); } -}); +} currentDatabase.subscribe(currentDb => { if (!getLockedDatabaseMode()) return;