Skip to content

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

feat(sql-editor): sql editor sheet improvements (#7616)

Liu Ji38efd3d4d0c1
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 3a05175215..152f1d9eb4 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -46,7 +46,7 @@ import {
connectionV1Slug as makeConnectionV1Slug,
isSheetReadableV1,
isDatabaseV1Queryable,
- getDefaultTabNameFromConnection,
+ getSuggestedTabNameFromConnection,
isSimilarTab,
hasWorkspacePermissionV1,
} from "@/utils";
@@ -219,7 +219,7 @@ const prepareSheet = async () => {
tabStore.setCurrentTabId(openingSheetTab.id);
} else {
// Open the sheet in a "temp" tab otherwise.
- tabStore.selectOrAddTempTab();
+ tabStore.addTab();
}
let insId = String(UNKNOWN_ID);
@@ -263,7 +263,7 @@ const prepareConnectionSlug = async () => {
const tab = tabStore.currentTab;
if (tab.sheetName) {
// Don't touch a saved sheet.
- tabStore.selectOrAddTempTab();
+ tabStore.addTab();
return;
}
const target: CoreTabInfo = {
@@ -275,7 +275,7 @@ const prepareConnectionSlug = async () => {
// Don't go further if the connection doesn't change.
return;
}
- const name = getDefaultTabNameFromConnection(target.connection);
+ const name = getSuggestedTabNameFromConnection(target.connection);
tabStore.selectOrAddSimilarTab(
target,
/* beside */ false,
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index ba2092bf8f..5c2c4552e8 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -1,19 +1,17 @@
import { watchThrottled } from "@vueuse/core";
import { pick } from "lodash-es";
import { defineStore } from "pinia";
-import { computed, reactive, ref, toRef, watch } from "vue";
+import { computed, nextTick, reactive, ref, toRef, watch } from "vue";
import { TabInfo, CoreTabInfo, AnyTabInfo, TabMode } from "@/types";
-import { UNKNOWN_ID } from "@/types";
import {
getDefaultTab,
INITIAL_TAB,
isTempTab,
isSimilarTab,
WebStorageHelper,
- instanceV1AllowsCrossDatabaseQuery,
+ isDisconnectedTab,
} from "@/utils";
import { useWebTerminalV1Store } from "./v1";
-import { useInstanceV1Store } from "./v1/instance";
import { useSheetV1Store } from "./v1/sheet";
const LOCAL_STORAGE_KEY_PREFIX = "bb.sql-editor.tab-list";
@@ -39,7 +37,6 @@ type PersistentTabInfo = Pick<TabInfo, typeof PERSISTENT_TAB_FIELDS[number]>;
export const useTabStore = defineStore("tab", () => {
const storage = new WebStorageHelper("bb.sql-editor.tab-list", localStorage);
- const instanceStore = useInstanceV1Store();
// states
// We store the tabIdList and the tabs separately.
@@ -60,16 +57,7 @@ export const useTabStore = defineStore("tab", () => {
return tab ?? getDefaultTab();
});
const isDisconnected = computed((): boolean => {
- const { instanceId, databaseId } = currentTab.value.connection;
- if (instanceId === String(UNKNOWN_ID)) {
- return true;
- }
- const instance = instanceStore.getInstanceByUID(instanceId);
- if (instanceV1AllowsCrossDatabaseQuery(instance)) {
- // Connecting to instance directly.
- return false;
- }
- return databaseId === String(UNKNOWN_ID);
+ return isDisconnectedTab(currentTab.value);
});
// actions
@@ -89,11 +77,13 @@ export const useTabStore = defineStore("tab", () => {
currentTabId.value = id;
tabs.value.set(id, newTab);
- if (!newTab.sheetName) {
- // Switch the tab to "database" after adding a new sheet
- // because users need to select a database to continue editing
- asidePanelTab.value = "databases";
- }
+ nextTick(() => {
+ if (!currentTab.value.sheetName && isDisconnectedTab(currentTab.value)) {
+ // Switch the tab to "database" after adding a new sheet
+ // because users need to select a database to continue editing
+ asidePanelTab.value = "databases";
+ }
+ });
watchTab(newTab, false);
};
@@ -167,6 +157,15 @@ export const useTabStore = defineStore("tab", () => {
// watchers
const watchTab = (tab: TabInfo, immediate: boolean) => {
+ const dirtyFields = [
+ () => tab.name,
+ () => tab.sheetName,
+ () => tab.statement,
+ ];
+ watch(dirtyFields, () => {
+ tab.isFreshNew = false;
+ });
+
// Use a throttled watcher to reduce the performance overhead when writing.
watchThrottled(
() => pick(tab, ...PERSISTENT_TAB_FIELDS),
diff --git a/frontend/src/types/tab.ts b/frontend/src/types/tab.ts
index 6d2c154721..fa0e019ff0 100644
--- a/frontend/src/types/tab.ts
+++ b/frontend/src/types/tab.ts
@@ -19,6 +19,11 @@ export enum TabMode {
Admin = 2,
}
+export type TabSheetType =
+ | "TEMP" // Unsaved local sheet
+ | "CLEAN" // Saved and untouched sheet
+ | "DIRTY"; // Saved remotely, touched and unsaved locally
+
export type EditMode = "SQL-EDITOR" | "CHAT-TO-SQL";
export interface TabInfo {
@@ -40,6 +45,7 @@ export interface TabInfo {
queryResult?: SQLResultSet;
sheetName?: string;
sqlResultSet?: SQLResultSetV1;
+ isFreshNew?: boolean;
}
export type CoreTabInfo = Pick<TabInfo, "connection" | "sheetName" | "mode">;
diff --git a/frontend/src/utils/tab.ts b/frontend/src/utils/tab.ts
index 4cbfc43d0f..8ad590b71b 100644
--- a/frontend/src/utils/tab.ts
+++ b/frontend/src/utils/tab.ts
@@ -1,12 +1,26 @@
import dayjs from "dayjs";
import { v1 as uuidv1 } from "uuid";
-import { computed } from "vue";
import { useDatabaseV1Store, useInstanceV1Store } from "@/store";
-import type { Connection, ConnectionAtom, CoreTabInfo, TabInfo } from "@/types";
+import type {
+ ComposedDatabase,
+ ComposedInstance,
+ Connection,
+ ConnectionAtom,
+ CoreTabInfo,
+ TabInfo,
+ TabSheetType,
+} from "@/types";
import { UNKNOWN_ID, TabMode } from "@/types";
-import { t } from "../plugins/i18n";
+import { instanceV1AllowsCrossDatabaseQuery } from "./v1/instance";
-export const defaultTabName = computed(() => t("sql-editor.untitled-sheet"));
+export const getDefaultTabName = () => {
+ return dayjs().format("YYYY-MM-DD HH:mm");
+};
+
+export const isSimilarDefaultTabName = (name: string) => {
+ const regex = /(^|\s)(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/;
+ return regex.test(name);
+};
export const emptyConnection = (): Connection => {
return {
@@ -18,7 +32,7 @@ export const emptyConnection = (): Connection => {
export const getDefaultTab = (): TabInfo => {
return {
id: uuidv1(),
- name: defaultTabName.value,
+ name: getDefaultTabName(),
connection: emptyConnection(),
isSaved: true,
savedAt: dayjs().format("YYYY-MM-DD HH:mm:ss"),
@@ -39,6 +53,36 @@ export const isTempTab = (tab: TabInfo): boolean => {
return true;
};
+export const sheetTypeForTab = (tab: TabInfo): TabSheetType => {
+ if (!tab.sheetName) {
+ return "TEMP";
+ }
+ if (tab.isSaved) {
+ return "CLEAN";
+ }
+ return "DIRTY";
+};
+
+export const connectionForTab = (tab: TabInfo) => {
+ const target: {
+ instance: ComposedInstance | undefined;
+ database: ComposedDatabase | undefined;
+ } = {
+ instance: undefined,
+ database: undefined,
+ };
+ const { instanceId, databaseId } = tab.connection;
+ if (databaseId !== String(UNKNOWN_ID)) {
+ const database = useDatabaseV1Store().getDatabaseByUID(databaseId);
+ target.database = database;
+ target.instance = database.instanceEntity;
+ } else if (instanceId !== String(UNKNOWN_ID)) {
+ const instance = useInstanceV1Store().getInstanceByUID(instanceId);
+ target.instance = instance;
+ }
+ return target;
+};
+
export const isSameConnection = (a: Connection, b: Connection): boolean => {
return a.instanceId === b.instanceId && a.databaseId === b.databaseId;
};
@@ -51,16 +95,17 @@ export const isSimilarTab = (a: CoreTabInfo, b: CoreTabInfo): boolean => {
);
};
-export const getDefaultTabNameFromConnection = (conn: Connection) => {
+export const getSuggestedTabNameFromConnection = (conn: Connection) => {
const instance = useInstanceV1Store().getInstanceByUID(conn.instanceId);
const database = useDatabaseV1Store().getDatabaseByUID(conn.databaseId);
+ const parts: string[] = [];
if (database.uid !== String(UNKNOWN_ID)) {
- return `${database.databaseName}`;
- }
- if (instance.uid !== String(UNKNOWN_ID)) {
- return `${instance.title}`;
+ parts.push(database.databaseName);
+ } else if (instance.uid !== String(UNKNOWN_ID)) {
+ parts.push(instance.title);
}
- return defaultTabName.value;
+ parts.push(getDefaultTabName());
+ return parts.join(" ");
};
export const instanceOfConnectionAtom = (atom: ConnectionAtom) => {
@@ -72,3 +117,16 @@ export const instanceOfConnectionAtom = (atom: ConnectionAtom) => {
}
return undefined;
};
+
+export const isDisconnectedTab = (tab: TabInfo) => {
+ const { instanceId, databaseId } = tab.connection;
+ if (instanceId === String(UNKNOWN_ID)) {
+ return true;
+ }
+ const instance = useInstanceV1Store().getInstanceByUID(instanceId);
+ if (instanceV1AllowsCrossDatabaseQuery(instance)) {
+ // Connecting to instance directly.
+ return false;
+ }
+ return databaseId === String(UNKNOWN_ID);
+};
diff --git a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
index 33473e5e3a..682681fdf9 100644
--- a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
@@ -75,7 +75,7 @@ import {
} from "@/types";
import {
emptyConnection,
- getDefaultTabNameFromConnection,
+ getSuggestedTabNameFromConnection,
hasWorkspacePermissionV1,
instanceV1HasReadonlyMode,
instanceOfConnectionAtom,
@@ -223,13 +223,19 @@ const setConnection = (
// Don't go further if the connection doesn't change.
return;
}
- const name = getDefaultTabNameFromConnection(target.connection);
- tabStore.selectOrAddSimilarTab(
- target,
- /* beside */ false,
- /* defaultTabName */ name
- );
- tabStore.updateCurrentTab(target);
+ if (tabStore.currentTab.isFreshNew) {
+ // If the current tab is "fresh new", update its connection directly.
+ tabStore.updateCurrentTab(target);
+ } else {
+ // Otherwise select or add a new tab and set its connection
+ const name = getSuggestedTabNameFromConnection(target.connection);
+ tabStore.selectOrAddSimilarTab(
+ target,
+ /* beside */ false,
+ /* defaultTabName */ name
+ );
+ tabStore.updateCurrentTab(target);
+ }
};
// If selected item is instance node
diff --git a/frontend/src/views/sql-editor/AsidePanel/QueryHistoryContainer.vue b/frontend/src/views/sql-editor/AsidePanel/QueryHistoryContainer.vue
index d594d47186..8138b30955 100644
--- a/frontend/src/views/sql-editor/AsidePanel/QueryHistoryContainer.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/QueryHistoryContainer.vue
@@ -22,9 +22,9 @@
@click="handleQueryHistoryClick(history)"
>
<div class="w-full flex flex-row justify-between items-center">
- <span class="text-xs text-gray-500">{{
- dayjs(history.createTime).format("YYYY-MM-DD HH:mm:ss")
- }}</span>
+ <span class="text-xs text-gray-500">
+ {{ titleOfQueryHistory(history) }}
+ </span>
<span
class="p-1 rounded text-gray-500 hover:text-gray-700 hover:bg-gray-200"
>
@@ -59,6 +59,7 @@
<script lang="ts" setup>
import { useClipboard } from "@vueuse/core";
+import dayjs from "dayjs";
import { escape } from "lodash-es";
import { computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
@@ -119,6 +120,10 @@ const data = computed(() => {
});
});
+const titleOfQueryHistory = (history: QueryHistory) => {
+ return dayjs(history.createTime).format("YYYY-MM-DD HH:mm:ss");
+};
+
const notifyMessage = computed(() => {
if (isLoading.value) {
return "";
@@ -151,7 +156,9 @@ const handleQueryHistoryClick = async (queryHistory: QueryHistory) => {
);
// Open a new tab with the connection and statement.
- tabStore.selectOrAddTempTab();
+ tabStore.addTab({
+ name: `Query history at ${titleOfQueryHistory(queryHistory)}`,
+ });
tabStore.updateCurrentTab({
connection,
statement,
diff --git a/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetItem.vue b/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetItem.vue
index 3090ca99ad..788380e7ea 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetItem.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetItem.vue
@@ -6,13 +6,29 @@
@click="$emit('click', item, $event)"
@contextmenu="$emit('contextmenu', item, $event)"
>
- <div class="flex-1 text-sm cursor-pointer pt-0.5 break-all">
+ <SheetConnectionIcon :sheet="item.target" class="shrink-0 w-4 h-6" />
+ <div class="flex-1 text-sm leading-6 cursor-pointer break-all">
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-if="item.target.title" v-html="titleHTML(item, keyword)" />
<span v-else>
{{ $t("sql-editor.untitled-sheet") }}
</span>
</div>
+
+ <div
+ v-if="unsaved"
+ class="shrink-0 w-4 h-6 flex items-center justify-center"
+ @click.stop
+ >
+ <NTooltip>
+ <template #trigger>
+ <carbon:dot-mark class="text-gray-500 w-4 h-4" />
+ </template>
+ <template #default>
+ <span>{{ $t("sql-editor.tab.unsaved") }}</span>
+ </template>
+ </NTooltip>
+ </div>
<div class="shrink-0" @click.stop>
<Dropdown :sheet="item.target" :view="view" :secondary="true" />
</div>
@@ -20,11 +36,14 @@
</template>
<script setup lang="ts">
+import { computed } from "vue";
+import { useTabStore } from "@/store";
import { Dropdown } from "@/views/sql-editor/Sheet";
import { SheetViewMode } from "@/views/sql-editor/Sheet";
+import { SheetConnectionIcon } from "../../EditorCommon";
import { MergedItem, SheetItem, domIDForItem, titleHTML } from "./common";
-defineProps<{
+const props = defineProps<{
item: SheetItem;
isCurrentItem: boolean;
view: SheetViewMode;
@@ -35,4 +54,17 @@ defineEmits<{
(event: "click", item: MergedItem, e: MouseEvent): void;
(event: "contextmenu", item: MergedItem, e: MouseEvent): void;
}>();
+
+const tabStore = useTabStore();
+
+const unsaved = computed(() => {
+ const tab = tabStore.tabList.find(
+ (tab) => tab.sheetName === props.item.target.name
+ );
+ if (tab) {
+ return !tab.isSaved;
+ }
+ console.assert(false, "should never reach this line");
+ return false;
+});
</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetList.vue b/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetList.vue
index e5ef9fa9a5..3b18b8de61 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetList.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SheetList/SheetList.vue
@@ -81,7 +81,7 @@ import { storeToRefs } from "pinia";
import scrollIntoView from "scroll-into-view-if-needed";
import { computed, nextTick, onMounted, ref, watch } from "vue";
import { useSheetAndTabStore, useTabStore } from "@/store";
-import { Sheet } from "@/types/proto/v1/sheet_service";
+import { getSuggestedTabNameFromConnection } from "@/utils";
import {
SheetViewMode,
openSheet,
@@ -113,18 +113,11 @@ const keyword = ref("");
const { currentSheet } = storeToRefs(useSheetAndTabStore());
const dropdown = ref<DropdownState>();
-const sortedSheetList = computed(() => {
- return orderBy<Sheet>(
- sheetList.value,
- [
- (sheet) => (sheet.title ? 0 : 1), // Untitled sheets go behind.
- (sheet) => sheet.title,
- ],
- ["asc", "asc"]
- );
-});
-
const mergedItemList = computed(() => {
+ if (isLoading.value) {
+ return [];
+ }
+
const { tabList } = tabStore;
const mergedList: MergedItem[] = [];
@@ -139,17 +132,26 @@ const mergedItemList = computed(() => {
}
});
}
- if (!isLoading.value) {
- // Sheets follow
- sortedSheetList.value.forEach((sheet) => {
- mergedList.push({
- type: "SHEET",
- target: sheet,
- });
+ // Sheets follow
+ sheetList.value.forEach((sheet) => {
+ mergedList.push({
+ type: "SHEET",
+ target: sheet,
});
- }
+ });
- return mergedList;
+ const sortedList = orderBy(
+ mergedList,
+ [
+ // Untitled sheets go behind
+ // They are probably dirty data
+ (item) => (item.type === "SHEET" && !item.target.title ? 1 : 0),
+ // Alphabetically otherwise
+ (item) => (item.type === "TAB" ? item.target.name : item.target.title),
+ ],
+ ["asc", "asc"]
+ );
+ return sortedList;
});
const filteredItemList = computed(() => {
@@ -183,7 +185,14 @@ const handleItemClick = (item: MergedItem, e: MouseEvent) => {
};
const addSheet = () => {
- useTabStore().addTab();
+ const connection = { ...tabStore.currentTab.connection };
+ const name = getSuggestedTabNameFromConnection(connection);
+ tabStore.addTab({
+ name,
+ connection,
+ // The newly created tab is "clean" so its connection can be changed
+ isFreshNew: true,
+ });
emit("add-tab");
};
diff --git a/frontend/src/views/sql-editor/AsidePanel/SheetList/TabItem.vue b/frontend/src/views/sql-editor/AsidePanel/SheetList/TabItem.vue
index a190e989c9..97bdd44ced 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SheetList/TabItem.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SheetList/TabItem.vue
@@ -5,15 +5,17 @@
:class="[isCurrentItem && 'bg-indigo-600/10']"
@click="$emit('click', item, $event)"
>
- <div class="flex-1 text-sm cursor-pointer pt-0.5 break-all ital">
+ <SheetConnectionIcon :tab="item.target" class="shrink-0 w-4 h-6" />
+
+ <div class="flex-1 text-sm leading-6 cursor-pointer break-all">
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-html="titleHTML(item, keyword)" />
</div>
- <div class="w-6 h-6 flex items-center justify-center" @click.stop>
+ <div class="shrink-0 w-6 h-6 flex items-center justify-center" @click.stop>
<NTooltip>
<template #trigger>
- <carbon:dot-mark class="text-accent opacity-50 w-4 h-4" />
+ <carbon:dot-mark class="text-accent w-4 h-4" />
</template>
<template #default>
<span>{{ $t("sql-editor.tab.unsaved") }}</span>
@@ -25,6 +27,7 @@
<script setup lang="ts">
import { NTooltip } from "naive-ui";
+import { SheetConnectionIcon } from "../../EditorCommon";
import { MergedItem, TabItem, domIDForItem, titleHTML } from "./common";
defineProps<{
diff --git a/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue b/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue
index 976c03186a..a89a35d3c7 100644
--- a/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue
@@ -17,7 +17,7 @@ import { computed } from "vue";
import { useCurrentUserV1, useTabStore, useWebTerminalStore } from "@/store";
import { TabMode } from "@/types";
import {
- getDefaultTabNameFromConnection,
+ getSuggestedTabNameFromConnection,
hasWorkspacePermissionV1,
} from "@/utils";
@@ -54,7 +54,7 @@ const enterAdminMode = () => {
const target = {
connection: current.connection,
mode: TabMode.Admin,
- name: getDefaultTabNameFromConnection(current.connection),
+ name: getSuggestedTabNameFromConnection(current.connection),
};
tabStore.selectOrAddSimilarTab(target, /* beside */ true);
tabStore.updateCurrentTab({
diff --git a/frontend/src/views/sql-editor/EditorCommon/ConnectionPathBar.vue b/frontend/src/views/sql-editor/EditorCommon/ConnectionPathBar.vue
index d89e250c3f..dce0139d8f 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ConnectionPathBar.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ConnectionPathBar.vue
@@ -1,9 +1,7 @@
<template>
- <div
- v-if="!tabStore.isDisconnected"
- class="w-full block lg:flex justify-between items-start bg-white"
- >
+ <div class="w-full block lg:flex justify-between items-start bg-white">
<div
+ v-if="!tabStore.isDisconnected"
class="flex justify-start items-center h-8 px-4 whitespace-nowrap overflow-x-auto"
>
<NPopover v-if="showReadonlyDatasourceWarning" trigger="hover">
@@ -71,6 +69,15 @@
>
{{ $t("sql-editor.sql-execute-in-production-environment") }}
</div>
+
+ <div
+ v-if="tabStore.isDisconnected && !currentTab.sheetName"
+ class="flex justify-start items-center h-8 px-4 whitespace-nowrap overflow-x-auto"
+ >
+ <div class="text-sm text-control">
+ {{ $t("sql-editor.connection-holder") }}
+ </div>
+ </div>
</div>
</template>
@@ -90,8 +97,8 @@ import { instanceV1Slug } from "@/utils";
const router = useRouter();
const tabStore = useTabStore();
-
-const connection = computed(() => tabStore.currentTab.connection);
+const currentTab = computed(() => tabStore.currentTab);
+const connection = computed(() => currentTab.value.connection);
const { instance: selectedInstance } = useInstanceV1ByUID(
computed(() => connection.value.instanceId)
@@ -108,6 +115,10 @@ const selectedEnvironment = computed(() => {
});
const isProductionEnvironment = computed(() => {
+ if (tabStore.isDisconnected) {
+ return false;
+ }
+
return selectedEnvironment.value.tier === EnvironmentTier.PROTECTED;
});
diff --git a/frontend/src/views/sql-editor/EditorCommon/DisconnectedIcon.vue b/frontend/src/views/sql-editor/EditorCommon/DisconnectedIcon.vue
new file mode 100644
index 0000000000..dd396e2321
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/DisconnectedIcon.vue
@@ -0,0 +1,3 @@
+<template>
+ <octicon:unlink-16 class="w-3.5 h-3.5 text-control opacity-50" />
+</template>
diff --git a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
index e84e0a6da3..51eda76684 100644
--- a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
@@ -19,9 +19,9 @@ import {
Sheet,
} from "@/types/proto/v1/sheet_service";
import {
- defaultTabName,
extractSheetUID,
- getDefaultTabNameFromConnection,
+ getSuggestedTabNameFromConnection,
+ isSimilarDefaultTabName,
} from "@/utils";
import { useSheetContext } from "../Sheet";
import SaveSheetForm from "./SaveSheetForm.vue";
@@ -105,8 +105,8 @@ const needSheetName = (sheetName: string | undefined) => {
if (!sheetName) {
const name = tab.name;
if (
- name === getDefaultTabNameFromConnection(tab.connection) ||
- name === defaultTabName.value
+ name === getSuggestedTabNameFromConnection(tab.connection) ||
+ isSimilarDefaultTabName(name)
) {
// The tab is unsaved and its name is still the default one.
return true;
diff --git a/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue b/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue
new file mode 100644
index 0000000000..2e191ac405
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue
@@ -0,0 +1,33 @@
+<template>
+ <div class="inline-flex items-center justify-center">
+ <EngineIcon v-if="instance" :engine="instance.engine" />
+ <octicon:unlink-16 v-else class="w-3.5 h-3.5 text-control opacity-50" />
+ </div>
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import { EngineIcon } from "@/components/Icon";
+import { useDatabaseV1Store } from "@/store";
+import { TabInfo } from "@/types";
+import { Sheet } from "@/types/proto/v1/sheet_service";
+import { connectionForTab } from "@/utils";
+
+const props = defineProps<{
+ sheet?: Sheet;
+ tab?: TabInfo;
+}>();
+
+const instance = computed(() => {
+ const { sheet, tab } = props;
+ if (sheet) {
+ if (!sheet.database) return undefined;
+ return useDatabaseV1Store().getDatabaseByName(sheet.database)
+ .instanceEntity;
+ }
+ if (tab) {
+ return connectionForTab(tab).instance;
+ }
+ return undefined;
+});
+</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/index.ts b/frontend/src/views/sql-editor/EditorCommon/index.ts
index 807308adfa..e0c0dbbd7a 100644
--- a/frontend/src/views/sql-editor/EditorCommon/index.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/index.ts
@@ -1,10 +1,12 @@
import ConnectionHolder from "./ConnectionHolder.vue";
import ConnectionPathBar from "./ConnectionPathBar.vue";
+import DisconnectedIcon from "./DisconnectedIcon.vue";
import EditorAction from "./EditorAction.vue";
import ExecutingHintModal from "./ExecutingHintModal.vue";
import { ResultViewV1 } from "./ResultView";
import SaveSheetModal from "./SaveSheetModal.vue";
import SharePopover from "./SharePopover.vue";
+import SheetConnectionIcon from "./SheetConnectionIcon.vue";
export {
ConnectionHolder,
@@ -14,4 +16,6 @@ export {
SaveSheetModal,
SharePopover,
ResultViewV1,
+ DisconnectedIcon,
+ SheetConnectionIcon,
};
diff --git a/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue b/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
index 67cb66e879..476533a6d7 100644
--- a/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
@@ -7,16 +7,7 @@
<SheetForIssueTipsBar />
- <template
- v-if="
- !tabStore.isDisconnected || isSheetOversize || sheetBacktracePayload
- "
- >
- <SQLEditor @execute="handleExecute" @save-sheet="trySaveSheet" />
- </template>
- <template v-else>
- <ConnectionHolder />
- </template>
+ <SQLEditor @execute="handleExecute" @save-sheet="trySaveSheet" />
</template>
<AIChatToSQL
@@ -31,21 +22,15 @@
</template>
<script lang="ts" setup>
-import { computed, ref } from "vue";
+import { ref } from "vue";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
import { AIChatToSQL } from "@/plugins/ai";
-import {
- useCurrentTab,
- useInstanceV1Store,
- useSheetV1Store,
- useTabStore,
-} from "@/store";
+import { useCurrentTab, useInstanceV1Store, useTabStore } from "@/store";
import type { Connection, ExecuteConfig, ExecuteOption } from "@/types";
-import { formatEngineV1, getSheetIssueBacktracePayloadV1 } from "@/utils";
+import { formatEngineV1 } from "@/utils";
import {
EditorAction,
ConnectionPathBar,
- ConnectionHolder,
ExecutingHintModal,
SaveSheetModal,
} from "../EditorCommon";
@@ -53,22 +38,9 @@ import SQLEditor from "./SQLEditor.vue";
import SheetForIssueTipsBar from "./SheetForIssueTipsBar.vue";
const tabStore = useTabStore();
-const sheetV1Store = useSheetV1Store();
const saveSheetModal = ref<InstanceType<typeof SaveSheetModal>>();
const tab = useCurrentTab();
-const sheet = computed(() => {
- const sheetName = tabStore.currentTab.sheetName;
- if (!sheetName) return undefined;
- const sheet = sheetV1Store.getSheetByName(sheetName);
- return sheet;
-});
-
-const sheetBacktracePayload = computed(() => {
- if (!sheet.value) return undefined;
- return getSheetIssueBacktracePayloadV1(sheet.value);
-});
-
const { executeReadonly } = useExecuteSQL();
const handleExecute = (
@@ -99,15 +71,4 @@ const handleApplyStatement = async (
});
}
};
-
-const isSheetOversize = computed(() => {
- if (!sheet.value) {
- return false;
- }
-
- return (
- new TextDecoder().decode(sheet.value.content).length <
- sheet.value.contentSize
- );
-});
</script>
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index a6aba869f1..a6736a1d10 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -130,7 +130,7 @@ export const openSheet = async (sheet: Sheet, forceNewTab = false) => {
tabStore.addTab(newTab, true /* beside */);
} else {
// Open the sheet in a "temp" tab otherwise.
- tabStore.selectOrAddTempTab(newTab);
+ tabStore.addTab(newTab);
}
let insId = String(UNKNOWN_ID);
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue b/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue
index 5470a21bb9..000515ba28 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue
@@ -1,4 +1,6 @@
<template>
+ <SheetConnectionIcon :tab="tab" class="w-4 h-4" />
+
<template v-if="sheet">
<heroicons-outline:user-group
v-if="sheet.visibility === Sheet_Visibility.VISIBILITY_PROJECT"
@@ -20,6 +22,7 @@ import { useSheetV1Store } from "@/store";
import type { TabInfo } from "@/types";
import { TabMode } from "@/types";
import { Sheet_Visibility } from "@/types/proto/v1/sheet_service";
+import { SheetConnectionIcon } from "../../EditorCommon";
const props = defineProps({
tab: {
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue b/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
index ca3919953a..4d140d3639 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
@@ -1,9 +1,14 @@
<template>
<div
class="suffix"
- :class="{
- admin: tab.mode === TabMode.Admin,
- }"
+ :class="[
+ {
+ admin: tab.mode === TabMode.Admin,
+ closable,
+ },
+
+ [sheetTypeForTab(tab).toLowerCase()],
+ ]"
@mouseenter="state.hovering = true"
@mouseleave="state.hovering = false"
>
@@ -22,6 +27,7 @@ import { computed, PropType, reactive } from "vue";
import { useTabStore } from "@/store";
import type { TabInfo } from "@/types";
import { TabMode } from "@/types";
+import { sheetTypeForTab } from "@/utils";
type LocalState = {
hovering: boolean;
@@ -50,14 +56,22 @@ defineEmits<{
const tabStore = useTabStore();
+const closable = computed(() => {
+ const { tabList } = tabStore;
+ if (tabList.length > 1) {
+ // Not the only one tab
+ return true;
+ }
+ if (tabList.length === 1) {
+ // It's the only one tab, and it's closable if it's a sheet tab
+ return !!props.tab.sheetName;
+ }
+ return false;
+});
+
const icon = computed((): IconType | undefined => {
- if (state.hovering) {
- if (tabStore.tabList.length > 1) {
- // Show 'close' if
- // - hovering
- // - and closeable (not the only one tab)
- return "close";
- }
+ if (state.hovering && closable.value) {
+ return "close";
}
if (props.tab.mode === TabMode.ReadOnly && !props.tab.isSaved) {
return "unsaved";
@@ -71,7 +85,14 @@ const icon = computed((): IconType | undefined => {
@apply flex items-center min-w-[1.25rem];
}
.icon {
- @apply block w-5 h-5 p-0.5 text-gray-500 hover:text-gray-700 hover:bg-gray-200 rounded;
+ @apply block w-5 h-5 p-0.5 text-gray-500 rounded;
+}
+
+.suffix.closable.temp .icon {
+ @apply text-accent;
+}
+.suffix.closable .icon {
+ @apply hover:text-gray-700 hover:bg-gray-200;
}
.suffix.admin .icon {
@apply text-gray-400 hover:text-gray-300 hover:bg-gray-400/30;
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue b/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
index 3866070e6d..8449a06601 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
@@ -1,12 +1,14 @@
<template>
<div
class="tab-item"
- :class="{
- current: isCurrentTab,
- temp: isTempTab(tab),
- hovering: state.hovering,
- admin: tab.mode === TabMode.Admin,
- }"
+ :class="[
+ {
+ current: isCurrentTab,
+ hovering: state.hovering,
+ admin: tab.mode === TabMode.Admin,
+ },
+ [sheetTypeForTab(tab).toLowerCase()],
+ ]"
@mousedown="$emit('select', tab, index)"
@mouseenter="state.hovering = true"
@mouseleave="state.hovering = false"
@@ -26,7 +28,7 @@ import { computed, PropType, reactive } from "vue";
import { useTabStore } from "@/store";
import type { TabInfo } from "@/types";
import { TabMode } from "@/types";
-import { isTempTab } from "@/utils";
+import { sheetTypeForTab } from "@/utils";
import AdminLabel from "./AdminLabel.vue";
import Label from "./Label.vue";
import Prefix from "./Prefix.vue";
diff --git a/frontend/src/views/sql-editor/TabList/TabList.vue b/frontend/src/views/sql-editor/TabList/TabList.vue
index 0d3d6a4488..40ddae8d4d 100644
--- a/frontend/src/views/sql-editor/TabList/TabList.vue
+++ b/frontend/src/views/sql-editor/TabList/TabList.vue
@@ -57,6 +57,7 @@ import Draggable from "vuedraggable";
import { useTabStore } from "@/store";
import type { TabInfo } from "@/types";
import { TabMode } from "@/types";
+import { getSuggestedTabNameFromConnection } from "@/utils";
import { useSheetContext } from "../Sheet";
import TabItem from "./TabItem";
@@ -88,7 +89,14 @@ const handleSelectTab = async (tab: TabInfo) => {
};
const handleAddTab = () => {
- tabStore.addTab();
+ const connection = { ...tabStore.currentTab.connection };
+ const name = getSuggestedTabNameFromConnection(connection);
+ tabStore.addTab({
+ name,
+ connection,
+ // The newly created tab is "clean" so its connection can be changed
+ isFreshNew: true,
+ });
nextTick(recalculateScrollState);
};
@@ -118,7 +126,10 @@ const handleRemoveTab = async (tab: TabInfo, index: number) => {
}
function remove(index: number) {
- if (tabStore.tabList.length <= 1) return;
+ if (tabStore.tabList.length === 1) {
+ // Ensure at least 1 tab
+ tabStore.addTab();
+ }
tabStore.removeTab(tab);