Skip to content

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

feat: prevent navigation when editing SQL statements in plan editor (#17115)

boojack4f00ba87145b
diff --git a/frontend/src/components/Plan/components/HeaderSection/Actions/Actions.vue b/frontend/src/components/Plan/components/HeaderSection/Actions/Actions.vue
index f8485cfd37..e4e430f4a7 100644
--- a/frontend/src/components/Plan/components/HeaderSection/Actions/Actions.vue
+++ b/frontend/src/components/Plan/components/HeaderSection/Actions/Actions.vue
@@ -5,6 +5,8 @@
<UnifiedActionGroup
:primary-action="primaryAction"
:secondary-actions="secondaryActions"
+ :disabled="actionsDisabled"
+ :disabled-tooltip="disabledTooltip"
@perform-action="handlePerformAction"
/>
@@ -35,6 +37,7 @@ import { useI18n } from "vue-i18n";
import { usePlanContext } from "@/components/Plan/logic";
import { useIssueReviewContext } from "@/components/Plan/logic/issue-review";
import { useResourcePoller } from "@/components/Plan/logic/poller";
+import { useEditorState } from "@/components/Plan/logic/useEditorState";
import {
useCurrentUserV1,
candidatesOfApprovalStepV1,
@@ -71,10 +74,24 @@ const { isCreating, plan, issue } = usePlanContext();
const currentUser = useCurrentUserV1();
const { project } = useCurrentProjectV1();
const planStore = usePlanStore();
+const editorState = useEditorState();
// Provide issue review context when an issue exists
const reviewContext = useIssueReviewContext();
+// Computed property for actions disabled state.
+const actionsDisabled = computed(() => {
+ return editorState.isEditing.value;
+});
+
+// Tooltip message for disabled state.
+const disabledTooltip = computed(() => {
+ if (editorState.isEditing.value) {
+ return t("plan.editor.save-changes-before-continuing");
+ }
+ return "";
+});
+
// Panel visibility state
const pendingReviewAction = ref<IssueReviewAction | undefined>(undefined);
const pendingStatusAction = ref<IssueStatusAction | undefined>(undefined);
diff --git a/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionButton.vue b/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionButton.vue
index d829120e4e..0474caa3fb 100644
--- a/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionButton.vue
+++ b/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionButton.vue
@@ -1,23 +1,31 @@
<template>
- <NButton
- size="medium"
- tag="div"
- v-bind="actionButtonProps(action)"
- :disabled="disabled"
- @click="$emit('perform-action', action)"
- >
- {{ actionDisplayName(action) }}
- </NButton>
+ <NTooltip :disabled="!disabledTooltip" placement="top">
+ <template #trigger>
+ <NButton
+ size="medium"
+ tag="div"
+ v-bind="actionButtonProps(action)"
+ :disabled="disabled"
+ @click="$emit('perform-action', action)"
+ >
+ {{ actionDisplayName(action) }}
+ </NButton>
+ </template>
+ <template #default>
+ {{ disabledTooltip }}
+ </template>
+ </NTooltip>
</template>
<script setup lang="ts">
-import { NButton } from "naive-ui";
+import { NButton, NTooltip } from "naive-ui";
import { useI18n } from "vue-i18n";
import type { UnifiedAction } from "./types";
defineProps<{
action: UnifiedAction;
disabled?: boolean;
+ disabledTooltip?: string;
}>();
defineEmits<{
diff --git a/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionGroup.vue b/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionGroup.vue
index 9857e8faf1..3af6b79ed9 100644
--- a/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionGroup.vue
+++ b/frontend/src/components/Plan/components/HeaderSection/Actions/unified/UnifiedActionGroup.vue
@@ -4,7 +4,8 @@
<UnifiedActionButton
v-if="primaryAction"
:action="primaryAction.action"
- :disabled="primaryAction.disabled"
+ :disabled="disabled || primaryAction.disabled"
+ :disabled-tooltip="disabled ? disabledTooltip : undefined"
@perform-action="handleAction"
/>
@@ -15,9 +16,16 @@
placement="bottom-end"
:options="dropdownOptions"
:render-option="renderDropdownOption"
+ :disabled="disabled"
@select="handleDropdownSelect"
>
- <NButton :quaternary="true" size="medium" style="--n-padding: 0 4px">
+ <NButton
+ :quaternary="true"
+ size="medium"
+ style="--n-padding: 0 4px"
+ :disabled="disabled"
+ :title="disabled ? disabledTooltip : undefined"
+ >
<EllipsisVerticalIcon class="w-5 h-5" />
</NButton>
</NDropdown>
@@ -37,6 +45,8 @@ import type { ActionConfig, UnifiedAction } from "./types";
const props = defineProps<{
primaryAction?: ActionConfig;
secondaryActions: ActionConfig[];
+ disabled?: boolean;
+ disabledTooltip?: string;
}>();
const emit = defineEmits<{
diff --git a/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue b/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue
index 550c560850..7fdc380731 100644
--- a/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue
+++ b/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue
@@ -24,7 +24,7 @@
</template>
<template v-else>
- <template v-if="!state.isEditing">
+ <template v-if="!editorState.isEditing.value">
<template v-if="shouldShowEditButton">
<!-- for small size sheets, show full featured UI editing button group -->
<NTooltip :disabled="denyEditStatementReasons.length === 0">
@@ -63,7 +63,7 @@
{{ $t("issue.upload-sql") }}
</SQLUploadButton>
<NButton
- v-if="state.isEditing"
+ v-if="editorState.isEditing.value"
size="small"
:disabled="!allowSaveSQL"
@click.prevent="saveEdit"
@@ -71,7 +71,7 @@
{{ $t("common.save") }}
</NButton>
<NButton
- v-if="state.isEditing"
+ v-if="editorState.isEditing.value"
size="small"
quaternary
@click.prevent="cancelEdit"
@@ -183,6 +183,7 @@ import {
usePlanContext,
planCheckRunListForSpec,
} from "@/components/Plan/logic";
+import { useEditorState } from "@/components/Plan/logic/useEditorState";
import DownloadSheetButton from "@/components/Sheet/DownloadSheetButton.vue";
import SQLUploadButton from "@/components/misc/SQLUploadButton.vue";
import { planServiceClientConnect } from "@/grpcweb";
@@ -207,7 +208,6 @@ import { useSQLAdviceMarkers } from "../useSQLAdviceMarkers";
import { useSpecSheet } from "../useSpecSheet";
type LocalState = {
- isEditing: boolean;
statement: string;
showEditorModal: boolean;
isUploadingFile: boolean;
@@ -220,9 +220,9 @@ const { isCreating, plan, planCheckRuns, rollout, events, readonly } =
usePlanContext();
const selectedSpec = useSelectedSpec();
const monacoEditorRef = ref<InstanceType<typeof MonacoEditor>>();
+const editorState = useEditorState();
const state = reactive<LocalState>({
- isEditing: false,
statement: "",
showEditorModal: false,
isUploadingFile: false,
@@ -268,7 +268,7 @@ const isEditorReadonly = computed(() => {
if (isCreating.value) {
return false;
}
- return !state.isEditing || isSheetOversize.value || false;
+ return !editorState.isEditing.value || isSheetOversize.value || false;
});
const { sheet, sheetName, sheetReady, sheetStatement } =
@@ -276,7 +276,7 @@ const { sheet, sheetName, sheetReady, sheetStatement } =
const isSheetOversize = computed(() => {
if (isCreating.value) return false;
- if (state.isEditing) return false;
+ if (editorState.isEditing.value) return false;
if (!sheetReady.value) return false;
if (!sheet.value) return false;
return (
@@ -306,7 +306,7 @@ const shouldShowEditButton = computed(() => {
}
// Will show another button group as [Upload][Cancel][Save]
// while editing
- if (state.isEditing) {
+ if (editorState.isEditing.value) {
return false;
}
if (plan.value.rollout && rollout?.value) {
@@ -350,20 +350,20 @@ const allowSaveSQL = computed((): boolean => {
});
const beginEdit = () => {
- state.isEditing = true;
+ editorState.setEditingState(true);
};
const saveEdit = async () => {
try {
await updateStatement(state.statement);
} finally {
- state.isEditing = false;
+ editorState.setEditingState(false);
}
};
const cancelEdit = () => {
state.statement = sheetStatement.value;
- state.isEditing = false;
+ editorState.setEditingState(false);
};
const showOverwriteConfirmDialog = () => {
@@ -397,7 +397,7 @@ const handleUpdateStatementAndOverwrite = async (
return;
}
- state.isEditing = true;
+ editorState.setEditingState(true);
state.statement = statement;
await handleUpdateStatement(statement, filename);
};
@@ -482,7 +482,7 @@ watch(
watch(isCreating, (curr, prev) => {
// Reset the edit state after creating the plan.
if (!curr && prev) {
- state.isEditing = false;
+ editorState.setEditingState(false);
}
});
</script>
diff --git a/frontend/src/components/Plan/logic/useEditorState.ts b/frontend/src/components/Plan/logic/useEditorState.ts
new file mode 100644
index 0000000000..67211aa9ca
--- /dev/null
+++ b/frontend/src/components/Plan/logic/useEditorState.ts
@@ -0,0 +1,20 @@
+import { ref, type Ref } from "vue";
+
+export interface EditorState {
+ isEditing: Ref<boolean>;
+}
+
+const editorState: EditorState = {
+ isEditing: ref(false),
+};
+
+export const useEditorState = () => {
+ const setEditingState = (editing: boolean) => {
+ editorState.isEditing.value = editing;
+ };
+
+ return {
+ isEditing: editorState.isEditing,
+ setEditingState,
+ };
+};
diff --git a/frontend/src/components/Plan/logic/useNavigationGuard.ts b/frontend/src/components/Plan/logic/useNavigationGuard.ts
new file mode 100644
index 0000000000..d12e0efb38
--- /dev/null
+++ b/frontend/src/components/Plan/logic/useNavigationGuard.ts
@@ -0,0 +1,36 @@
+import { useDialog } from "naive-ui";
+import { useI18n } from "vue-i18n";
+import { useEditorState } from "./useEditorState";
+
+export const useNavigationGuard = () => {
+ const dialog = useDialog();
+ const { t } = useI18n();
+ const editorState = useEditorState();
+
+ const confirmNavigation = (): Promise<boolean> => {
+ return new Promise((resolve) => {
+ // If not editing, allow navigation
+ if (!editorState.isEditing.value) {
+ resolve(true);
+ return;
+ }
+
+ // Show confirmation dialog when in editing mode
+ dialog.warning({
+ title: t("plan.editor.unsaved-changes"),
+ content: t("plan.editor.unsaved-changes-message"),
+ positiveText: t("common.leave"),
+ negativeText: t("common.stay"),
+ onPositiveClick: () => {
+ // Stop editing when user chooses to leave
+ editorState.setEditingState(false);
+ resolve(true);
+ },
+ onNegativeClick: () => resolve(false),
+ onClose: () => resolve(false),
+ });
+ });
+ };
+
+ return { confirmNavigation };
+};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index d378291bb0..481180fb44 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -45,6 +45,8 @@
"no-license": "No license",
"logout": "Logout",
"close": "Close",
+ "leave": "Leave",
+ "stay": "Stay",
"latest": "Latest",
"error": "Error",
"canceled": "Canceled",
@@ -1123,6 +1125,11 @@
"applies-to-some-tasks": "Applies to some tasks {count}/{total}",
"split-into-multiple-issues-tip": "If you want to configure different options for each task, please split into multiple issues."
},
+ "editor": {
+ "save-changes-before-continuing": "Please save or cancel your changes before continuing",
+ "unsaved-changes": "Unsaved Changes",
+ "unsaved-changes-message": "You have unsaved changes. Are you sure you want to leave?"
+ },
"pre-backup": {
"only-some-databases-checked": "Only some databases are checked",
"some-databases-not-meeting-requirements": "Some databases ({databases}) don't meet requirements",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 12fe5c088d..ed42457d3a 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -45,6 +45,8 @@
"no-license": "Sin licencia",
"logout": "Cerrar sesión",
"close": "Cerrar",
+ "leave": "Dejar",
+ "stay": "Permanecer",
"latest": "Último",
"error": "Error",
"canceled": "Cancelado",
@@ -1123,6 +1125,11 @@
"applies-to-some-tasks": "Se aplica a algunas tareas {count}/{total}",
"split-into-multiple-issues-tip": "Si desea configurar diferentes opciones para cada tarea, divídala en varios problemas."
},
+ "editor": {
+ "save-changes-before-continuing": "Guarde o cancele sus cambios antes de continuar.",
+ "unsaved-changes": "Cambios no guardados",
+ "unsaved-changes-message": "Tienes cambios sin guardar. ¿Estás seguro de que quieres salir?"
+ },
"pre-backup": {
"only-some-databases-checked": "Sólo se comprueban algunas bases de datos",
"some-databases-not-meeting-requirements": "Algunas bases de datos ({databases}) no cumplen los requisitos",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index ac7222e95b..c1f60396a4 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -45,6 +45,8 @@
"no-license": "ライセンスがありません",
"logout": "サインアウト",
"close": "閉じる",
+ "leave": "離れる",
+ "stay": "滞在する",
"latest": "最新の",
"error": "間違い",
"canceled": "キャンセル",
@@ -1123,6 +1125,11 @@
"applies-to-some-tasks": "一部のタスクに適用されます {count}/{total}",
"split-into-multiple-issues-tip": "タスクごとに異なるオプションを設定する場合は、複数の問題に分割してください。"
},
+ "editor": {
+ "save-changes-before-continuing": "続行する前に変更を保存またはキャンセルしてください",
+ "unsaved-changes": "保存されていない変更",
+ "unsaved-changes-message": "保存されていない変更があります。終了してもよろしいですか?"
+ },
"pre-backup": {
"only-some-databases-checked": "一部のデータベースのみがチェックされます",
"some-databases-not-meeting-requirements": "一部のデータベース ({databases}) は要件を満たしていません",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index f3ad8d147e..5f83da24a8 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -45,6 +45,8 @@
"no-license": "Không có giấy phép",
"logout": "Đăng xuất",
"close": "Đóng",
+ "leave": "Rời khỏi",
+ "stay": "Ở lại",
"latest": "Mới nhất",
"error": "Lỗi",
"canceled": "Đã hủy",
@@ -1123,6 +1125,11 @@
"applies-to-some-tasks": "Áp dụng cho một số tác vụ {count}/{total}",
"split-into-multiple-issues-tip": "Nếu bạn muốn cấu hình các tùy chọn khác nhau cho mỗi tác vụ, vui lòng chia thành nhiều vấn đề."
},
+ "editor": {
+ "save-changes-before-continuing": "Vui lòng lưu hoặc hủy thay đổi của bạn trước khi tiếp tục",
+ "unsaved-changes": "Những thay đổi chưa lưu",
+ "unsaved-changes-message": "Bạn chưa lưu thay đổi. Bạn có chắc chắn muốn thoát không?"
+ },
"pre-backup": {
"only-some-databases-checked": "Chỉ một số cơ sở dữ liệu được kiểm tra",
"some-databases-not-meeting-requirements": "Một số cơ sở dữ liệu ({databases}) không đáp ứng được yêu cầu",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index e5d3e4a775..f272c2ea33 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -45,6 +45,8 @@
"no-license": "无证书",
"logout": "登出",
"close": "关闭",
+ "leave": "离开",
+ "stay": "停留",
"latest": "最新",
"error": "错误",
"canceled": "已取消",
@@ -1123,6 +1125,11 @@
"applies-to-some-tasks": "适用于部分任务 {count}/{total}",
"split-into-multiple-issues-tip": "如果您想为每个任务设置不同的配置项,请拆分成多个工单。"
},
+ "editor": {
+ "save-changes-before-continuing": "请保存或取消更改,然后再继续",
+ "unsaved-changes": "未保存的更改",
+ "unsaved-changes-message": "有未保存的更改。您确定要离开吗?"
+ },
"pre-backup": {
"only-some-databases-checked": "仅检查部分数据库",
"some-databases-not-meeting-requirements": "部分数据库({databases})不符合要求",
diff --git a/frontend/src/views/project/CICDLayout.vue b/frontend/src/views/project/CICDLayout.vue
index e611f48350..c35b9d9a99 100644
--- a/frontend/src/views/project/CICDLayout.vue
+++ b/frontend/src/views/project/CICDLayout.vue
@@ -63,7 +63,12 @@ import { CirclePlayIcon, FileDiffIcon, Layers2Icon } from "lucide-vue-next";
import { NSpin, NTab, NTabs, NTag } from "naive-ui";
import { computed, ref, toRef, watch } from "vue";
import { useI18n } from "vue-i18n";
-import { useRoute, useRouter } from "vue-router";
+import {
+ useRoute,
+ useRouter,
+ onBeforeRouteLeave,
+ onBeforeRouteUpdate,
+} from "vue-router";
import {
providePlanContext,
useBasePlanContext,
@@ -73,6 +78,7 @@ import PollerProvider from "@/components/Plan/PollerProvider.vue";
import { HeaderSection } from "@/components/Plan/components";
import RefreshIndicator from "@/components/Plan/components/RefreshIndicator.vue";
import { provideIssueReviewContext } from "@/components/Plan/logic/issue-review";
+import { useNavigationGuard } from "@/components/Plan/logic/useNavigationGuard";
import { useIssueLayoutVersion } from "@/composables/useIssueLayoutVersion";
import { useBodyLayoutContext } from "@/layouts/common";
import {
@@ -141,6 +147,7 @@ const shouldShowNavigation = computed(() => {
const route = useRoute();
const router = useRouter();
+const { confirmNavigation } = useNavigationGuard();
providePlanContext({
isCreating,
@@ -333,4 +340,23 @@ const showBanner = computed(() => {
});
useTitle(documentTitle);
+
+// Set up navigation guards to check for unsaved changes
+onBeforeRouteLeave(async (_to, _from, next) => {
+ const canNavigate = await confirmNavigation();
+ if (canNavigate) {
+ next();
+ } else {
+ next(false);
+ }
+});
+
+onBeforeRouteUpdate(async (_to, _from, next) => {
+ const canNavigate = await confirmNavigation();
+ if (canNavigate) {
+ next();
+ } else {
+ next(false);
+ }
+});
</script>