Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: optimize tab store (#18380)

ecmadao76c32cc8629d
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 1870496f71..dfb8b3a8dd 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -90,7 +90,7 @@ const {
useRouteChangeGuard(
computed(() => {
return (
- tabStore.tabList.find(
+ tabStore.openTabList.find(
(tab) => !!tab?.worksheet && tab.status === "DIRTY"
) !== undefined
);
@@ -159,7 +159,7 @@ const connect = (connection: SQLEditorConnection) => {
};
const switchWorksheet = async (sheetName: string) => {
- const openingSheetTab = tabStore.tabList.find(
+ const openingSheetTab = tabStore.openTabList.find(
(tab) => tab.worksheet == sheetName
);
@@ -255,7 +255,7 @@ const prepareConnectionParams = async () => {
await useDatabaseV1Store().getOrFetchDatabaseByName(databaseName);
await maybeSwitchProject(database.project);
if (!isDatabaseV1Queryable(database)) {
- const tabs = tabStore.tabList.filter(
+ const tabs = tabStore.openTabList.filter(
(tab) => tab.connection.database === database.name
);
for (const tab of tabs) {
diff --git a/frontend/src/store/modules/sqlEditor/tab.ts b/frontend/src/store/modules/sqlEditor/tab.ts
index 4b1d6a9973..9d5ffef08f 100644
--- a/frontend/src/store/modules/sqlEditor/tab.ts
+++ b/frontend/src/store/modules/sqlEditor/tab.ts
@@ -1,5 +1,5 @@
import type { MaybeRef } from "@vueuse/core";
-import { head, pick } from "lodash-es";
+import { head, pick, uniqBy } from "lodash-es";
import { defineStore, storeToRefs } from "pinia";
import { computed, reactive, unref, watch } from "vue";
import type {
@@ -65,7 +65,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
}
);
- const openTabList = useDynamicLocalStorage<PersistentTab[]>(
+ const openTmpTabList = useDynamicLocalStorage<PersistentTab[]>(
computed(() => `${keyNamespace.value}.opening-tab-list`),
[],
localStorage,
@@ -84,8 +84,11 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
);
const loadStoredTabs = async () => {
- const validTabList: PersistentTab[] = [];
- for (const tab of openTabList.value) {
+ const validOpenTabMap: Map<string, PersistentTab> = new Map();
+ for (const tab of openTmpTabList.value) {
+ if (validOpenTabMap.has(tab.id)) {
+ continue;
+ }
let fullTab: SQLEditorTab | undefined;
if (tab.worksheet) {
const worksheet = await worksheetStore.getOrFetchWorksheetByName(
@@ -122,32 +125,30 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
continue;
}
- validTabList.push(tab);
+ validOpenTabMap.set(tab.id, tab);
tabsById.set(tab.id, fullTab);
}
- openTabList.value = validTabList;
+ openTmpTabList.value = [...validOpenTabMap.values()];
};
const maybeInitProject = async () => {
tabsById.clear();
+ draftTabList.value = uniqBy(draftTabList.value, (draft) => draft.id);
await loadStoredTabs();
- currentTabId.value = head(openTabList.value)?.id ?? "";
+ currentTabId.value = head(openTmpTabList.value)?.id ?? "";
};
- const tabById = (id: string) => {
- return tabsById.get(id);
- };
-
- const tabList = computed(() => {
- return openTabList.value.map((item) => {
- return tabById(item.id) ?? defaultSQLEditorTab();
+ const openTabList = computed(() => {
+ return openTmpTabList.value.map((item) => {
+ return tabsById.get(item.id) ?? defaultSQLEditorTab();
});
});
+
const currentTab = computed(() => {
const currId = currentTabId.value;
if (!currId) return undefined;
- return tabById(currId);
+ return tabsById.get(currId);
});
const supportBatchMode = computed(() => currentTab.value?.mode !== "ADMIN");
@@ -187,8 +188,10 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
});
const { id, worksheet } = newTab;
- if (openTabList.value.find((tab) => tab.id === id)) {
+ if (openTmpTabList.value.find((tab) => tab.id === id)) {
setCurrentTabId(id);
+ const response = tabsById.get(id) ?? newTab;
+ tabsById.set(id, response);
return newTab;
}
@@ -196,13 +199,13 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
newTab,
...PERSISTENT_TAB_FIELDS
) as PersistentTab;
- const position = openTabList.value.findIndex(
+ const position = openTmpTabList.value.findIndex(
(item) => item.id === currentTabId.value
);
if (beside && position >= 0) {
- openTabList.value.splice(position + 1, 0, persistentTab);
+ openTmpTabList.value.splice(position + 1, 0, persistentTab);
} else {
- openTabList.value.push(persistentTab);
+ openTmpTabList.value.push(persistentTab);
}
if (
!worksheet &&
@@ -228,11 +231,11 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
const closeTab = (tab: SQLEditorTab) => {
const { id } = tab;
- const position = openTabList.value.findIndex((item) => item.id === id);
+ const position = openTmpTabList.value.findIndex((item) => item.id === id);
if (position < 0) {
return;
}
- openTabList.value.splice(position, 1);
+ openTmpTabList.value.splice(position, 1);
tabsById.delete(id);
if (tab.mode === "ADMIN") {
@@ -240,8 +243,8 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
}
if (id === currentTabId.value) {
- const nextIndex = Math.min(position, openTabList.value.length - 1);
- const nextTab = openTabList.value[nextIndex];
+ const nextIndex = Math.min(position, openTmpTabList.value.length - 1);
+ const nextTab = openTmpTabList.value[nextIndex];
setCurrentTabId(nextTab?.id ?? "");
}
};
@@ -254,7 +257,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
Object.assign(draftTabList.value[draftIndex], tab);
}
- const openTabIndex = openTabList.value.findIndex(
+ const openTabIndex = openTmpTabList.value.findIndex(
(item) => item.id === tab.id
);
if (openTabIndex >= 0) {
@@ -262,12 +265,12 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
tab,
...PERSISTENT_TAB_FIELDS
) as PersistentTab;
- Object.assign(openTabList.value[openTabIndex], persistentTab);
+ Object.assign(openTmpTabList.value[openTabIndex], persistentTab);
}
};
const updateTab = (id: string, payload: Partial<SQLEditorTab>) => {
- const tab = tabById(id);
+ const tab = tabsById.get(id);
if (!tab) return;
Object.assign(tab, payload);
@@ -308,7 +311,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
database: string;
contextId: string;
}): SQLEditorDatabaseQueryContext | undefined => {
- const tab = tabById(currentTabId.value);
+ const tab = tabsById.get(currentTabId.value);
if (!tab || !tab.databaseQueryContexts) {
return;
}
@@ -331,7 +334,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
database: string;
contextIds: string[];
}) => {
- const tab = tabById(currentTabId.value);
+ const tab = tabsById.get(currentTabId.value);
if (!tab || !tab.databaseQueryContexts) {
return;
}
@@ -354,7 +357,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
};
const deleteDatabaseQueryContext = (database: string) => {
- const tab = tabById(currentTabId.value);
+ const tab = tabsById.get(currentTabId.value);
if (!tab || !tab.databaseQueryContexts) {
return;
}
@@ -370,7 +373,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
contextId: string;
context: Partial<SQLEditorDatabaseQueryContext>;
}) => {
- const tab = tabById(currentTabId.value);
+ const tab = tabsById.get(currentTabId.value);
if (!tab || !tab.databaseQueryContexts) {
return;
}
@@ -411,7 +414,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
return curr;
}
}
- const similarNewTab = tabList.value.find(
+ const similarNewTab = openTabList.value.find(
(tmp) => tmp.status === "CLEAN" && isSimilarSQLEditorTab(tmp, tab)
);
if (similarNewTab) {
@@ -444,11 +447,10 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
return {
project,
- tabList,
+ openTabList,
draftList: computed(() => draftTabList.value),
currentTabId,
currentTab,
- tabById,
addTab,
removeDraft,
closeTab,
@@ -473,13 +475,13 @@ export const useCurrentSQLEditorTab = () => {
};
export const isSQLEditorTabClosable = (tab: SQLEditorTab) => {
- const { tabList } = useSQLEditorTabStore();
+ const { openTabList } = useSQLEditorTabStore();
- if (tabList.length > 1) {
+ if (openTabList.length > 1) {
// Not the only one tab
return true;
}
- if (tabList.length === 1) {
+ if (openTabList.length === 1) {
// It's the only one tab, and it's closable if it's a sheet tab
return !!tab.worksheet;
}
@@ -523,11 +525,11 @@ export const useConnectionOfCurrentSQLEditorTab = () => {
};
export const resolveOpeningDatabaseListFromSQLEditorTabList = () => {
- const { tabList } = useSQLEditorTabStore();
+ const { openTabList } = useSQLEditorTabStore();
const databaseStore = useDatabaseV1Store();
const databaseSet = new Set<string>();
- for (const tab of tabList) {
+ for (const tab of openTabList) {
const { database } = tab.connection;
if (database) {
const db = databaseStore.getDatabaseByName(database);
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
index be0098edc0..96de110408 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
@@ -132,7 +132,7 @@ export const useActions = () => {
clonedTab.status = "CLEAN";
clonedTab.title = params.title ?? "";
- for (const tab of tabStore.tabList) {
+ for (const tab of tabStore.openTabList) {
if (tab.id === fromTab?.id) {
continue;
}
diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
index 32af3f737e..373ee17856 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
@@ -336,7 +336,7 @@ const handleRenameNode = useDebounceFn(async () => {
);
// update tab title
- const tab = tabStore.tabList.find(
+ const tab = tabStore.openTabList.find(
(t) => t.worksheet === editingNode.value?.node.worksheet?.name
);
if (tab) {
@@ -465,7 +465,7 @@ const deleteWorksheets = async (worksheets: string[]) => {
)
);
for (const worksheet of worksheets) {
- const tab = tabStore.tabList.find((tab) => tab.worksheet === worksheet);
+ const tab = tabStore.openTabList.find((tab) => tab.worksheet === worksheet);
if (tab) {
tabStore.closeTab(tab);
removeViewState(tab.id);
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index b1a7f39137..1be9416c40 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -561,7 +561,7 @@ export const openWorksheetByName = async (
await editorContext.maybeSwitchProject(worksheet.project);
const tabStore = useSQLEditorTabStore();
- const openingSheetTab = tabStore.tabList.find(
+ const openingSheetTab = tabStore.openTabList.find(
(tab) => tab.worksheet === worksheet.name
);
diff --git a/frontend/src/views/sql-editor/TabList/TabList.vue b/frontend/src/views/sql-editor/TabList/TabList.vue
index b8e328d923..2ce8b5fb3b 100644
--- a/frontend/src/views/sql-editor/TabList/TabList.vue
+++ b/frontend/src/views/sql-editor/TabList/TabList.vue
@@ -13,7 +13,7 @@
<Draggable
id="tab-list"
ref="tabListRef"
- v-model="tabStore.tabList"
+ v-model="tabStore.openTabList"
item-key="id"
animation="300"
class="relative flex flex-nowrap overflow-hidden h-9 pt-0.5 hide-scrollbar"
@@ -230,7 +230,7 @@ useEmitteryEventListener(
context.events,
"close-tab",
async ({ tab, index, action }) => {
- const { tabList } = tabStore;
+ const { openTabList } = tabStore;
const remove = async (tab: SQLEditorTab) => {
await handleRemoveTab(tab, true);
@@ -241,25 +241,25 @@ useEmitteryEventListener(
await remove(tab);
return;
}
- const max = tabList.length - 1;
+ const max = openTabList.length - 1;
if (action === "CLOSE_OTHERS") {
for (let i = max; i > index; i--) {
- await remove(tabList[i]);
+ await remove(openTabList[i]);
}
for (let i = index - 1; i >= 0; i--) {
- await remove(tabList[i]);
+ await remove(openTabList[i]);
}
return;
}
if (action === "CLOSE_TO_THE_RIGHT") {
for (let i = max; i > index; i--) {
- await remove(tabList[i]);
+ await remove(openTabList[i]);
}
return;
}
if (action === "CLOSE_SAVED") {
for (let i = max; i >= 0; i--) {
- const tab = tabList[i];
+ const tab = openTabList[i];
if (tab.status === "CLEAN") {
await remove(tab);
}
@@ -268,7 +268,7 @@ useEmitteryEventListener(
}
if (action === "CLOSE_ALL") {
for (let i = max; i >= 0; i--) {
- await remove(tabList[i]);
+ await remove(openTabList[i]);
}
}
}