Skip to content

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

chore: optimize rename sheet (#10967)

ecmadao8c48fce28d1c
diff --git a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
index 801fa4993b..fe0dc45e30 100644
--- a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
@@ -272,7 +272,7 @@ const handleClearScreen = () => {
const handleClickSave = () => {
events.emit("save-sheet", {
- title: tabStore.currentTab.name,
+ tab: tabStore.currentTab,
});
};
diff --git a/frontend/src/views/sql-editor/EditorCommon/SaveSheetForm.vue b/frontend/src/views/sql-editor/EditorCommon/SaveSheetForm.vue
index 10fba6385f..ff6ed61092 100644
--- a/frontend/src/views/sql-editor/EditorCommon/SaveSheetForm.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/SaveSheetForm.vue
@@ -17,20 +17,25 @@
<script lang="ts" setup>
import { ref, nextTick } from "vue";
-import { useTabStore } from "@/store";
+import { TabInfo } from "@/types";
+
+const props = defineProps<{
+ tab: TabInfo;
+}>();
const emit = defineEmits<{
(e: "close"): void;
- (e: "confirm", title: string): void;
+ (e: "confirm", tab: TabInfo): void;
}>();
-const tabStore = useTabStore();
-
-const sheetTitle = ref(tabStore.currentTab.name);
+const sheetTitle = ref(props.tab.name);
const sheetNameInputRef = ref();
const handleSaveSheet = () => {
- emit("confirm", sheetTitle.value);
+ emit("confirm", {
+ ...props.tab,
+ name: sheetTitle.value,
+ });
};
nextTick(() => {
diff --git a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
index 0035341ebd..624229f01e 100644
--- a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
@@ -1,18 +1,22 @@
<template>
<BBModal
- v-if="state.showModal"
+ v-if="state.pendingEditTab"
:title="$t('sql-editor.save-sheet')"
@close="close"
>
- <SaveSheetForm @close="close" @confirm="doSaveSheet" />
+ <SaveSheetForm
+ :tab="state.pendingEditTab"
+ @close="close"
+ @confirm="doSaveSheet"
+ />
</BBModal>
</template>
<script lang="ts" setup>
-import { computed, reactive } from "vue";
+import { reactive } from "vue";
import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import { useDatabaseV1Store, useWorkSheetStore, useTabStore } from "@/store";
-import { UNKNOWN_ID } from "@/types";
+import { UNKNOWN_ID, TabInfo } from "@/types";
import {
Worksheet,
Worksheet_Visibility,
@@ -23,7 +27,7 @@ import { useSQLEditorContext } from "../context";
import SaveSheetForm from "./SaveSheetForm.vue";
type LocalState = {
- showModal: boolean;
+ pendingEditTab?: TabInfo;
};
const tabStore = useTabStore();
@@ -32,75 +36,67 @@ const worksheetV1Store = useWorkSheetStore();
const { events: sheetEvents } = useSheetContext();
const { events: editorEvents } = useSQLEditorContext();
-const state = reactive<LocalState>({
- showModal: false,
-});
+const state = reactive<LocalState>({});
-const allowSave = computed((): boolean => {
- const tab = tabStore.currentTab;
- if (tab.statement === "") {
- return false;
- }
- if (tab.isSaved) {
- return false;
- }
- // Temporarily disable saving and sharing if we are connected to an instance
- // but not a database.
- if (tab.connection.databaseId === String(UNKNOWN_ID)) {
- return false;
- }
- return true;
-});
+const doSaveSheet = async (tab: TabInfo) => {
+ const { name, statement, sheetName } = tab;
-const doSaveSheet = async (title: string) => {
- const { name, statement, sheetName } = tabStore.currentTab;
- title = title || name;
+ if (name === "" || statement === "") {
+ return;
+ }
const sheetId = Number(extractWorksheetUID(sheetName ?? ""));
- const conn = tabStore.currentTab.connection;
- const database = await databaseStore.getOrFetchDatabaseByUID(
- conn.databaseId,
- true /* silent */
- );
-
- let sheet: Worksheet | undefined;
if (sheetId !== UNKNOWN_ID) {
- sheet = await worksheetV1Store.patchSheet(
+ const sheet = await worksheetV1Store.patchSheet(
{
name: sheetName,
- title,
+ title: name,
content: new TextEncoder().encode(statement),
},
["title", "content"]
);
+ if (sheet) {
+ const tab = tabStore.tabList.find((t) => t.sheetName === sheet.name);
+ if (tab) {
+ tabStore.updateTab(tab.id, {
+ isSaved: true,
+ name,
+ });
+ }
+ }
} else {
- sheet = await worksheetV1Store.createSheet(
+ if (tab.connection.databaseId === String(UNKNOWN_ID)) {
+ return false;
+ }
+ const database = await databaseStore.getOrFetchDatabaseByUID(
+ tab.connection.databaseId,
+ true /* silent */
+ );
+ const sheet = await worksheetV1Store.createSheet(
Worksheet.fromPartial({
- title: title,
+ title: name,
project: database.project,
content: new TextEncoder().encode(statement),
database: database.name,
visibility: Worksheet_Visibility.VISIBILITY_PRIVATE,
})
);
+ if (tabStore.currentTabId === tab.id) {
+ tabStore.updateCurrentTab({
+ sheetName: sheet.name,
+ isSaved: true,
+ name,
+ });
+ }
}
- if (sheet) {
- tabStore.updateCurrentTab({
- sheetName: sheet.name,
- isSaved: true,
- name: title,
- });
-
- // Refresh "my" sheet list.
- sheetEvents.emit("refresh", { views: ["my"] });
- }
- state.showModal = false;
+ // Refresh "my" sheet list.
+ sheetEvents.emit("refresh", { views: ["my"] });
+ state.pendingEditTab = undefined;
};
-const needSheetTitle = (title: string) => {
- const tab = tabStore.currentTab;
+const needSheetTitle = (tab: TabInfo) => {
if (tab.sheetName) {
// If the sheet is saved, we don't need to show the name popup.
return false;
@@ -108,25 +104,21 @@ const needSheetTitle = (title: string) => {
return true;
};
-const trySaveSheet = (title: string) => {
- if (!allowSave.value) {
- return;
- }
-
- if (needSheetTitle(title)) {
- state.showModal = true;
+const trySaveSheet = (tab: TabInfo, editTitle?: boolean) => {
+ if (needSheetTitle(tab) || editTitle) {
+ state.pendingEditTab = tab;
return;
}
- state.showModal = false;
+ state.pendingEditTab = undefined;
- doSaveSheet(title);
+ doSaveSheet(tab);
};
const close = () => {
- state.showModal = false;
+ state.pendingEditTab = undefined;
};
-useEmitteryEventListener(editorEvents, "save-sheet", ({ title }) => {
- trySaveSheet(title);
+useEmitteryEventListener(editorEvents, "save-sheet", ({ tab, editTitle }) => {
+ trySaveSheet(tab, editTitle);
});
</script>
diff --git a/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue b/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue
index c0dd078531..600b3af84b 100644
--- a/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue
@@ -139,7 +139,7 @@ const handleChangeSelection = (value: string) => {
};
const handleSaveSheet = () => {
- editorEvents.emit("save-sheet", { title: tabStore.currentTab.name });
+ editorEvents.emit("save-sheet", { tab: tabStore.currentTab });
};
const runQueryAction = (explain = false) => {
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue b/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
deleted file mode 100644
index b4399acfb0..0000000000
--- a/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
+++ /dev/null
@@ -1,134 +0,0 @@
-<template>
- <NDropdown
- v-if="options.length > 0"
- trigger="click"
- :options="options"
- @select="handleAction"
- >
- <heroicons-outline:dots-horizontal
- class="w-6 h-auto border border-gray-300 bg-white p-1 rounded outline-none shadow"
- />
- </NDropdown>
-</template>
-
-<script lang="ts" setup>
-import { type DropdownOption, NDropdown, useDialog } from "naive-ui";
-import { computed } from "vue";
-import { useI18n } from "vue-i18n";
-import { useWorkSheetStore, pushNotification } from "@/store";
-import {
- Worksheet,
- Worksheet_Visibility,
-} from "@/types/proto/v1/worksheet_service";
-import { isWorksheetWritableV1 } from "@/utils";
-import type { SheetViewMode } from "../types";
-
-const props = defineProps<{
- view: SheetViewMode;
- sheet: Worksheet;
-}>();
-
-const emit = defineEmits<{
- (event: "refresh"): void;
-}>();
-
-const { t } = useI18n();
-const worksheetV1Store = useWorkSheetStore();
-const dialog = useDialog();
-
-const options = computed(() => {
- const options: DropdownOption[] = [];
- const { sheet, view } = props;
-
- if (sheet.starred) {
- options.push({
- key: "unstar",
- label: t("sheet.unstar"),
- });
- } else {
- options.push({
- key: "star",
- label: t("sheet.star"),
- });
- }
-
- const canWriteSheet = isWorksheetWritableV1(sheet);
- if (canWriteSheet) {
- options.push({
- key: "delete",
- label: t("common.delete"),
- });
- }
- if (view === "shared") {
- options.push({
- key: "duplicate",
- label: t("common.duplicate"),
- });
- }
-
- return options;
-});
-
-const handleAction = async (key: string) => {
- const { sheet } = props;
- if (key === "delete") {
- const dialogInstance = dialog.create({
- title: t("sheet.hint-tips.confirm-to-delete-this-sheet"),
- type: "info",
- autoFocus: false,
- closable: false,
- maskClosable: false,
- closeOnEsc: false,
- async onPositiveClick() {
- await worksheetV1Store.deleteSheetByName(sheet.name);
- emit("refresh");
- dialogInstance.destroy();
- },
- onNegativeClick() {
- dialogInstance.destroy();
- },
- negativeText: t("common.cancel"),
- positiveText: t("common.confirm"),
- showIcon: true,
- });
- } else if (key === "star" || key === "unstar") {
- await worksheetV1Store.upsertSheetOrganizer({
- worksheet: sheet.name,
- starred: key === "star",
- });
- emit("refresh");
- } else if (key === "duplicate") {
- const dialogInstance = dialog.create({
- title: t("sheet.hint-tips.confirm-to-duplicate-sheet"),
- type: "info",
- autoFocus: false,
- closable: false,
- maskClosable: false,
- closeOnEsc: false,
- async onPositiveClick() {
- await worksheetV1Store.createSheet(
- Worksheet.fromPartial({
- title: sheet.title,
- project: sheet.project,
- content: sheet.content,
- database: sheet.database,
- visibility: Worksheet_Visibility.VISIBILITY_PRIVATE,
- })
- );
- pushNotification({
- module: "bytebase",
- style: "INFO",
- title: t("sheet.notifications.duplicate-success"),
- });
- dialogInstance.destroy();
- },
- onNegativeClick() {
- dialogInstance.destroy();
- },
- negativeText: t("common.cancel"),
- positiveText: t("common.confirm"),
- showIcon: true,
- });
- }
-};
-</script>
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/SheetTable.vue b/frontend/src/views/sql-editor/Sheet/SheetTable/SheetTable.vue
deleted file mode 100644
index 79246bb6d4..0000000000
--- a/frontend/src/views/sql-editor/Sheet/SheetTable/SheetTable.vue
+++ /dev/null
@@ -1,181 +0,0 @@
-<template>
- <div class="w-full grid grid-cols-1">
- <div class="sheet-list-container text-sm text-gray-400" :class="view">
- <span v-for="header in headers" :key="header.key" class="uppercase">{{
- header.label
- }}</span>
- </div>
- <div
- v-if="loading"
- class="w-full flex flex-col py-6 justify-start items-center"
- >
- <span class="text-sm leading-6 text-gray-500">{{
- $t("sql-editor.loading-data")
- }}</span>
- </div>
- <div
- v-for="sheet in sheetList"
- :key="sheet.name"
- class="sheet-list-container text-sm cursor-pointer hover:bg-gray-100"
- :class="view"
- @click="handleSheetClick(sheet)"
- >
- <span
- v-for="value in getValueList(sheet)"
- :key="value.key"
- class="truncate w-5/6"
- >{{ value.value }}</span
- >
- <div class="flex flex-row justify-end items-center" @click.stop>
- <Dropdown :sheet="sheet" :view="view" @refresh="$emit('refresh')" />
- </div>
- </div>
-
- <div
- v-show="!loading && sheetList.length === 0"
- class="w-full flex flex-col py-6 justify-start items-center"
- >
- <heroicons-outline:inbox class="w-12 h-auto text-gray-500" />
- <span class="text-sm leading-6 text-gray-500">{{
- $t("common.no-data")
- }}</span>
- </div>
- </div>
-</template>
-
-<script lang="ts" setup>
-import dayjs from "dayjs";
-import { computed } from "vue";
-import { useI18n } from "vue-i18n";
-import { useRouter } from "vue-router";
-import { SQL_EDITOR_SHARE_MODULE } from "@/router/sqlEditor";
-import { useUserStore, useProjectV1Store } from "@/store";
-import {
- Worksheet,
- Worksheet_Visibility,
-} from "@/types/proto/v1/worksheet_service";
-import { projectV1Name, worksheetSlugV1 } from "@/utils";
-import { SheetViewMode } from "../types";
-import Dropdown from "./Dropdown.vue";
-
-const props = withDefaults(
- defineProps<{
- view: SheetViewMode;
- sheetList: Worksheet[];
- loading?: boolean;
- }>(),
- {
- view: "my",
- loading: false,
- }
-);
-
-defineEmits<{
- (event: "refresh"): void;
-}>();
-
-const { t } = useI18n();
-const router = useRouter();
-const projectV1Store = useProjectV1Store();
-
-const showCreator = computed(() => {
- return props.view === "shared" || props.view === "starred";
-});
-
-const handleSheetClick = (sheet: Worksheet) => {
- router.push({
- name: SQL_EDITOR_SHARE_MODULE,
- params: {
- sheetSlug: worksheetSlugV1(sheet),
- },
- });
-};
-
-const headers = computed(() => {
- const labelList = [
- {
- key: "name",
- label: t("common.name"),
- },
- {
- key: "project",
- label: t("common.project"),
- },
- {
- key: "visibility",
- label: t("common.visibility"),
- },
- ];
-
- if (showCreator.value) {
- labelList.push({
- key: "creator",
- label: t("common.creator"),
- });
- }
-
- labelList.push({
- key: "updated",
- label: t("common.updated-at"),
- });
-
- return labelList;
-});
-
-const getValueList = (sheet: Worksheet) => {
- const project = projectV1Store.getProjectByName(sheet.project);
- let visibility = t("sql-editor.private");
- switch (sheet.visibility) {
- case Worksheet_Visibility.VISIBILITY_PROJECT:
- visibility = t("common.project");
- break;
- case Worksheet_Visibility.VISIBILITY_PUBLIC:
- visibility = t("sql-editor.public");
- }
- const valueList = [
- {
- key: "name",
- value: sheet.title,
- },
- {
- key: "project",
- value: projectV1Name(project),
- },
- {
- key: "visibility",
- value: visibility,
- },
- ];
-
- if (showCreator.value) {
- valueList.push({
- key: "creator",
- value:
- useUserStore().getUserByIdentifier(sheet.creator)?.title ??
- sheet.creator,
- });
- }
-
- valueList.push({
- key: "updated",
- value: dayjs
- .duration((sheet.updateTime ?? new Date()).getTime() - Date.now())
- .humanize(true),
- });
-
- return valueList;
-};
-</script>
-
-<style scoped lang="postcss">
-.sheet-list-container {
- @apply w-full grid py-3 px-4 border-b text-sm leading-6 select-none;
-}
-.sheet-list-container.my {
- grid-template-columns: 2fr repeat(4, 1fr) 32px;
-}
-.sheet-list-container.shared,
-.sheet-list-container.starred {
- grid-template-columns: 2fr repeat(5, 1fr) 32px;
-}
-</style>
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/index.ts b/frontend/src/views/sql-editor/Sheet/SheetTable/index.ts
deleted file mode 100644
index 7c7bac0e73..0000000000
--- a/frontend/src/views/sql-editor/Sheet/SheetTable/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-import SheetTable from "./SheetTable.vue";
-
-export default SheetTable;
diff --git a/frontend/src/views/sql-editor/Sheet/common/Dropdown.vue b/frontend/src/views/sql-editor/Sheet/common/Dropdown.vue
index b19ecec107..c6584231ef 100644
--- a/frontend/src/views/sql-editor/Sheet/common/Dropdown.vue
+++ b/frontend/src/views/sql-editor/Sheet/common/Dropdown.vue
@@ -28,8 +28,13 @@ import {
Worksheet,
Worksheet_Visibility,
} from "@/types/proto/v1/worksheet_service";
-import { isWorksheetWritableV1 } from "@/utils";
+import {
+ isWorksheetWritableV1,
+ getDefaultTab,
+ getSheetStatement,
+} from "@/utils";
import { useSheetContext, type SheetViewMode } from "../";
+import { useSQLEditorContext } from "../../context";
const props = defineProps<{
view: SheetViewMode;
@@ -43,6 +48,7 @@ const { t } = useI18n();
const worksheetV1Store = useWorkSheetStore();
const dialog = useDialog();
const { events } = useSheetContext();
+const { events: editorEvents } = useSQLEditorContext();
const options = computed(() => {
const options: DropdownOption[] = [];
@@ -62,10 +68,16 @@ const options = computed(() => {
const canWriteSheet = isWorksheetWritableV1(sheet);
if (canWriteSheet) {
- options.push({
- key: "delete",
- label: t("common.delete"),
- });
+ options.push(
+ {
+ key: "delete",
+ label: t("common.delete"),
+ },
+ {
+ key: "rename",
+ label: t("sql-editor.tab.context-menu.actions.rename"),
+ }
+ );
}
if (view === "shared") {
options.push({
@@ -143,6 +155,16 @@ const handleAction = async (key: string) => {
positiveText: t("common.confirm"),
showIcon: true,
});
+ } else if (key === "rename") {
+ editorEvents.emit("save-sheet", {
+ tab: {
+ ...getDefaultTab(),
+ sheetName: sheet.name,
+ name: sheet.title,
+ statement: getSheetStatement(sheet),
+ },
+ editTitle: true,
+ });
}
};
diff --git a/frontend/src/views/sql-editor/Sheet/index.ts b/frontend/src/views/sql-editor/Sheet/index.ts
index 4932cb6a2b..f3067e997e 100644
--- a/frontend/src/views/sql-editor/Sheet/index.ts
+++ b/frontend/src/views/sql-editor/Sheet/index.ts
@@ -1,6 +1,3 @@
-import SheetTable from "./SheetTable";
-
export * from "./types";
export * from "./context";
export * from "./common";
-export { SheetTable };
diff --git a/frontend/src/views/sql-editor/context.ts b/frontend/src/views/sql-editor/context.ts
index 19ddd41d04..6bd4d62017 100644
--- a/frontend/src/views/sql-editor/context.ts
+++ b/frontend/src/views/sql-editor/context.ts
@@ -1,6 +1,6 @@
import Emittery from "emittery";
import { InjectionKey, inject, provide, Ref, ref } from "vue";
-import { ComposedDatabase } from "@/types";
+import { ComposedDatabase, TabInfo } from "@/types";
import {
DatabaseMetadata,
ExternalTableMetadata,
@@ -9,7 +9,7 @@ import {
} from "@/types/proto/v1/database_service";
type SQLEditorEvents = Emittery<{
- "save-sheet": { title: string };
+ "save-sheet": { tab: TabInfo; editTitle?: boolean };
"alter-schema": {
databaseUID: string;
schema: string;