Skip to content

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

chore: optimize sql editor (#18382)

ecmadao2a9c9318604d
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 79ea3332b4..d1c19d7d8f 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2061,7 +2061,7 @@
},
"copy-url": "Copy URL",
"url-copied-to-clipboard": "The URL is copied to your clipboard",
- "connect-to-a-database": "Connect to a database",
+ "connect-to-a-database": "Connect to database",
"link-access": "Link access",
"private": "Private",
"private-desc": "Only you can access this sheet",
@@ -2150,7 +2150,7 @@
"result-limit": {
"self": "Result limit"
},
- "add-a-new-instance": "Add a new instance",
+ "add-a-new-instance": "Add new instance",
"web-socket": {
"errors": {
"title": "WebSocket connection failed",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index a324984b2a..fd4df12b25 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2061,7 +2061,7 @@
},
"copy-url": "Copiar URL",
"url-copied-to-clipboard": "La URL se copia al portapapeles.",
- "connect-to-a-database": "Conectarse a una base de datos",
+ "connect-to-a-database": "Conectar a base de datos",
"link-access": "Acceso al enlace",
"private": "Privado",
"private-desc": "Solo tú puedes acceder a esta hoja",
@@ -2150,7 +2150,7 @@
"result-limit": {
"self": "Límite de resultados"
},
- "add-a-new-instance": "Agregar una nueva instancia",
+ "add-a-new-instance": "Agregar nueva instancia",
"web-socket": {
"errors": {
"title": "La conexión WebSocket falló",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 1a84516c04..addfd5e9ab 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -2061,7 +2061,7 @@
},
"copy-url": "URLをコピー",
"url-copied-to-clipboard": "URLがクリップボードにコピーされました",
- "connect-to-a-database": "データベースに接続する",
+ "connect-to-a-database": "データベースに接続",
"link-access": "リンクアクセス",
"private": "プライベート",
"private-desc": "このシートにアクセスできるのはあなただけです",
@@ -2150,7 +2150,7 @@
"result-limit": {
"self": "結果の制限"
},
- "add-a-new-instance": "新しいインスタンスを追加する",
+ "add-a-new-instance": "新しいインスタンスを追加",
"web-socket": {
"errors": {
"title": "WebSocket接続に失敗しました",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 0203a834d2..2d4e98324e 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -2061,7 +2061,7 @@
},
"copy-url": "Sao chép URL",
"url-copied-to-clipboard": "URL đã được sao chép vào bảng nhớ tạm của bạn",
- "connect-to-a-database": "Kết nối với cơ sở dữ liệu",
+ "connect-to-a-database": "Kết nối cơ sở dữ liệu",
"link-access": "Truy cập liên kết",
"private": "Riêng tư",
"private-desc": "Chỉ bạn mới có thể truy cập trang tính này",
@@ -2150,7 +2150,7 @@
"result-limit": {
"self": "Giới hạn kết quả"
},
- "add-a-new-instance": "Thêm một phiên bản mới",
+ "add-a-new-instance": "Thêm phiên bản mới",
"web-socket": {
"errors": {
"title": "Kết nối WebSocket không thành công",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 8326a8df01..6ced687adf 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2061,7 +2061,7 @@
},
"copy-url": "复制 URL",
"url-copied-to-clipboard": "URL 已复制到剪贴板",
- "connect-to-a-database": "连接到数据库",
+ "connect-to-a-database": "连接数据库",
"link-access": "链接访问权限",
"private": "私有",
"private-desc": "只有您自己可以访问此工作表",
@@ -2150,7 +2150,7 @@
"result-limit": {
"self": "结果限制"
},
- "add-a-new-instance": "添加实例",
+ "add-a-new-instance": "添加新实例",
"web-socket": {
"errors": {
"title": "WebSocket 连接失败",
diff --git a/frontend/src/store/modules/sqlEditor/tab.ts b/frontend/src/store/modules/sqlEditor/tab.ts
index 9d5ffef08f..9c1d9e30c6 100644
--- a/frontend/src/store/modules/sqlEditor/tab.ts
+++ b/frontend/src/store/modules/sqlEditor/tab.ts
@@ -17,7 +17,7 @@ import {
emptySQLEditorConnection,
extractWorksheetConnection,
getSheetStatement,
- isDisconnectedSQLEditorTab,
+ isConnectedSQLEditorTab,
isSimilarSQLEditorTab,
useDynamicLocalStorage,
} from "@/utils";
@@ -403,7 +403,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
const curr = currentTab.value;
if (curr) {
if (
- isDisconnectedSQLEditorTab(curr) ||
+ !isConnectedSQLEditorTab(curr) ||
isSimilarSQLEditorTab(tab, curr, ignoreMode)
) {
updateTab(curr.id, {
@@ -442,7 +442,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
const isDisconnected = computed(() => {
const tab = currentTab.value;
if (!tab) return true;
- return isDisconnectedSQLEditorTab(tab);
+ return !isConnectedSQLEditorTab(tab);
});
return {
diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.ts
index b1e02d0d29..118f015a5e 100644
--- a/frontend/src/utils/sqlEditor.ts
+++ b/frontend/src/utils/sqlEditor.ts
@@ -3,7 +3,6 @@ import { head } from "lodash-es";
import { v1 as uuidv1 } from "uuid";
import {
useDatabaseV1Store,
- useInstanceResourceByName,
usePolicyV1Store,
useSQLEditorTabStore,
} from "@/store";
@@ -118,17 +117,20 @@ export const suggestedTabTitleForSQLEditorConnection = (
return parts.join(" ");
};
-export const isDisconnectedSQLEditorTab = (tab: SQLEditorTab) => {
- const { connection } = tab;
- if (!connection.instance) {
- return true;
+export const isConnectedSQLEditorTab = (tab: SQLEditorTab) => {
+ const { instance, database } = connectionForSQLEditorTab(tab);
+ if (!instance) {
+ return false;
}
- const { instance } = useInstanceResourceByName(connection.instance);
- if (instanceV1AllowsCrossDatabaseQuery(instance.value)) {
- // Connecting to instance directly.
+ if (!isValidInstanceName(instance.name)) {
return false;
}
- return connection.database === "";
+
+ if (instanceV1AllowsCrossDatabaseQuery(instance)) {
+ // Connecting to instance directly.
+ return true;
+ }
+ return database && isValidDatabaseName(database.name);
};
export const tryConnectToCoreSQLEditorTab = (
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 373ee17856..65df811a26 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
@@ -5,45 +5,16 @@
</div>
<template v-else>
- <div v-if="checkable" class="px-1 flex items-center justify-start flex-wrap gap-y-1 gap-x-1 bg-blue-100 py-2">
- <NButton
- quaternary
- size="tiny"
- :disabled="checkedNodes.length === 0" @click="handleMultiDelete"
- >
- <template #icon>
- <TrashIcon />
- </template>
- {{ t("common.delete") }}
- </NButton>
- <NButton
- quaternary
- size="tiny"
- :disabled="checkedWorksheets.length === 0"
- @click="showReorgModal = true"
- >
- <template #icon>
- <FolderInputIcon />
- </template>
- {{ $t('sheet.move-worksheets') }}
- </NButton>
- <NButton quaternary size="tiny" @click="checkable = false">
- <template #icon>
- <XIcon />
- </template>
- {{$t("common.cancel")}}
- </NButton>
- </div>
<NTree
block-line
block-node
:keyboard="false"
- :draggable="!editingNode && !checkable"
+ :draggable="!editingNode && !multiSelectMode"
:data="treeData"
:multiple="false"
cascade
:selectable="true"
- :checkable="checkable"
+ :checkable="multiSelectMode"
:show-irrelevant-nodes="false"
:filter="filterNode(folderContext.rootPath.value)"
:pattern="worksheetFilter.keyword"
@@ -87,34 +58,13 @@
@on-updated="handleContextMenuClickOutside"
/>
</NPopover>
-
- <BBModal
- :show="showReorgModal"
- :title="$t('sheet.move-worksheets')"
- @close="() => showReorgModal = false"
- >
- <div class="flex flex-col gap-y-3 w-lg max-w-[calc(100vw-8rem)]">
- <FolderForm ref="folderFormRef" :folder="''" />
- <div class="flex justify-end gap-x-2 mt-4">
- <NButton @click="showReorgModal = false">{{ $t("common.close") }}</NButton>
- <NButton
- type="primary"
- @click="handleMoveWorksheets"
- >
- {{ $t("common.save") }}
- </NButton>
- </div>
- </div>
- </BBModal>
</div>
</template>
<script setup lang="tsx">
import { create } from "@bufbuild/protobuf";
import { useDebounceFn } from "@vueuse/core";
-import { FolderInputIcon, TrashIcon, XIcon } from "lucide-vue-next";
import {
- NButton,
NDropdown,
NInput,
NPopover,
@@ -124,8 +74,8 @@ import {
useDialog,
} from "naive-ui";
import { storeToRefs } from "pinia";
-import { computed, nextTick, ref, watch } from "vue";
-import { BBModal, BBSpin } from "@/bbkit";
+import { computed, nextTick, watch } from "vue";
+import { BBSpin } from "@/bbkit";
import { HighlightLabelText } from "@/components/v2";
import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import { t } from "@/plugins/i18n";
@@ -155,12 +105,24 @@ import {
} from "@/views/sql-editor/Sheet";
import { filterNode } from "./common";
import { type DropdownOptionType, useDropdown } from "./dropdown";
-import FolderForm from "./FolderForm.vue";
import TreeNodePrefix from "./TreeNodePrefix.vue";
import TreeNodeSuffix from "./TreeNodeSuffix.vue";
-const props = defineProps<{
- view: SheetViewMode;
+const props = withDefaults(
+ defineProps<{
+ view: SheetViewMode;
+ multiSelectMode?: boolean;
+ checkedNodes?: WorksheetFolderNode[];
+ }>(),
+ {
+ multiSelect: false,
+ checkedNodes: () => [],
+ }
+);
+
+const emit = defineEmits<{
+ (event: "update:multiSelectMode", multiSelect: boolean): void;
+ (event: "update:checkedNodes", nodes: WorksheetFolderNode[]): void;
}>();
const worksheetV1Store = useWorkSheetStore();
@@ -201,28 +163,16 @@ const {
const expandedKeysArray = computed(() => Array.from(expandedKeys.value));
const treeData = computed(() => [sheetTree.value]);
-
-// multi-select operations
-const checkedNodes = ref<WorksheetFolderNode[]>([]);
-const checkedKeys = computed(() => checkedNodes.value.map((node) => node.key));
-const checkable = ref(false);
-const showReorgModal = ref(false);
-const checkedWorksheets = computed(() => {
- const worksheets: string[] = [];
- for (const node of checkedNodes.value) {
- if (node.worksheet) {
- worksheets.push(node.worksheet.name);
- }
- }
- return worksheets;
-});
-const folderFormRef = ref<InstanceType<typeof FolderForm>>();
+const checkedKeys = computed(() => props.checkedNodes.map((node) => node.key));
const onCheckedKeysUpdate = (
_: Array<string | number>,
options: Array<TreeOption | null>
) => {
- checkedNodes.value = options.filter((node) => node) as WorksheetFolderNode[];
+ emit(
+ "update:checkedNodes",
+ options.filter((node) => node) as WorksheetFolderNode[]
+ );
};
const onExpandedKeysUpdate = (keys: string[]) => {
@@ -233,15 +183,6 @@ const onExpandedKeysUpdate = (keys: string[]) => {
expandedKeys.value = new Set(keys);
};
-watch(
- () => checkable.value,
- (check) => {
- if (!check) {
- checkedNodes.value = [];
- }
- }
-);
-
watch(
isInitialized,
async () => {
@@ -434,11 +375,7 @@ const nodeProps = ({ option }: { option: TreeOption }) => {
}
if (node.worksheet) {
if (node.worksheet.type === "worksheet") {
- openWorksheetByName(
- node.worksheet.name,
- editorContext,
- e.metaKey || e.ctrlKey
- );
+ openWorksheetByName(node.worksheet.name, e.metaKey || e.ctrlKey);
} else {
const tab = tabStore.draftList.find(
(draft) => draft.id === node.worksheet?.name
@@ -473,22 +410,15 @@ const deleteWorksheets = async (worksheets: string[]) => {
}
};
-const handleMoveWorksheets = async () => {
- const folders = folderFormRef.value?.folders ?? [];
- await batchUpdateWorksheetFolders(
- checkedWorksheets.value.map((worksheet) => ({
- name: worksheet,
- folders,
- }))
- );
- showReorgModal.value = false;
- checkable.value = false;
-};
-
-const handleMultiDelete = async () => {
+const handleMultiDelete = async (nodes: WorksheetFolderNode[]) => {
const folders: string[] = [];
- for (const node of checkedNodes.value) {
- if (node.key === folderContext.rootPath.value || node.worksheet) {
+ const worksheets: string[] = [];
+ for (const node of nodes) {
+ if (node.worksheet) {
+ worksheets.push(node.worksheet.name);
+ continue;
+ }
+ if (node.key === folderContext.rootPath.value) {
continue;
}
if (
@@ -503,11 +433,9 @@ const handleMultiDelete = async () => {
}
folders.push(node.key);
}
- const removed = await handleDeleteFolders(folders, [
- ...checkedWorksheets.value,
- ]);
+ const removed = await handleDeleteFolders(folders, worksheets);
if (removed) {
- checkable.value = false;
+ emit("update:multiSelectMode", false);
}
};
@@ -746,29 +674,16 @@ const handleContextMenuSelect = async (key: DropdownOptionType) => {
};
break;
case "add-worksheet":
- const newWorksheet = await worksheetV1Store.createWorksheet(
- create(WorksheetSchema, {
- title: "new worksheet",
- project: project.value,
- visibility: Worksheet_Visibility.PRIVATE,
- })
- );
- await worksheetV1Store.upsertWorksheetOrganizer(
- {
- worksheet: newWorksheet.name,
- folders: getFoldersForWorksheet(contextMenuContext.node.key),
- },
- ["folders"]
- );
-
- nextTick(() => {
- openWorksheetByName(newWorksheet.name, editorContext, true);
- editorContext.showConnectionPanel.value = true;
+ await editorContext.createWorksheet({
+ folders: getFoldersForWorksheet(contextMenuContext.node.key),
});
break;
case "multi-select":
- checkable.value = true;
- checkedNodes.value = revealNodes(contextMenuContext.node, (n) => n);
+ emit("update:multiSelectMode", true);
+ emit(
+ "update:checkedNodes",
+ revealNodes(contextMenuContext.node, (n) => n)
+ );
break;
default:
break;
@@ -926,6 +841,10 @@ const handleDrop = async ({ node, dragNode }: TreeDropInfo) => {
}
});
};
+
+defineExpose({
+ handleMultiDelete,
+});
</script>
<style lang="postcss" scoped>
diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts
index ec553de934..0ef73faa71 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts
@@ -150,6 +150,11 @@ export const useDropdown = (
}
);
}
+ items.push({
+ icon: ListCheckIcon,
+ key: "multi-select",
+ label: t("sql-editor.tab.context-menu.actions.multi-select"),
+ });
} else {
if (allowCreateNew.value) {
items.push({
diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/WorksheetPane.vue b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/WorksheetPane.vue
index 90ad125e7e..e52ec19bb3 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/WorksheetPane.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/WorksheetPane.vue
@@ -25,26 +25,121 @@
</NButton>
</NDropdown>
</div>
- <div class="flex-1 flex flex-col gap-y-2 overflow-y-auto worksheet-scroll">
+ <div class="relative flex-1 flex flex-col gap-y-2 overflow-y-auto worksheet-scroll">
+ <div v-if="multiSelectMode && filter.showMine" class="sticky top-0 z-10 px-1 flex items-center justify-start flex-wrap gap-y-1 gap-x-1 bg-blue-100 py-2">
+ <NButton
+ quaternary
+ size="tiny"
+ type="error"
+ :disabled="checkedNodes.length === 0 || loading"
+ @click="handleMultiDelete"
+ >
+ <template #icon>
+ <TrashIcon />
+ </template>
+ {{ t("common.delete") }}
+ </NButton>
+ <NButton
+ quaternary
+ size="tiny"
+ :disabled="checkedWorksheets.length === 0 || loading"
+ @click="showReorgModal = true"
+ >
+ <template #icon>
+ <FolderInputIcon />
+ </template>
+ {{ $t('sheet.move-worksheets') }}
+ </NButton>
+ <NButton
+ quaternary
+ size="tiny"
+ :disabled="loading"
+ @click="multiSelectMode = false"
+ >
+ <template #icon>
+ <XIcon />
+ </template>
+ {{$t("common.cancel")}}
+ </NButton>
+ </div>
+ <SheetTree
+ v-if="filter.showMine"
+ ref="mineSheetTreeRef"
+ key="my"
+ :view="'my'"
+ v-model:multi-select-mode="multiSelectMode"
+ v-model:checked-nodes="checkedNodes"
+ />
<SheetTree v-for="view in views" :key="view" :view="view" />
<NEmpty v-if="views.length === 0" class="mt-10" />
</div>
</div>
+
+ <BBModal
+ :show="showReorgModal"
+ :title="$t('sheet.move-worksheets')"
+ @close="() => showReorgModal = false"
+ >
+ <div class="flex flex-col gap-y-3 w-lg max-w-[calc(100vw-8rem)]">
+ <FolderForm ref="folderFormRef" :folder="''" />
+ <div class="flex justify-end gap-x-2 mt-4">
+ <NButton @click="showReorgModal = false">{{ $t("common.close") }}</NButton>
+ <NButton
+ type="primary"
+ @click="handleMoveWorksheets"
+ >
+ {{ $t("common.save") }}
+ </NButton>
+ </div>
+ </div>
+ </BBModal>
</template>
<script setup lang="tsx">
-import { FunnelIcon } from "lucide-vue-next";
+import { FolderInputIcon, FunnelIcon, TrashIcon, XIcon } from "lucide-vue-next";
import { type DropdownOption, NButton, NDropdown, NEmpty } from "naive-ui";
-import { computed, ref } from "vue";
+import { computed, ref, watch } from "vue";
+import { BBModal } from "@/bbkit";
import { SearchBox } from "@/components/v2";
import { t } from "@/plugins/i18n";
-import { type SheetViewMode } from "@/views/sql-editor/Sheet";
+import type {
+ SheetViewMode,
+ WorksheetFolderNode,
+} from "@/views/sql-editor/Sheet";
import { useSheetContext } from "../../Sheet";
import FilterMenuItem from "./FilterMenuItem.vue";
import { SheetTree } from "./SheetList";
+import FolderForm from "./SheetList/FolderForm.vue";
-const { filter, filterChanged } = useSheetContext();
+const { filter, filterChanged, batchUpdateWorksheetFolders } =
+ useSheetContext();
const showDropdown = ref<boolean>(false);
+const mineSheetTreeRef = ref<InstanceType<typeof SheetTree>>();
+
+// multi-select operations
+const checkedNodes = ref<WorksheetFolderNode[]>([]);
+const multiSelectMode = ref(false);
+const showReorgModal = ref(false);
+const loading = ref(false);
+const checkedWorksheets = computed(() => {
+ const worksheets: string[] = [];
+ for (const node of checkedNodes.value) {
+ if (node.worksheet) {
+ worksheets.push(node.worksheet.name);
+ }
+ }
+ return worksheets;
+});
+const folderFormRef = ref<InstanceType<typeof FolderForm>>();
+
+watch(
+ [() => multiSelectMode.value, () => filter.value.showMine],
+ ([multiSelectMode, showMine]) => {
+ if (!multiSelectMode || !showMine) {
+ checkedNodes.value = [];
+ }
+ }
+);
const options = computed((): DropdownOption[] => {
return [
@@ -98,10 +193,8 @@ const options = computed((): DropdownOption[] => {
});
const views = computed((): SheetViewMode[] => {
+ // do not push the "my" view
const results: SheetViewMode[] = [];
- if (filter.value.showMine) {
- results.push("my");
- }
if (filter.value.showShared) {
results.push("shared");
}
@@ -110,6 +203,32 @@ const views = computed((): SheetViewMode[] => {
}
return results;
});
+
+const handleMoveWorksheets = async () => {
+ loading.value = true;
+ try {
+ const folders = folderFormRef.value?.folders ?? [];
+ await batchUpdateWorksheetFolders(
+ checkedWorksheets.value.map((worksheet) => ({
+ name: worksheet,
+ folders,
+ }))
+ );
+ showReorgModal.value = false;
+ multiSelectMode.value = false;
+ } finally {
+ loading.value = false;
+ }
+};
+
+const handleMultiDelete = async () => {
+ loading.value = true;
+ try {
+ await mineSheetTreeRef.value?.handleMultiDelete(checkedNodes.value);
+ } finally {
+ loading.value = false;
+ }
+};
</script>
<style lang="postcss" scoped>
diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/actions.tsx b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/actions.tsx
index cbc360c841..43a50b6cd5 100644
--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/actions.tsx
+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/actions.tsx
@@ -20,7 +20,6 @@ import {
type SQLEditorTreeNode as TreeNode,
} from "@/types";
import {
- emptySQLEditorConnection,
extractInstanceResourceName,
extractProjectResourceName,
hasWorkspacePermissionV2,
@@ -170,8 +169,8 @@ export const setConnection = (
options: {
extra?: { worksheet: string; mode: TabMode };
newTab?: boolean;
- context?: SQLEditorContext;
- } = {}
+ context: SQLEditorContext;
+ }
) => {
if (!node) {
return;
@@ -188,15 +187,15 @@ export const setConnection = (
newTab = false,
context,
} = options;
+ const database = node.meta.target;
const coreTab: CoreSQLEditorTab = {
- connection: emptySQLEditorConnection(),
+ connection: {
+ instance: database.instance,
+ database: database.name,
+ },
...extra,
};
- const conn = coreTab.connection;
- const database = node.meta.target;
- conn.instance = database.instance;
- conn.database = database.name;
- setDefaultDataSourceForConn(conn, database);
+ setDefaultDataSourceForConn(coreTab.connection, database);
tryConnectToCoreSQLEditorTab(coreTab, /* overrideTitle */ true, newTab);
if (context) {
diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPanel.vue b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPanel.vue
index e175f55a0d..d53b75b034 100644
--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPanel.vue
+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPanel.vue
@@ -19,12 +19,12 @@
<template #trigger>
<NButton
quaternary
- size="tiny"
+ size="small"
style="--n-padding: 0 4px"
@click="$router.push({ name: INSTANCE_ROUTE_DASHBOARD })"
>
<template #icon>
- <SettingsIcon class="w-4 h-4" />
+ <SettingsIcon class="w-4" />
</template>
</NButton>
</template>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ConnectionHolder.vue b/frontend/src/views/sql-editor/EditorCommon/ConnectionHolder.vue
index 7cee9812de..20951ed622 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ConnectionHolder.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ConnectionHolder.vue
@@ -12,7 +12,7 @@
@click="changeConnection"
>
<template #icon>
- <heroicons-outline:lightning-bolt class="w-5 h-5" />
+ <LinkIcon class="w-5 h-5" />
</template>
{{ $t("sql-editor.connect-to-a-database") }}
</NButton>
@@ -21,6 +21,7 @@
</template>
<script setup lang="ts">
+import { LinkIcon } from "lucide-vue-next";
import { NButton } from "naive-ui";
import { useSQLEditorContext } from "../context";
diff --git a/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue b/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue
index 312dc36cf4..bcfd3b4912 100644
--- a/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/SheetConnectionIcon.vue
@@ -1,5 +1,5 @@
<template>
- <EngineIcon v-if="instance" :engine="instance.engine" />
+ <EngineIcon v-if="connected && instance" :engine="instance.engine" />
<UnlinkIcon v-else />
</template>
@@ -7,25 +7,14 @@
import { UnlinkIcon } from "lucide-vue-next";
import { computed } from "vue";
import { EngineIcon } from "@/components/Icon";
-import { isValidInstanceName, type SQLEditorTab } from "@/types";
-import { connectionForSQLEditorTab } from "@/utils";
+import { type SQLEditorTab } from "@/types";
+import { connectionForSQLEditorTab, isConnectedSQLEditorTab } from "@/utils";
const props = defineProps<{
- tab?: SQLEditorTab;
+ tab: SQLEditorTab;
}>();
-const instance = computed(() => {
- const { tab } = props;
- if (!tab) {
- return;
- }
- const { instance } = connectionForSQLEditorTab(tab);
- if (!instance) {
- return;
- }
- if (!isValidInstanceName(instance.name)) {
- return;
- }
- return instance;
-});
+const connected = computed(() => isConnectedSQLEditorTab(props.tab));
+
+const instance = computed(() => connectionForSQLEditorTab(props.tab).instance);
</script>
diff --git a/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue b/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue
index bd1aaac7b1..f2087b73c4 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue
@@ -10,6 +10,7 @@
<Button
v-if="showCreateInstanceButton"
type="default"
+ class="px-4!"
@click="gotoInstanceCreatePage"
>
<template #icon>
@@ -17,7 +18,13 @@
</template>
{{ $t("sql-editor.add-a-new-instance") }}
</Button>
- <Button v-if="showConnectButton" type="primary" secondary @click="changeConnection">
+ <Button
+ v-if="showConnectButton"
+ secondary
+ type="primary"
+ class="px-4!"
+ @click="changeConnection"
+ >
<template #icon>
<LinkIcon :stroke-width="1.5" class="w-8 h-8" />
</template>
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index 1be9416c40..f5312a2ca0 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -28,7 +28,6 @@ import {
suggestedTabTitleForSQLEditorConnection,
useDynamicLocalStorage,
} from "@/utils";
-import type { SQLEditorContext } from "../context";
import { useFolderByView } from "./folder";
import type { SheetViewMode } from "./types";
@@ -542,7 +541,6 @@ export const provideSheetContext = () => {
export const openWorksheetByName = async (
name: string,
- editorContext: SQLEditorContext,
forceNewTab = false
) => {
const worksheet = await useWorkSheetStore().getOrFetchWorksheetByName(name);
@@ -559,7 +557,6 @@ export const openWorksheetByName = async (
return false;
}
- await editorContext.maybeSwitchProject(worksheet.project);
const tabStore = useSQLEditorTabStore();
const openingSheetTab = tabStore.openTabList.find(
(tab) => tab.worksheet === worksheet.name
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/index.ts b/frontend/src/views/sql-editor/TabList/TabItem/index.ts
deleted file mode 100644
index aabe8dc019..0000000000
--- a/frontend/src/views/sql-editor/TabList/TabItem/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-import TabItem from "./TabItem.vue";
-
-export default TabItem;
diff --git a/frontend/src/views/sql-editor/TabList/TabList.vue b/frontend/src/views/sql-editor/TabList/TabList.vue
index 2ce8b5fb3b..080618c2fc 100644
--- a/frontend/src/views/sql-editor/TabList/TabList.vue
+++ b/frontend/src/views/sql-editor/TabList/TabList.vue
@@ -46,6 +46,7 @@
>
<button
class="bg-gray-200/20 hover:bg-accent/10py-1 px-1.5 border-t border-x rounded-t hover:border-accent"
+ :disabled="state.loading"
@click="handleAddTab"
>
<PlusIcon class="h-5 w-5" stroke-width="2.5" />
@@ -80,22 +81,24 @@ import { useSQLEditorContext } from "../context";
import BrandingLogoWrapper from "./BrandingLogoWrapper.vue";
import ContextMenu from "./ContextMenu.vue";
import { provideTabListContext } from "./context";
-import TabItem from "./TabItem";
+import TabItem from "./TabItem/TabItem.vue";
type LocalState = {
dragging: boolean;
hoverTabId: string;
+ loading: boolean;
};
const tabStore = useSQLEditorTabStore();
+const editorContext = useSQLEditorContext();
const { t } = useI18n();
const dialog = useDialog();
-const { showConnectionPanel } = useSQLEditorContext();
const state = reactive<LocalState>({
dragging: false,
hoverTabId: "",
+ loading: false,
});
const scrollbarRef = ref<InstanceType<typeof NScrollbar>>();
const tabListRef = ref<InstanceType<typeof Draggable>>();
@@ -112,21 +115,28 @@ const scrollElement = computed((): HTMLElement | null | undefined => {
return scrollbarRef.value?.scrollbarInstRef?.containerRef;
});
-const handleAddTab = () => {
- tabStore.addTab();
+const handleAddTab = async () => {
+ if (state.loading) {
+ return;
+ }
- nextTick(() => {
- showConnectionPanel.value = true;
- requestAnimationFrame(() => {
- const elem = scrollElement.value;
- if (elem) {
- elem.scrollTo(elem.scrollWidth, 0);
- }
+ state.loading = true;
+ try {
+ await editorContext.createWorksheet({});
+ nextTick(() => {
requestAnimationFrame(() => {
- recalculateScrollState();
+ const elem = scrollElement.value;
+ if (elem) {
+ elem.scrollTo(elem.scrollWidth, 0);
+ }
+ requestAnimationFrame(() => {
+ recalculateScrollState();
+ });
});
});
- });
+ } finally {
+ state.loading = false;
+ }
};
const handleRemoveTab = async (tab: SQLEditorTab, focusWhenConfirm = false) => {
diff --git a/frontend/src/views/sql-editor/context.ts b/frontend/src/views/sql-editor/context.ts
index 4e47f1e297..5358db5b05 100644
--- a/frontend/src/views/sql-editor/context.ts
+++ b/frontend/src/views/sql-editor/context.ts
@@ -1,12 +1,22 @@
+import { create } from "@bufbuild/protobuf";
import { useLocalStorage } from "@vueuse/core";
import Emittery from "emittery";
import { type IRange } from "monaco-editor";
import type { ComputedRef, InjectionKey, Ref } from "vue";
-import { computed, inject, provide, ref } from "vue";
-import { useProjectV1Store, useSQLEditorStore } from "@/store";
+import { computed, inject, nextTick, provide, ref } from "vue";
+import {
+ useProjectV1Store,
+ useSQLEditorStore,
+ useWorkSheetStore,
+} from "@/store";
import type { SQLEditorTab } from "@/types";
import { isValidProjectName } from "@/types";
import type { GetSchemaStringRequest_ObjectType } from "@/types/proto-es/v1/database_service_pb";
+import {
+ Worksheet_Visibility,
+ WorksheetSchema,
+} from "@/types/proto-es/v1/worksheet_service_pb";
+import { openWorksheetByName } from "@/views/sql-editor/Sheet";
export type AsidePanelTab = "SCHEMA" | "WORKSHEET" | "HISTORY";
@@ -59,6 +69,13 @@ export type SQLEditorContext = {
maybeSwitchProject: (project: string) => Promise<string | undefined>;
handleEditorPanelResize: (size: number) => void;
+ createWorksheet: ({
+ title,
+ folders,
+ }: {
+ title?: string;
+ folders?: string[];
+ }) => Promise<void>;
};
export const KEY = Symbol(
@@ -72,6 +89,8 @@ export const useSQLEditorContext = () => {
export const provideSQLEditorContext = () => {
const editorStore = useSQLEditorStore();
const projectStore = useProjectV1Store();
+ const worksheetStore = useWorkSheetStore();
+ const showConnectionPanel = ref(false);
const aiPanelSize = useLocalStorage(
"bb.plugin.editor.ai-panel-size",
@@ -93,9 +112,43 @@ export const provideSQLEditorContext = () => {
};
});
+ const createWorksheet = async ({
+ title = "new worksheet",
+ statement = "",
+ folders = [],
+ }: {
+ title?: string;
+ statement?: string;
+ folders?: string[];
+ }) => {
+ const newWorksheet = await worksheetStore.createWorksheet(
+ create(WorksheetSchema, {
+ title: title,
+ content: new TextEncoder().encode(statement),
+ project: editorStore.project,
+ visibility: Worksheet_Visibility.PRIVATE,
+ })
+ );
+
+ if (folders.length > 0) {
+ await worksheetStore.upsertWorksheetOrganizer(
+ {
+ worksheet: newWorksheet.name,
+ folders: folders,
+ },
+ ["folders"]
+ );
+ }
+
+ nextTick(() => {
+ openWorksheetByName(newWorksheet.name, true);
+ showConnectionPanel.value = true;
+ });
+ };
+
const context: SQLEditorContext = {
asidePanelTab: ref("WORKSHEET"),
- showConnectionPanel: ref(false),
+ showConnectionPanel,
showAIPanel,
editorPanelSize,
schemaViewer: ref(undefined),
@@ -124,6 +177,7 @@ export const provideSQLEditorContext = () => {
}
aiPanelSize.value = 1 - size;
},
+ createWorksheet,
};
provide(KEY, context);