Skip to content

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

feat(frontend): support filtering instance while transferring databases (#5456)

Liu Jidd0cc597a82f
diff --git a/frontend/src/components/TransferDatabaseForm.vue b/frontend/src/components/TransferDatabaseForm.vue
index e7a499d27d..a50cf20f04 100644
--- a/frontend/src/components/TransferDatabaseForm.vue
+++ b/frontend/src/components/TransferDatabaseForm.vue
@@ -10,7 +10,10 @@
<TransferSourceSelector
:project="project"
:transfer-source="state.transferSource"
+ :instance-filter="state.instanceFilter"
+ :search-text="state.searchText"
@change="state.transferSource = $event"
+ @select-instance="state.instanceFilter = $event"
@search-text-change="state.searchText = $event"
/>
</template>
@@ -37,6 +40,8 @@ import {
ProjectId,
DEFAULT_PROJECT_ID,
DatabaseLabel,
+ Instance,
+ UNKNOWN_ID,
} from "../types";
import {
buildDatabaseNameRegExpByTemplate,
@@ -54,6 +59,7 @@ import {
interface LocalState {
transferSource: TransferSource;
+ instanceFilter: Instance | undefined;
searchText: string;
loading: boolean;
}
@@ -75,6 +81,7 @@ const currentUser = useCurrentUser();
const state = reactive<LocalState>({
transferSource: props.projectId === DEFAULT_PROJECT_ID ? "OTHER" : "DEFAULT",
+ instanceFilter: undefined,
searchText: "",
loading: false,
});
@@ -118,6 +125,10 @@ const databaseList = computed(() => {
])
);
+ if (state.instanceFilter && state.instanceFilter.id !== UNKNOWN_ID) {
+ list = list.filter((db) => db.instance.id === state.instanceFilter?.id);
+ }
+
return sortDatabaseList(list, environmentList.value);
});
diff --git a/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue b/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue
index 68e65ae945..bf4d06fed0 100644
--- a/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue
+++ b/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue
@@ -32,22 +32,36 @@
</label>
</template>
</div>
- <div>
- <BBTableSearch
- class="m-px"
+ <NInputGroup style="width: auto">
+ <InstanceSelect
+ :instance="instanceFilter?.id ?? UNKNOWN_ID"
+ :include-all="true"
+ @update:instance="changeInstanceFilter"
+ />
+ <SearchBox
:value="searchText"
:placeholder="$t('database.search-database')"
- @change-text="(text: string) => $emit('search-text-change', text)"
+ @update:value="$emit('search-text-change', $event)"
/>
- </div>
+ </NInputGroup>
</div>
</div>
</template>
<script lang="ts" setup>
-import { PropType, reactive, watch } from "vue";
+import { PropType, reactive, watch, watchEffect } from "vue";
+import { NInputGroup } from "naive-ui";
+
import { TransferSource } from "./utils";
-import { Project, DEFAULT_PROJECT_ID } from "@/types";
+import {
+ type Project,
+ type Instance,
+ DEFAULT_PROJECT_ID,
+ UNKNOWN_ID,
+ InstanceId,
+} from "@/types";
+import { InstanceSelect, SearchBox } from "@/components/v2";
+import { useInstanceStore } from "@/store";
interface LocalState {
transferSource: TransferSource;
@@ -62,6 +76,10 @@ const props = defineProps({
type: String as PropType<TransferSource>,
required: true,
},
+ instanceFilter: {
+ type: Object as PropType<Instance>,
+ default: undefined,
+ },
searchText: {
type: String,
default: "",
@@ -70,6 +88,7 @@ const props = defineProps({
const emit = defineEmits<{
(event: "change", src: TransferSource): void;
+ (event: "select-instance", instance: Instance | undefined): void;
(event: "search-text-change", searchText: string): void;
}>();
@@ -77,6 +96,17 @@ const state = reactive<LocalState>({
transferSource: props.transferSource,
});
+const prepare = () => {
+ useInstanceStore().fetchInstanceList();
+};
+
+const changeInstanceFilter = (instanceId: InstanceId | undefined) => {
+ if (!instanceId || instanceId === UNKNOWN_ID) {
+ return emit("select-instance", undefined);
+ }
+ emit("select-instance", useInstanceStore().getInstanceById(instanceId));
+};
+
watch(
() => props.transferSource,
(src) => (state.transferSource = src)
@@ -90,4 +120,6 @@ watch(
}
}
);
+
+watchEffect(prepare);
</script>
diff --git a/frontend/src/components/v2/Form/SearchBox.vue b/frontend/src/components/v2/Form/SearchBox.vue
new file mode 100644
index 0000000000..1c70660780
--- /dev/null
+++ b/frontend/src/components/v2/Form/SearchBox.vue
@@ -0,0 +1,39 @@
+<template>
+ <NInput
+ :value="value"
+ :clearable="true"
+ :placeholder="placeholder"
+ style="width: 12rem"
+ @update:value="$emit('update:value', $event)"
+ >
+ <template #prefix>
+ <heroicons-outline:search class="h-4 w-4 text-control-placeholder" />
+ </template>
+ </NInput>
+</template>
+
+<script lang="ts" setup>
+import { computed, useAttrs } from "vue";
+import { useI18n } from "vue-i18n";
+import { NInput } from "naive-ui";
+
+withDefaults(
+ defineProps<{
+ value?: string;
+ }>(),
+ {
+ value: "",
+ }
+);
+
+defineEmits<{
+ (event: "update:value", value: string): void;
+}>();
+
+const attrs = useAttrs();
+const { t } = useI18n();
+
+const placeholder = computed(() => {
+ return (attrs.placeholder as string) ?? t("common.search");
+});
+</script>
diff --git a/frontend/src/components/v2/Form/index.ts b/frontend/src/components/v2/Form/index.ts
index 6b9951c199..7525c18df7 100644
--- a/frontend/src/components/v2/Form/index.ts
+++ b/frontend/src/components/v2/Form/index.ts
@@ -1,3 +1,4 @@
import SpinnerSwitch from "./SpinnerSwitch.vue";
+import SearchBox from "./SearchBox.vue";
-export { SpinnerSwitch };
+export { SpinnerSwitch, SearchBox };
diff --git a/frontend/src/components/v2/Select/InstanceSelect.vue b/frontend/src/components/v2/Select/InstanceSelect.vue
new file mode 100644
index 0000000000..7676afbffe
--- /dev/null
+++ b/frontend/src/components/v2/Select/InstanceSelect.vue
@@ -0,0 +1,132 @@
+<template>
+ <NSelect
+ :value="instance"
+ :options="options"
+ :placeholder="$t('instance.select')"
+ style="width: 12rem"
+ @update:value="$emit('update:instance', $event)"
+ />
+</template>
+
+<script lang="ts" setup>
+import { computed, watch, h } from "vue";
+import { NSelect, SelectOption } from "naive-ui";
+import { useI18n } from "vue-i18n";
+
+import { useInstanceStore } from "@/store";
+import {
+ InstanceId,
+ EnvironmentId,
+ Instance,
+ UNKNOWN_ID,
+ unknown,
+ EngineType,
+ EngineTypeList,
+} from "@/types";
+import InstanceEngineIcon from "@/components/InstanceEngineIcon.vue";
+
+const props = withDefaults(
+ defineProps<{
+ instance: InstanceId | undefined;
+ environment?: EnvironmentId;
+ allowedEngineTypeList?: readonly EngineType[];
+ includeAll?: boolean;
+ includeArchived?: boolean;
+ autoReset?: boolean;
+ filter?: (instance: Instance, index: number) => boolean;
+ }>(),
+ {
+ environment: undefined,
+ allowedEngineTypeList: () => EngineTypeList,
+ includeAll: false,
+ includeArchived: false,
+ autoReset: true,
+ filter: undefined,
+ }
+);
+
+const emit = defineEmits<{
+ (event: "update:instance", value: InstanceId | undefined): void;
+}>();
+
+const { t } = useI18n();
+const instanceStore = useInstanceStore();
+
+const rawInstanceList = computed(() => {
+ let list: Instance[] = [];
+ if (props.environment && props.environment !== UNKNOWN_ID) {
+ list = instanceStore.getInstanceListByEnvironmentId(props.environment, [
+ "NORMAL",
+ "ARCHIVED",
+ ]);
+ } else {
+ list = instanceStore.getInstanceList(["NORMAL", "ARCHIVED"]);
+ }
+ // Filter by engine type
+ list = list.filter((instance) =>
+ props.allowedEngineTypeList.includes(instance.engine)
+ );
+ return list;
+});
+
+const combinedInstanceList = computed(() => {
+ let list = rawInstanceList.value.filter((instance) => {
+ if (props.includeArchived) return true;
+ if (instance.rowStatus === "NORMAL") return true;
+ // ARCHIVED
+ if (instance.id === props.instance) return true;
+ return false;
+ });
+
+ if (props.filter) {
+ list = list.filter(props.filter);
+ }
+
+ if (props.instance === UNKNOWN_ID || props.includeAll) {
+ const dummyAll = unknown("INSTANCE");
+ dummyAll.name = t("instance.all");
+ list.unshift(dummyAll);
+ }
+
+ return list;
+});
+
+const options = computed(() => {
+ return combinedInstanceList.value.map<SelectOption>((instance) => {
+ return {
+ value: instance.id,
+ label: () => {
+ if (instance.id === UNKNOWN_ID) {
+ return t("instance.all");
+ }
+ const icon = h(InstanceEngineIcon, { instance, class: "shrink-0" });
+ const text = h("span", {}, instance.name);
+ return h(
+ "div",
+ {
+ class: "flex items-center gap-x-2",
+ },
+ [icon, text]
+ );
+ },
+ };
+ });
+});
+
+// The instance list might change if environment changes, and the previous selected id
+// might not exist in the new list. In such case, we need to reset the selection
+// and emit the event.
+const resetInvalidSelection = () => {
+ if (!props.autoReset) return;
+ if (
+ props.instance &&
+ !combinedInstanceList.value.find((item) => item.id === props.instance)
+ ) {
+ emit("update:instance", undefined);
+ }
+};
+
+watch([() => props.instance, combinedInstanceList], resetInvalidSelection, {
+ immediate: true,
+});
+</script>
diff --git a/frontend/src/components/v2/Select/index.ts b/frontend/src/components/v2/Select/index.ts
new file mode 100644
index 0000000000..24fb471c76
--- /dev/null
+++ b/frontend/src/components/v2/Select/index.ts
@@ -0,0 +1,3 @@
+import InstanceSelect from "./InstanceSelect.vue";
+
+export { InstanceSelect };
diff --git a/frontend/src/components/v2/index.ts b/frontend/src/components/v2/index.ts
index 66209c1fff..cb6f25f5ac 100644
--- a/frontend/src/components/v2/index.ts
+++ b/frontend/src/components/v2/index.ts
@@ -1,3 +1,4 @@
export * from "./TabFilter";
export * from "./Model";
export * from "./Form";
+export * from "./Select";
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 4482e7cdae..217cee7f53 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -876,7 +876,8 @@
"grants": "Grants",
"find-gcp-project-id-and-instance-id": "To find GCP Project ID and instance ID, see",
"create-gcp-credentials": "To create Credentials, see",
- "used-for-testing-connection": "Used for testing connections only"
+ "used-for-testing-connection": "Used for testing connections only",
+ "all": "All instances"
},
"environment": {
"id": "Environment ID",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 1f761403bd..b322ffec31 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -874,7 +874,8 @@
"authentication-database": "认证数据库",
"find-gcp-project-id-and-instance-id": "查看 GCP 项目 ID 与实例 ID 的方法见",
"create-gcp-credentials": "创建凭据的方法见",
- "used-for-testing-connection": "仅作测试连接用"
+ "used-for-testing-connection": "仅作测试连接用",
+ "all": "所有实例"
},
"environment": {
"id": "环境 ID",