Skip to content

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

feat: upload sheet with progress (#5351)

Liu Jif1aa6ef41527
diff --git a/frontend/src/components/Issue/IssueTaskStatementPanel.vue b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
index 05ea5092ee..ac36fdba19 100644
--- a/frontend/src/components/Issue/IssueTaskStatementPanel.vue
+++ b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
@@ -55,32 +55,10 @@
/>
<span class="textlabel">{{ $t("issue.format-on-save") }}</span>
</label>
- <BBContextMenuButton
- v-if="state.editing"
- :disabled="state.isUploadingFile"
- :action-list="uploadSQLActionButtonList"
- >
- <template #default="{ action }">
- <button type="button">
- <label
- for="sql-file-input"
- class="w-full flex flex-row justify-center items-center cursor-pointer"
- >
- <heroicons-outline:arrow-up-tray class="w-4 h-auto mr-1" />
- <span>{{ action.text }}</span>
- <input
- id="sql-file-input"
- type="file"
- accept=".sql,.txt,application/sql,text/plain"
- class="hidden"
- @change="
- (event) => handleUploadFileButtonClick(event, action.key)
- "
- />
- </label>
- </button>
- </template>
- </BBContextMenuButton>
+
+ <UploadProgressButton :upload="handleUploadFile">
+ {{ $t("issue.upload-sql") }}
+ </UploadProgressButton>
</template>
<button
@@ -149,6 +127,8 @@
import { useDialog } from "naive-ui";
import { onMounted, reactive, watch, computed, ref, nextTick } from "vue";
import { useI18n } from "vue-i18n";
+import { isNumber, isUndefined } from "lodash-es";
+
import {
hasFeature,
pushNotification,
@@ -168,9 +148,9 @@ import {
import { UNKNOWN_ID } from "@/types";
import { TableMetadata } from "@/types/proto/store/database";
import MonacoEditor from "../MonacoEditor/MonacoEditor.vue";
-import { isUndefined } from "lodash-es";
import { useInstanceEditorLanguage } from "@/utils";
import { useSQLAdviceMarkers } from "./logic/useSQLAdviceMarkers";
+import UploadProgressButton from "../misc/UploadProgressButton.vue";
interface LocalState {
editing: boolean;
@@ -347,24 +327,6 @@ const allowUploadSheetForTask = computed(() => {
return TaskTypeWithSheetId.includes(task.type);
});
-const uploadSQLActionButtonList = computed(() => {
- const list = [
- {
- key: "NORMAL",
- text: t("issue.upload-sql"),
- },
- ];
-
- if (allowUploadSheetForTask.value) {
- list.push({
- key: "SHEET",
- text: t("issue.upload-sql-as-sheet"),
- });
- }
-
- return list;
-});
-
const isTaskHasSheetId = computed(() => {
const task = selectedTask.value;
if (create.value) {
@@ -471,15 +433,65 @@ const allowSaveSQL = computed((): boolean => {
return true;
});
-const handleUploadFileButtonClick = (
- event: Event,
- action: "NORMAL" | "SHEET"
-) => {
- if (action === "SHEET") {
- handleUploadLocalFileAsSheet(event);
- } else {
- handleUploadLocalFile(event);
+const handleUploadFile = async (event: Event, tick: (p: number) => void) => {
+ if (!allowUploadSheetForTask.value || !selectedDatabase.value) {
+ return;
}
+
+ state.isUploadingFile = true;
+ const projectId = selectedDatabase.value.projectId;
+ const { filename, content: statement } = await handleUploadFileEvent(
+ event,
+ 100
+ );
+
+ const uploadStatementAsSheet = async (statement: string) => {
+ const sheet = await sheetStore.createSheet(
+ {
+ projectId: projectId,
+ name: filename,
+ statement,
+ visibility: "PRIVATE",
+ payload: {},
+ },
+ {
+ timeout: 0, // 10 * 60 * 1000, // 10 minutes
+ onUploadProgress: (event) => {
+ console.debug("upload progress", event);
+ const progress = event.progress;
+ if (isNumber(progress)) {
+ tick(progress * 100);
+ } else {
+ tick(-1); // -1 to show a simple spinner instead of progress
+ }
+ },
+ }
+ );
+
+ updateSheetId(sheet.id);
+ state.isUploadingFile = false;
+ if (selectedTask.value) updateEditorHeight();
+ };
+
+ return new Promise((resolve) => {
+ if (state.editStatement) {
+ // Show a confirm dialog before replacing if the editing statement
+ // is not empty
+ overrideSQLDialog.create({
+ positiveText: t("common.confirm"),
+ negativeText: t("common.cancel"),
+ title: t("issue.override-current-statement"),
+ onNegativeClick: () => {
+ // nothing to do
+ },
+ onPositiveClick: () => {
+ resolve(uploadStatementAsSheet(statement));
+ },
+ });
+ } else {
+ resolve(uploadStatementAsSheet(statement));
+ }
+ });
};
const handleUploadFileEvent = (
@@ -542,73 +554,6 @@ const handleUploadFileEvent = (
});
};
-const handleUploadLocalFile = async (event: Event) => {
- const { content: statement } = await handleUploadFileEvent(event, 1);
- if (state.editStatement) {
- // Show a confirm dialog before replacing if the editing statement
- // is not empty
- overrideSQLDialog.create({
- positiveText: t("common.confirm"),
- negativeText: t("common.cancel"),
- title: t("issue.override-current-statement"),
- onNegativeClick: () => {
- // nothing to do
- },
- onPositiveClick: () => {
- onStatementChange(statement);
- },
- });
- } else {
- updateSheetId(undefined);
- updateStatement(statement);
- }
-};
-
-const handleUploadLocalFileAsSheet = async (event: Event) => {
- if (!allowUploadSheetForTask.value || !selectedDatabase.value) {
- return;
- }
-
- state.isUploadingFile = true;
- const projectId = selectedDatabase.value.projectId;
- const { filename, content: statement } = await handleUploadFileEvent(
- event,
- 100
- );
-
- const uploadStatementAsSheet = async (statement: string) => {
- const sheet = await sheetStore.createSheet({
- projectId: projectId,
- name: filename,
- statement: statement,
- visibility: "PRIVATE",
- payload: {},
- });
-
- updateSheetId(sheet.id);
- state.isUploadingFile = false;
- if (selectedTask.value) updateEditorHeight();
- };
-
- if (state.editStatement) {
- // Show a confirm dialog before replacing if the editing statement
- // is not empty
- overrideSQLDialog.create({
- positiveText: t("common.confirm"),
- negativeText: t("common.cancel"),
- title: t("issue.override-current-statement"),
- onNegativeClick: () => {
- // nothing to do
- },
- onPositiveClick: () => {
- uploadStatementAsSheet(statement);
- },
- });
- } else {
- uploadStatementAsSheet(statement);
- }
-};
-
const onStatementChange = (value: string) => {
if (readonly.value) {
return;
diff --git a/frontend/src/components/misc/UploadProgressButton.vue b/frontend/src/components/misc/UploadProgressButton.vue
new file mode 100644
index 0000000000..dcdaf3e6a1
--- /dev/null
+++ b/frontend/src/components/misc/UploadProgressButton.vue
@@ -0,0 +1,77 @@
+<template>
+ <button
+ class="select-none inline-flex items-center space-x-1 border border-control-border rounded-md text-control bg-white hover:bg-control-bg-hover disabled:bg-white disabled:opacity-50 disabled:cursor-not-allowed px-4 py-2 text-sm leading-5 font-medium focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
+ :disabled="disabled"
+ @click="handleClick"
+ >
+ <slot v-if="!uploading" name="icon">
+ <heroicons-outline:arrow-up-tray class="w-4 h-auto mr-1" />
+ </slot>
+ <template v-else>
+ <BBProgressPie
+ v-if="percent > 0"
+ :percent="percent"
+ class="w-5 h-5 text-info"
+ >
+ <template #default="{ percent: displayPercent }">
+ <span class="scale-[66%]">{{ displayPercent }}</span>
+ </template>
+ </BBProgressPie>
+ <BBSpin v-else class="w-5 h-5" />
+ </template>
+
+ <span>
+ <slot v-if="!uploading" />
+ <slot v-else name="uploading">
+ {{ $t("common.uploading") }}
+ </slot>
+ </span>
+
+ <input
+ ref="inputRef"
+ type="file"
+ accept=".sql,.txt,application/sql,text/plain"
+ class="hidden"
+ @change="handleUpload"
+ />
+ </button>
+</template>
+
+<script lang="ts" setup>
+import { computed, ref } from "vue";
+import { BBProgressPie } from "@/bbkit";
+
+type Tick = (p: number) => void;
+
+const props = defineProps<{
+ upload: (e: Event, tick: Tick) => Promise<any>;
+ disabled?: boolean;
+}>();
+
+const inputRef = ref<HTMLInputElement>();
+const uploading = ref(false);
+const percent = ref(-1); // -1 to show a simple spinner instead of progress
+
+const disabled = computed(() => {
+ return props.disabled || uploading.value;
+});
+
+const updatePercent = (p: number) => {
+ percent.value = p;
+};
+
+const handleClick = () => {
+ inputRef.value?.click();
+};
+
+const handleUpload = async (e: Event) => {
+ uploading.value = true;
+ percent.value = -1;
+ try {
+ await props.upload(e, updatePercent);
+ } finally {
+ uploading.value = false;
+ percent.value = -1;
+ }
+};
+</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index e576c62bca..1af2e5f1f7 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -232,7 +232,8 @@
"will-lose-unsaved-data": "Will lose any unsaved data.",
"deleted": "Deleted",
"will-override-current-data": "Will override current data.",
- "rollout": "Roll out"
+ "rollout": "Roll out",
+ "uploading": "Uploading"
},
"error-page": {
"go-back-home": "Go back home"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 7c4c52dd1f..ec78261f4d 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -230,7 +230,8 @@
"will-lose-unsaved-data": "这会丢失当前所有未保存的数据。",
"deleted": "已删除",
"will-override-current-data": "将会覆盖当前数据。",
- "rollout": "发布"
+ "rollout": "发布",
+ "uploading": "上传中"
},
"error-page": {
"go-back-home": "回到主页"
diff --git a/frontend/src/store/modules/sheet.ts b/frontend/src/store/modules/sheet.ts
index ef0e998d03..3ba32c8224 100644
--- a/frontend/src/store/modules/sheet.ts
+++ b/frontend/src/store/modules/sheet.ts
@@ -1,5 +1,5 @@
import { defineStore } from "pinia";
-import axios from "axios";
+import axios, { type AxiosRequestConfig } from "axios";
import { isEmpty } from "lodash-es";
import {
Sheet,
@@ -170,7 +170,10 @@ export const useSheetStore = defineStore("sheet", {
visibility: "PRIVATE",
});
},
- async createSheet(sheetCreate: SheetCreate): Promise<Sheet> {
+ async createSheet(
+ sheetCreate: SheetCreate,
+ config?: AxiosRequestConfig
+ ): Promise<Sheet> {
if (sheetCreate.databaseId === UNKNOWN_ID) {
sheetCreate.databaseId = undefined;
}
@@ -181,12 +184,16 @@ export const useSheetStore = defineStore("sheet", {
}
const resData = (
- await axios.post(`/api/sheet`, {
- data: {
- type: "createSheet",
- attributes,
+ await axios.post(
+ `/api/sheet`,
+ {
+ data: {
+ type: "createSheet",
+ attributes,
+ },
},
- })
+ config
+ )
).data;
const sheet = convertSheet(resData.data, resData.included);