Skip to content

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

refactor: selector (#18567)

ecmadaoe1255e233fe4
diff --git a/frontend/src/components/CreateDatabasePrepForm/CreateDatabasePrepPanel.vue b/frontend/src/components/CreateDatabasePrepForm/CreateDatabasePrepPanel.vue
index 897358a754..5c8114ac2a 100644
--- a/frontend/src/components/CreateDatabasePrepForm/CreateDatabasePrepPanel.vue
+++ b/frontend/src/components/CreateDatabasePrepForm/CreateDatabasePrepPanel.vue
@@ -4,7 +4,7 @@
class="w-[40rem] max-w-[100vw]"
>
<div class="mx-auto flex flex-col gap-y-4">
- <div v-if="!isValidProjectName(currentProject.name)" class="w-full">
+ <div class="w-full">
<label for="project" class="textlabel">
{{ $t("common.project") }}
<RequiredStar />
@@ -12,7 +12,8 @@
<ProjectSelect
class="mt-1 w-full!"
required
- v-model:project-name="state.projectName"
+ :disabled="isValidProjectName(currentProject.name)"
+ v-model:value="state.projectName"
/>
</div>
@@ -25,9 +26,9 @@
name="instance"
required
:disabled="!allowEditInstance"
- :instance-name="state.instanceName"
+ :value="state.instanceName"
:allowed-engine-list="supportedEngines"
- @update:instance-name="selectInstance"
+ @update:value="selectInstance"
/>
</div>
@@ -84,7 +85,7 @@
{{ $t("common.environment") }}
</label>
<EnvironmentSelect
- v-model:environment-name="state.environmentName"
+ v-model:value="state.environmentName"
class="mt-1"
required
name="environment"
diff --git a/frontend/src/components/DatabaseDetail/Settings/DatabaseSettingsPanel.vue b/frontend/src/components/DatabaseDetail/Settings/DatabaseSettingsPanel.vue
index 50af4f763b..6914c3c631 100644
--- a/frontend/src/components/DatabaseDetail/Settings/DatabaseSettingsPanel.vue
+++ b/frontend/src/components/DatabaseDetail/Settings/DatabaseSettingsPanel.vue
@@ -7,7 +7,7 @@
</p>
<EnvironmentSelect
class="mt-1 max-w-md"
- :environment-name="`${environmentNamePrefix}${environment.id}`"
+ :value="`${environmentNamePrefix}${environment.id}`"
:disabled="!allowUpdateDatabase"
:clearable="!database.instanceResource.environment"
:render-suffix="
@@ -16,7 +16,7 @@
? `(${$t('common.default')})`
: ''
"
- @update:environment-name="handleSelectEnvironment"
+ @update:value="handleSelectEnvironment($event as (string | undefined))"
/>
</div>
</div>
diff --git a/frontend/src/components/EditEnvironmentDrawer.vue b/frontend/src/components/EditEnvironmentDrawer.vue
index cb92cdb7f0..1746dd5f68 100644
--- a/frontend/src/components/EditEnvironmentDrawer.vue
+++ b/frontend/src/components/EditEnvironmentDrawer.vue
@@ -5,7 +5,7 @@
class="w-96 max-w-[100vw]"
>
<EnvironmentSelect
- v-model:environment-name="environment"
+ v-model:value="environment"
class="mt-1"
required
name="environment"
diff --git a/frontend/src/components/InstanceForm/Form.vue b/frontend/src/components/InstanceForm/Form.vue
index a698e93dbb..82aa2515d1 100644
--- a/frontend/src/components/InstanceForm/Form.vue
+++ b/frontend/src/components/InstanceForm/Form.vue
@@ -93,7 +93,7 @@
<EnvironmentSelect
class="mt-1 w-full"
required="true"
- :environment-name="
+ :value="
isValidEnvironmentName(
`${environmentNamePrefix}${environment.id}`
)
@@ -101,7 +101,7 @@
: undefined
"
:disabled="!allowEdit"
- @update:environment-name="handleSelectEnvironment"
+ @update:value="handleSelectEnvironment($event as (string | undefined))"
/>
</div>
diff --git a/frontend/src/components/Member/MembersBindingSelect.vue b/frontend/src/components/Member/MembersBindingSelect.vue
index e028dc6350..85d7c0e4ee 100644
--- a/frontend/src/components/Member/MembersBindingSelect.vue
+++ b/frontend/src/components/Member/MembersBindingSelect.vue
@@ -39,14 +39,14 @@
</div>
<UserSelect
key="user-select"
- :users="memberList"
+ :value="memberList"
:multiple="true"
:disabled="disabled"
:project-name="projectName"
:include-all-users="includeAllUsers"
:include-service-account="includeServiceAccount"
:include-workload-identity="includeWorkloadIdentity"
- @update:users="onMemberListUpdate"
+ @update:value="onMemberListUpdate($event as string[])"
/>
</div>
<div
@@ -62,11 +62,11 @@
<GroupSelect
key="group-select"
- :groups="memberList"
+ :value="memberList"
:disabled="disabled"
:multiple="true"
:project-name="projectName"
- @update:groups="onMemberListUpdate"
+ @update:value="onMemberListUpdate($event as string[])"
/>
</div>
</div>
diff --git a/frontend/src/components/SQLReview/SQLReviewInfo.vue b/frontend/src/components/SQLReview/SQLReviewInfo.vue
index eeb5cac664..5c60206deb 100644
--- a/frontend/src/components/SQLReview/SQLReviewInfo.vue
+++ b/frontend/src/components/SQLReview/SQLReviewInfo.vue
@@ -24,18 +24,19 @@
class="mt-2"
required
name="environment"
- :environment-name="attachedResources[0]"
+ :value="attachedResources[0]"
:disabled="!allowChangeAttachedResource"
:filter="
- (env: Environment, _: number) =>
+ (env: Environment) =>
filterResource(formatEnvironmentName(env.id))
"
- @update:environment-name="
- (val: string | undefined) => {
- if (!val) {
+ @update:value="
+ (val) => {
+ const name = val as string
+ if (!name) {
$emit('attached-resources-change', []);
} else {
- $emit('attached-resources-change', [val]);
+ $emit('attached-resources-change', [name]);
}
}
"
@@ -45,15 +46,16 @@
class="mt-2"
style="width: 100%"
required
- :project-name="attachedResources[0]"
+ :value="attachedResources[0]"
:disabled="!allowChangeAttachedResource"
:filter="(proj) => filterResource(proj.name)"
- @update:project-name="
- (val: string | undefined) => {
+ @update:value="
+ (val) => {
+ const name = val as string
if (!val) {
$emit('attached-resources-change', []);
} else {
- $emit('attached-resources-change', [val]);
+ $emit('attached-resources-change', [name]);
}
}
"
@@ -63,10 +65,17 @@
class="mt-2"
style="width: 100%"
required
- :database-name="attachedResources[0]"
+ :value="attachedResources[0]"
:disabled="!allowChangeAttachedResource"
:filter="(db: Database) => filterResource(db.name)"
- @update:database-names="$emit('attached-resources-change', $event)"
+ @update:value="(val) => {
+ const name = val as string
+ if (!name) {
+ $emit('attached-resources-change', []);
+ } else {
+ $emit('attached-resources-change', [name]);
+ }
+ }"
/>
</div>
<div>
diff --git a/frontend/src/components/SQLReview/components/SQLReviewAttachResourcesPanel.vue b/frontend/src/components/SQLReview/components/SQLReviewAttachResourcesPanel.vue
index 37bbee80c4..4f3f6b568a 100644
--- a/frontend/src/components/SQLReview/components/SQLReviewAttachResourcesPanel.vue
+++ b/frontend/src/components/SQLReview/components/SQLReviewAttachResourcesPanel.vue
@@ -19,11 +19,11 @@
<EnvironmentSelect
class="mt-3"
required
- :environment-names="environmentNames"
+ :value="environmentNames"
:multiple="true"
:render-suffix="getResourceAttachedConfigName"
- @update:environment-names="
- onResourcesChange($event, projectNames)
+ @update:value="
+ onResourcesChange($event as string[], projectNames)
"
/>
</div>
@@ -42,11 +42,11 @@
class="mt-3"
style="width: 100%"
required
- :project-names="projectNames"
+ :value="projectNames"
:multiple="true"
:render-suffix="getResourceAttachedConfigName"
- @update:project-names="
- onResourcesChange($event, environmentNames)
+ @update:value="
+ onResourcesChange($event as string[], environmentNames)
"
/>
</div>
diff --git a/frontend/src/components/SyncDatabaseSchemaV1/DatabaseSchemaSelector.vue b/frontend/src/components/SyncDatabaseSchemaV1/DatabaseSchemaSelector.vue
index b9d2bd5ac8..a00cb624e7 100644
--- a/frontend/src/components/SyncDatabaseSchemaV1/DatabaseSchemaSelector.vue
+++ b/frontend/src/components/SyncDatabaseSchemaV1/DatabaseSchemaSelector.vue
@@ -11,16 +11,16 @@
<EnvironmentSelect
class="mr-3"
name="environment"
- :environment-name="state.environmentName"
- @update:environment-name="handleEnvironmentSelect"
+ :value="state.environmentName"
+ @update:value="handleEnvironmentSelect($event as (string | undefined))"
/>
<DatabaseSelect
:placeholder="$t('database.select')"
:project-name="project.name"
- :database-name="state.databaseName"
+ :value="state.databaseName"
:environment-name="state.environmentName"
:allowed-engine-type-list="ALLOWED_ENGINES"
- @update:database-name="handleDatabaseSelect"
+ @update:value="(val) => handleDatabaseSelect(val as (string | undefined))"
/>
</div>
<div
diff --git a/frontend/src/components/TransferDatabaseForm/TransferDatabaseForm.vue b/frontend/src/components/TransferDatabaseForm/TransferDatabaseForm.vue
index 89cfaf242e..3be822bea6 100644
--- a/frontend/src/components/TransferDatabaseForm/TransferDatabaseForm.vue
+++ b/frontend/src/components/TransferDatabaseForm/TransferDatabaseForm.vue
@@ -15,9 +15,9 @@
class="w-48!"
:include-all="false"
:include-default-project="true"
- :project-name="state.fromProjectName"
+ :value="state.fromProjectName"
:filter="filterSourceProject"
- @update:project-name="changeProjectFilter"
+ @update:value="changeProjectFilter($event as (string | undefined))"
/>
<template
v-if="state.transferSource === 'OTHER' && !state.fromProjectName"
diff --git a/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue b/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue
index 8ed27058aa..ff56bfbace 100644
--- a/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue
+++ b/frontend/src/components/TransferDatabaseForm/TransferSourceSelector.vue
@@ -21,17 +21,17 @@
<NInputGroup style="width: auto">
<EnvironmentSelect
class="w-40!"
- :environment-name="
+ :value="
environment ? formatEnvironmentName(environment.id) : undefined
"
- @update:environment-name="changeEnvironmentFilter"
+ @update:value="changeEnvironmentFilter($event as (string | undefined))"
/>
<InstanceSelect
class="w-40!"
:project-name="sourceProjectName"
- :instance-name="instance?.name ?? ''"
+ :value="instance?.name ?? ''"
:environment-name="environment?.name"
- @update:instance-name="changeInstanceFilter"
+ @update:value="changeInstanceFilter"
/>
<SearchBox
class="w-40!"
@@ -85,6 +85,7 @@ const emit = defineEmits<{
}>();
const changeEnvironmentFilter = (name: string | undefined) => {
+ emit("update:instance", undefined);
if (!isValidEnvironmentName(name)) {
return emit("update:environment", undefined);
}
@@ -92,7 +93,6 @@ const changeEnvironmentFilter = (name: string | undefined) => {
"update:environment",
useEnvironmentV1Store().getEnvironmentByName(name)
);
- emit("update:instance", undefined);
};
const changeInstanceFilter = (name: string | undefined) => {
diff --git a/frontend/src/components/TransferOutDatabaseForm/TransferOutDatabaseForm.vue b/frontend/src/components/TransferOutDatabaseForm/TransferOutDatabaseForm.vue
index 943b86037d..22f502706c 100644
--- a/frontend/src/components/TransferOutDatabaseForm/TransferOutDatabaseForm.vue
+++ b/frontend/src/components/TransferOutDatabaseForm/TransferOutDatabaseForm.vue
@@ -34,7 +34,7 @@
</NRadioGroup>
<ProjectSelect
v-if="transfer === 'project'"
- v-model:project-name="targetProjectName"
+ v-model:value="targetProjectName"
:allowed-project-role-list="[PresetRoleType.PROJECT_OWNER]"
/>
</div>
diff --git a/frontend/src/components/User/Settings/CreateGroupDrawer.vue b/frontend/src/components/User/Settings/CreateGroupDrawer.vue
index dc2abdfda8..7dd5f117ae 100644
--- a/frontend/src/components/User/Settings/CreateGroupDrawer.vue
+++ b/frontend/src/components/User/Settings/CreateGroupDrawer.vue
@@ -58,13 +58,13 @@
class="w-full flex items-center gap-x-3"
>
<UserSelect
- :user="extractUserId(member.member)"
+ :value="extractUserId(member.member)"
:multiple="false"
:size="'medium'"
:include-all="false"
:disabled="!allowEdit"
:filter="(user) => userFilter(user, member.member)"
- @update:user="(email) => updateMemberEmail(i, email)"
+ @update:value="($event) => updateMemberEmail(i, $event as (string | undefined))"
/>
<GroupMemberRoleSelect
:value="member.role"
diff --git a/frontend/src/components/v2/Select/DatabaseGroupSelect.vue b/frontend/src/components/v2/Select/DatabaseGroupSelect.vue
deleted file mode 100644
index b65e6fba36..0000000000
--- a/frontend/src/components/v2/Select/DatabaseGroupSelect.vue
+++ /dev/null
@@ -1,41 +0,0 @@
-<template>
- <LocalResourceSelector
- v-bind="$attrs"
- :placeholder="$t('database-group.select')"
- :value="selected"
- :options="dbGroupOptions"
- :disabled="disabled"
- @update:value="$emit('update:selected', $event)"
- />
-</template>
-
-<script lang="ts" setup>
-import { computed } from "vue";
-import { useDBGroupListByProject } from "@/store";
-import LocalResourceSelector from "./LocalResourceSelector.vue";
-
-const props = withDefaults(
- defineProps<{
- project: string;
- selected?: string;
- disabled?: boolean;
- }>(),
- {
- selected: undefined,
- }
-);
-
-defineEmits<{
- (event: "update:selected", name: string | undefined): void;
-}>();
-
-const { dbGroupList } = useDBGroupListByProject(props.project);
-
-const dbGroupOptions = computed(() => {
- return dbGroupList.value.map((dbGroup) => ({
- resource: dbGroup,
- value: dbGroup.name,
- label: dbGroup.title,
- }));
-});
-</script>
diff --git a/frontend/src/components/v2/Select/DatabaseSelect.vue b/frontend/src/components/v2/Select/DatabaseSelect.vue
index 761451a6bb..7630f8ee3d 100644
--- a/frontend/src/components/v2/Select/DatabaseSelect.vue
+++ b/frontend/src/components/v2/Select/DatabaseSelect.vue
@@ -3,79 +3,83 @@
v-bind="$attrs"
class="bb-database-select"
:multiple="multiple"
- :value="databaseName"
- :values="databaseNames"
- :custom-label="renderLabel"
- :additional-data="additionalData"
+ :disabled="disabled"
+ :size="size"
+ :value="value"
+ :render-label="renderLabel"
+ :render-tag="renderTag"
+ :additional-options="additionalOptions"
:search="handleSearch"
- :get-option="getOption"
:filter="filter"
- @update:value="(val) => $emit('update:database-name', val)"
- @update:values="(val) => $emit('update:database-names', val)"
+ @update:value="(val) => $emit('update:value', val)"
/>
</template>
<script lang="tsx" setup>
import { computedAsync } from "@vueuse/core";
+import { computed } from "vue";
import { RichDatabaseName } from "@/components/v2";
import { useDatabaseV1Store } from "@/store";
import { workspaceNamePrefix } from "@/store/modules/v1/common";
import type { ComposedDatabase } from "@/types";
-import { isValidDatabaseName } from "@/types";
import { type Engine } from "@/types/proto-es/v1/common_pb";
-import RemoteResourceSelector from "./RemoteResourceSelector.vue";
+import RemoteResourceSelector from "./RemoteResourceSelector/index.vue";
+import type {
+ ResourceSelectOption,
+ SelectSize,
+} from "./RemoteResourceSelector/types";
+import {
+ getRenderLabelFunc,
+ getRenderTagFunc,
+} from "./RemoteResourceSelector/utils";
const props = withDefaults(
defineProps<{
- databaseName?: string; // UNKNOWN_DATABASE_NAME stands for "ALL"
- databaseNames?: string[];
+ value?: string | string[] | undefined;
environmentName?: string;
projectName?: string;
allowedEngineTypeList?: Engine[];
filter?: (database: ComposedDatabase) => boolean;
multiple?: boolean;
- clearable?: boolean;
+ disabled?: boolean;
+ size?: SelectSize;
showInstance?: boolean;
}>(),
{
- databaseName: undefined,
- databaseNames: undefined,
- environmentName: undefined,
- projectName: undefined,
// empty equals no limit.
allowedEngineTypeList: () => [],
- filter: undefined,
- multiple: false,
- clearable: false,
showInstance: true,
}
);
defineEmits<{
- (event: "update:database-name", value: string | undefined): void;
- (event: "update:database-names", value: string[]): void;
+ (event: "update:value", value: string[] | string | undefined): void;
}>();
const databaseStore = useDatabaseV1Store();
-const additionalData = computedAsync(async () => {
- const data = [];
+const getOption = (db: ComposedDatabase) => {
+ return {
+ resource: db,
+ value: db.name,
+ label: db.databaseName,
+ };
+};
+
+const additionalOptions = computedAsync(async () => {
+ const options: ResourceSelectOption<ComposedDatabase>[] = [];
let databaseNames: string[] = [];
- if (props.databaseName) {
- databaseNames = [props.databaseName];
- } else if (props.databaseNames) {
- databaseNames = props.databaseNames;
+ if (Array.isArray(props.value)) {
+ databaseNames = props.value;
+ } else if (props.value) {
+ databaseNames = [props.value];
}
- for (const databaseName of databaseNames) {
- if (isValidDatabaseName(databaseName)) {
- const db = await databaseStore.getOrFetchDatabaseByName(databaseName);
- data.push(db);
- }
- }
+ const databases = await databaseStore.batchGetDatabases(databaseNames);
+ options.push(...databases.map(getOption));
- return data;
+ return options;
}, []);
const handleSearch = async (params: {
@@ -93,15 +97,13 @@ const handleSearch = async (params: {
pageToken: params.pageToken,
pageSize: params.pageSize,
});
- return { nextPageToken, data: databases };
+ return {
+ nextPageToken,
+ options: databases.map(getOption),
+ };
};
-const getOption = (database: ComposedDatabase) => ({
- value: database.name,
- label: database.databaseName,
-});
-
-const renderLabel = (database: ComposedDatabase, keyword: string) => {
+const customLabel = (database: ComposedDatabase, keyword: string) => {
return (
<RichDatabaseName
database={database}
@@ -112,4 +114,21 @@ const renderLabel = (database: ComposedDatabase, keyword: string) => {
/>
);
};
+
+const renderLabel = computed(() => {
+ return getRenderLabelFunc({
+ multiple: props.multiple,
+ customLabel,
+ showResourceName: true,
+ });
+});
+
+const renderTag = computed(() => {
+ return getRenderTagFunc({
+ multiple: props.multiple,
+ disabled: props.disabled,
+ size: props.size,
+ customLabel,
+ });
+});
</script>
diff --git a/frontend/src/components/v2/Select/EnvironmentSelect.vue b/frontend/src/components/v2/Select/EnvironmentSelect.vue
index aa503a9e70..25d281108f 100644
--- a/frontend/src/components/v2/Select/EnvironmentSelect.vue
+++ b/frontend/src/components/v2/Select/EnvironmentSelect.vue
@@ -1,54 +1,61 @@
<template>
- <LocalResourceSelector
+ <NSelect
v-bind="$attrs"
+ :filterable="true"
+ :virtual-scroll="true"
:placeholder="t('environment.select')"
:multiple="multiple"
- :value="environmentName"
- :values="environmentNames"
+ :disabled="disabled"
+ :size="size"
+ :value="value"
:options="options"
- :custom-label="renderLabel"
+ :render-label="renderLabel"
+ :render-tag="renderTag"
+ :filter="filterEnvironment"
+ :consistent-menu-width="true"
class="bb-environment-select"
- @update:value="(val) => $emit('update:environment-name', val)"
- @update:values="(val) => $emit('update:environment-names', val)"
+ @update:value="(val) => $emit('update:value', val)"
/>
</template>
<script lang="tsx" setup>
+import type { SelectOption } from "naive-ui";
+import { NSelect } from "naive-ui";
import { computed, type VNodeChild } from "vue";
import { useI18n } from "vue-i18n";
import { useEnvironmentV1Store } from "@/store";
import { formatEnvironmentName } from "@/types";
import type { Environment } from "@/types/v1/environment";
import { EnvironmentV1Name } from "../Model";
-import LocalResourceSelector from "./LocalResourceSelector.vue";
+import type { SelectSize } from "./RemoteResourceSelector/types";
+import {
+ getRenderLabelFunc,
+ getRenderTagFunc,
+} from "./RemoteResourceSelector/utils";
const { t } = useI18n();
const props = withDefaults(
defineProps<{
- environmentName?: string | undefined;
- environmentNames?: string[] | undefined;
+ value?: string[] | string | undefined;
includeArchived?: boolean;
showProductionIcon?: boolean;
multiple?: boolean;
- filter?: (environment: Environment, index: number) => boolean;
+ disabled?: boolean;
+ size?: SelectSize;
+ filter?: (environment: Environment) => boolean;
renderSuffix?: (environment: string) => VNodeChild;
}>(),
{
- environmentName: undefined,
- environmentNames: undefined,
- includeArchived: false,
showProductionIcon: true,
- multiple: false,
- filter: () => true,
renderSuffix: () => null,
}
);
defineEmits<{
- (event: "update:environment-name", name: string | undefined): void;
- (event: "update:environment-names", names: string[]): void;
+ (event: "update:value", name: string[] | string | undefined): void;
}>();
+
const environmentV1Store = useEnvironmentV1Store();
const rawEnvironmentList = computed(() => {
@@ -68,17 +75,13 @@ const combinedEnvironmentList = computed(() => {
const options = computed(() => {
return combinedEnvironmentList.value.map((environment) => {
return {
- resource: {
- ...environment,
- name: formatEnvironmentName(environment.id),
- },
value: formatEnvironmentName(environment.id),
label: environment.title,
};
});
});
-const renderLabel = (environment: Environment) => {
+const customLabel = (environment: Environment) => {
return (
<div class="flex items-center gap-x-2">
<EnvironmentV1Name
@@ -90,4 +93,29 @@ const renderLabel = (environment: Environment) => {
</div>
);
};
+
+const renderLabel = (option: SelectOption, selected: boolean) =>
+ getRenderLabelFunc({
+ multiple: props.multiple,
+ customLabel,
+ showResourceName: true,
+ })(option, selected, "");
+
+const renderTag = computed(() => {
+ return getRenderTagFunc({
+ multiple: props.multiple,
+ disabled: props.disabled,
+ size: props.size,
+ customLabel,
+ });
+});
+
+const filterEnvironment = (pattern: string, option: SelectOption) => {
+ const { value, label } = option;
+ const search = pattern.trim().toLowerCase();
+ return (
+ (value as string).toLowerCase().includes(search) ||
+ (label as string).toLowerCase().includes(search)
+ );
+};
</script>
diff --git a/frontend/src/components/v2/Select/GroupSelect.vue b/frontend/src/components/v2/Select/GroupSelect.vue
index bc0bff46f8..a070c6185a 100644
--- a/frontend/src/components/v2/Select/GroupSelect.vue
+++ b/frontend/src/components/v2/Select/GroupSelect.vue
@@ -1,65 +1,70 @@
<template>
<RemoteResourceSelector
v-bind="$attrs"
- :value="group"
- :values="groups"
+ :value="value"
:disabled="disabled"
:multiple="multiple"
- :custom-label="renderLabel"
- :additional-data="additionalData"
+ :size="size"
+ :render-label="renderLabel"
+ :render-tag="renderTag"
+ :additional-options="additionalOptions"
:search="handleSearch"
- :get-option="getOption"
- @update:value="(val) => $emit('update:group', val)"
- @update:values="(val) => $emit('update:groups', val)"
+ @update:value="(val) => $emit('update:value', val)"
/>
</template>
<script lang="tsx" setup>
import { computedAsync } from "@vueuse/core";
+import { computed } from "vue";
import GroupNameCell from "@/components/User/Settings/UserDataTableByGroup/cells/GroupNameCell.vue";
import { useGroupStore } from "@/store";
import type { Group } from "@/types/proto-es/v1/group_service_pb";
-import RemoteResourceSelector from "./RemoteResourceSelector.vue";
+import RemoteResourceSelector from "./RemoteResourceSelector/index.vue";
+import type {
+ ResourceSelectOption,
+ SelectSize,
+} from "./RemoteResourceSelector/types";
+import {
+ getRenderLabelFunc,
+ getRenderTagFunc,
+} from "./RemoteResourceSelector/utils";
-const props = withDefaults(
- defineProps<{
- group?: string | undefined;
- groups?: string[] | undefined;
- disabled?: boolean;
- multiple?: boolean;
- projectName?: string;
- }>(),
- {
- group: undefined,
- groups: undefined,
- multiple: false,
- projectName: undefined,
- }
-);
+const props = defineProps<{
+ value?: string[] | string | undefined;
+ disabled?: boolean;
+ multiple?: boolean;
+ projectName?: string;
+ size?: SelectSize;
+}>();
defineEmits<{
- (event: "update:group", val: string | undefined): void;
- (event: "update:groups", val: string[]): void;
+ (event: "update:value", val: string[] | string | undefined): void;
}>();
const groupStore = useGroupStore();
-const additionalData = computedAsync(async () => {
- const data = [];
+const getOption = (group: Group): ResourceSelectOption<Group> => ({
+ resource: group,
+ value: group.name,
+ label: group.title,
+});
+
+const additionalOptions = computedAsync(async () => {
+ const options: ResourceSelectOption<Group>[] = [];
let groupNames: string[] = [];
- if (props.group) {
- groupNames = [props.group];
- } else if (props.groups) {
- groupNames = props.groups;
+ if (Array.isArray(props.value)) {
+ groupNames = props.value;
+ } else if (props.value) {
+ groupNames = [props.value];
}
const groups = await groupStore.batchFetchGroups(groupNames);
for (const group of groups) {
- data.push(group);
+ options.push(getOption(group));
}
- return data;
+ return options;
}, []);
const handleSearch = async (params: {
@@ -78,16 +83,11 @@ const handleSearch = async (params: {
return {
nextPageToken,
- data: groups,
+ options: groups.map(getOption),
};
};
-const getOption = (group: Group) => ({
- value: group.name,
- label: group.title,
-});
-
-const renderLabel = (group: Group, keyword: string) => {
+const customLabel = (group: Group, keyword: string) => {
return (
<GroupNameCell
showEmail={false}
@@ -98,4 +98,21 @@ const renderLabel = (group: Group, keyword: string) => {
/>
);
};
+
+const renderLabel = computed(() => {
+ return getRenderLabelFunc({
+ multiple: props.multiple,
+ customLabel,
+ showResourceName: true,
+ });
+});
+
+const renderTag = computed(() => {
+ return getRenderTagFunc({
+ multiple: props.multiple,
+ disabled: props.disabled,
+ size: props.size,
+ customLabel,
+ });
+});
</script>
diff --git a/frontend/src/components/v2/Select/InstanceSelect.vue b/frontend/src/components/v2/Select/InstanceSelect.vue
index c49d56d7c4..a8024b356c 100644
--- a/frontend/src/components/v2/Select/InstanceSelect.vue
+++ b/frontend/src/components/v2/Select/InstanceSelect.vue
@@ -1,20 +1,24 @@
<template>
<RemoteResourceSelector
v-bind="$attrs"
- :value="instanceName"
- :custom-label="renderLabel"
+ :multiple="false"
+ :disabled="disabled"
+ :size="size"
+ :value="value"
+ :render-label="renderLabel"
+ :render-tag="renderTag"
:consistent-menu-width="false"
class="bb-instance-select"
- :additional-data="additionalData"
+ :additional-options="additionalOptions"
:search="handleSearch"
- :get-option="getOption"
- @update:value="(val) => $emit('update:instance-name', val)"
+ @update:value="(val) => $emit('update:value', val as (string | undefined))"
/>
</template>
<script lang="tsx" setup>
import { computedAsync } from "@vueuse/core";
import { ChevronRightIcon } from "lucide-vue-next";
+import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { EnvironmentV1Name, InstanceV1Name } from "@/components/v2";
import { useEnvironmentV1Store, useInstanceV1Store } from "@/store";
@@ -26,47 +30,59 @@ import {
import { type Engine } from "@/types/proto-es/v1/common_pb";
import type { Instance } from "@/types/proto-es/v1/instance_service_pb";
import { supportedEngineV1List } from "@/utils";
-import RemoteResourceSelector from "./RemoteResourceSelector.vue";
+import RemoteResourceSelector from "./RemoteResourceSelector/index.vue";
+import type {
+ ResourceSelectOption,
+ SelectSize,
+} from "./RemoteResourceSelector/types";
+import {
+ getRenderLabelFunc,
+ getRenderTagFunc,
+} from "./RemoteResourceSelector/utils";
const props = withDefaults(
defineProps<{
- instanceName?: string | undefined;
+ value?: string | undefined;
environmentName?: string;
projectName?: string;
allowedEngineList?: Engine[];
+ disabled?: boolean;
+ size?: SelectSize;
}>(),
{
- instanceName: undefined,
- environmentName: undefined,
allowedEngineList: () => supportedEngineV1List(),
}
);
defineEmits<{
- (event: "update:instance-name", value: string | undefined): void;
+ (event: "update:value", value: string | undefined): void;
}>();
const { t } = useI18n();
const instanceStore = useInstanceV1Store();
const environmentStore = useEnvironmentV1Store();
-const additionalData = computedAsync(async () => {
- const data = [];
- if (props.instanceName === UNKNOWN_INSTANCE_NAME) {
+const getOption = (instance: Instance): ResourceSelectOption<Instance> => ({
+ resource: instance,
+ value: instance.name,
+ label: instance.title,
+});
+
+const additionalOptions = computedAsync(async () => {
+ const options: ResourceSelectOption<Instance>[] = [];
+ if (props.value === UNKNOWN_INSTANCE_NAME) {
const dummyAll = {
...unknownInstance(),
title: t("instance.all"),
};
- data.push(dummyAll);
+ options.push(getOption(dummyAll));
}
- if (isValidInstanceName(props.instanceName)) {
- const instance = await instanceStore.getOrFetchInstanceByName(
- props.instanceName
- );
- data.push(instance);
+ if (isValidInstanceName(props.value)) {
+ const instance = await instanceStore.getOrFetchInstanceByName(props.value);
+ options.push(getOption(instance));
}
- return data;
+ return options;
}, []);
const handleSearch = async (params: {
@@ -87,11 +103,11 @@ const handleSearch = async (params: {
return {
nextPageToken,
- data: instances,
+ options: instances.map(getOption),
};
};
-const renderLabel = (instance: Instance, keyword: string) => {
+const customLabel = (instance: Instance, keyword: string) => {
const isUnknown = instance.name === UNKNOWN_INSTANCE_NAME;
const environment = environmentStore.getEnvironmentByName(
instance.environment ?? ""
@@ -117,9 +133,21 @@ const renderLabel = (instance: Instance, keyword: string) => {
);
};
-const getOption = (instance: Instance) => ({
- value: instance.name,
- label: instance.title,
+const renderLabel = computed(() => {
+ return getRenderLabelFunc({
+ multiple: false,
+ customLabel,
+ showResourceName: true,
+ });
+});
+
+const renderTag = computed(() => {
+ return getRenderTagFunc({
+ multiple: false,
+ disabled: props.disabled,
+ size: props.size,
+ customLabel,
+ });
});
</script>
diff --git a/frontend/src/components/v2/Select/LocalResourceSelector.vue b/frontend/src/components/v2/Select/LocalResourceSelector.vue
deleted file mode 100644
index 94119fabba..0000000000
--- a/frontend/src/components/v2/Select/LocalResourceSelector.vue
+++ /dev/null
@@ -1,150 +0,0 @@
-<template>
- <NSelect
- v-bind="$attrs"
- :filterable="true"
- :clearable="true"
- :remote="false"
- :virtual-scroll="true"
- :multiple="multiple"
- :value="selected"
- :disabled="disabled"
- :options="options"
- :fallback-option="false"
- :filter="filterResource"
- :render-label="renderLabel"
- :render-tag="renderTag"
- :placeholder="placeholder"
- :size="size"
- :consistent-menu-width="consistentMenuWidth"
- class="bb-user-select"
- @update:value="handleValueUpdated"
- >
- <template v-if="$slots.empty" #empty>
- <slot name="empty" />
- </template>
- </NSelect>
-</template>
-
-<script lang="tsx" setup generic="T extends { name: string }">
-import type { SelectOption } from "naive-ui";
-import { NCheckbox, NSelect, NTag } from "naive-ui";
-import type { SelectBaseOption } from "naive-ui/lib/select/src/interface";
-import { computed, type VNodeChild } from "vue";
-import EllipsisText from "@/components/EllipsisText.vue";
-
-type ResourceSelectOption = SelectOption & {
- resource: T;
- value: string;
- label: string;
-};
-
-const props = withDefaults(
- defineProps<{
- placeholder?: string;
- multiple?: boolean;
- disabled?: boolean;
- value?: string | undefined | null;
- values?: string[] | undefined | null;
- consistentMenuWidth?: boolean;
- size?: "tiny" | "small" | "medium" | "large";
- options: ResourceSelectOption[];
- showResourceName?: boolean;
- resourceNameClass?: string;
- customLabel?: (resource: T) => VNodeChild;
- }>(),
- {
- customLabel: undefined,
- size: "medium",
- disabled: false,
- multiple: false,
- showResourceName: true,
- resourceNameClass: "",
- consistentMenuWidth: true,
- }
-);
-
-const emit = defineEmits<{
- (event: "update:value", value: string | undefined): void;
- (event: "update:values", value: string[]): void;
-}>();
-
-const selected = computed(() => {
- if (props.multiple) {
- return props.values || [];
- } else {
- return props.value;
- }
-});
-
-const renderLabel = (option: SelectOption, selected: boolean) => {
- const { resource, label } = option as ResourceSelectOption;
- const node = (
- <div class="py-1">
- {props.customLabel ? props.customLabel(resource) : label}
- {props.showResourceName && (
- <div>
- <EllipsisText
- class={`opacity-60 textinfolabel ${props.resourceNameClass}`}
- >
- {resource.name}
- </EllipsisText>
- </div>
- )}
- </div>
- );
- if (props.multiple) {
- return (
- <div class="flex items-center gap-x-2 py-2">
- <NCheckbox checked={selected} size="small" />
- {node}
- </div>
- );
- }
-
- return node;
-};
-
-const renderTag = ({
- option,
- handleClose,
-}: {
- option: SelectBaseOption;
- handleClose: () => void;
-}) => {
- const { resource, label } = option as ResourceSelectOption;
- const node = props.customLabel ? props.customLabel(resource) : label;
- if (props.multiple) {
- return (
- <NTag size={props.size} closable={!props.disabled} onClose={handleClose}>
- {node}
- </NTag>
- );
- }
- return node;
-};
-
-const filterResource = (pattern: string, option: SelectOption) => {
- const { resource, label } = option as ResourceSelectOption;
- const search = pattern.trim().toLowerCase();
- return (
- resource.name.toLowerCase().includes(search) ||
- (label as string).toLowerCase().includes(search)
- );
-};
-
-const handleValueUpdated = (value: string | string[] | undefined | null) => {
- if (props.multiple) {
- if (!value) {
- // normalize value
- value = [];
- }
- emit("update:values", value as string[]);
- } else {
- if (value === null) {
- // normalize value
- value = "";
- }
- emit("update:value", value as string);
- }
-};
-</script>
diff --git a/frontend/src/components/v2/Select/ProjectSelect.vue b/frontend/src/components/v2/Select/ProjectSelect.vue
index e5f4c0e4e4..c6148b8458 100644
--- a/frontend/src/components/v2/Select/ProjectSelect.vue
+++ b/frontend/src/components/v2/Select/ProjectSelect.vue
@@ -3,16 +3,15 @@
v-bind="$attrs"
:multiple="multiple"
:disabled="disabled"
- :value="projectName"
- :values="projectNames"
- :custom-label="renderLabel"
+ :size="size"
+ :value="value"
+ :render-label="renderLabel"
+ :render-tag="renderTag"
class="bb-project-select"
- :additional-data="additionalData"
+ :additional-options="additionalOptions"
:search="handleSearch"
- :get-option="getOption"
:filter="filterProject"
- @update:value="(val) => $emit('update:project-name', val)"
- @update:values="(val) => $emit('update:project-names', val)"
+ @update:value="(val) => $emit('update:value', val)"
>
<template v-if="$slots.empty" #empty>
<slot name="empty" />
@@ -29,42 +28,41 @@ import { ProjectNameCell } from "@/components/v2/Model/cells";
import { usePermissionStore, useProjectV1Store } from "@/store";
import {
DEFAULT_PROJECT_NAME,
- isValidProjectName,
UNKNOWN_PROJECT_NAME,
unknownProject,
} from "@/types";
import type { Project } from "@/types/proto-es/v1/project_service_pb";
import { hasWorkspacePermissionV2 } from "@/utils";
-import RemoteResourceSelector from "./RemoteResourceSelector.vue";
+import RemoteResourceSelector from "./RemoteResourceSelector/index.vue";
+import type {
+ ResourceSelectOption,
+ SelectSize,
+} from "./RemoteResourceSelector/types";
+import {
+ getRenderLabelFunc,
+ getRenderTagFunc,
+} from "./RemoteResourceSelector/utils";
const props = withDefaults(
defineProps<{
disabled?: boolean;
- projectName?: string | undefined | null; // UNKNOWN_PROJECT_NAME to "ALL"
- projectNames?: string[] | undefined | null;
+ value?: string[] | string | undefined; // UNKNOWN_PROJECT_NAME to "ALL"
allowedProjectRoleList?: string[]; // Empty array([]) to "ALL"
includeAll?: boolean;
includeDefaultProject?: boolean;
multiple?: boolean;
+ size?: SelectSize;
renderSuffix?: (project: string) => string;
filter?: (project: Project) => boolean;
}>(),
{
- disabled: false,
- projectName: undefined,
- projectNames: undefined,
allowedProjectRoleList: () => [],
- includeAll: false,
- includeDefaultProject: false,
- multiple: false,
- filter: () => true,
renderSuffix: () => "",
}
);
defineEmits<{
- (event: "update:project-name", name: string | undefined): void;
- (event: "update:project-names", names: string[]): void;
+ (event: "update:value", name: string[] | string | undefined): void;
}>();
const { t } = useI18n();
@@ -93,32 +91,37 @@ const filterProject = (project: Project) => {
return true;
};
-const additionalData = computedAsync(async () => {
- const data = [];
+const getOption = (project: Project): ResourceSelectOption<Project> => ({
+ resource: project,
+ value: project.name,
+ label:
+ project.name === DEFAULT_PROJECT_NAME
+ ? t("common.unassigned")
+ : project.title,
+});
+
+const additionalOptions = computedAsync(async () => {
+ const options: ResourceSelectOption<Project>[] = [];
- if (props.projectName === UNKNOWN_PROJECT_NAME || props.includeAll) {
+ let projectNames: string[] = [];
+ if (Array.isArray(props.value)) {
+ projectNames = props.value;
+ } else if (props.value) {
+ projectNames = [props.value];
+ }
+
+ if (projectNames.includes(UNKNOWN_PROJECT_NAME) || props.includeAll) {
const dummyAll = {
...unknownProject(),
title: t("project.all"),
};
- data.unshift(dummyAll);
+ options.unshift(getOption(dummyAll));
}
- let projectNames: string[] = [];
- if (props.projectName) {
- projectNames = [props.projectName];
- } else if (props.projectNames) {
- projectNames = props.projectNames;
- }
+ const projects = await projectStore.batchGetProjects(projectNames);
+ options.push(...projects.map(getOption));
- for (const projectName of projectNames) {
- if (isValidProjectName(projectName)) {
- const project = await projectStore.getOrFetchProjectByName(projectName);
- data.push(project);
- }
- }
-
- return data;
+ return options;
}, []);
const handleSearch = async (params: {
@@ -137,21 +140,11 @@ const handleSearch = async (params: {
return {
nextPageToken,
- data: projects,
+ options: projects.map(getOption),
};
};
-const getOption = (project: Project) => ({
- value: project.name,
- label:
- project.name === DEFAULT_PROJECT_NAME
- ? t("common.unassigned")
- : project.name === UNKNOWN_PROJECT_NAME
- ? t("project.all")
- : project.title,
-});
-
-const renderLabel = (project: Project, keyword: string) => {
+const customLabel = (project: Project, keyword: string) => {
if (!project) return null;
return (
<ProjectNameCell
@@ -168,4 +161,21 @@ const renderLabel = (project: Project, keyword: string) => {
</ProjectNameCell>
);
};
+
+const renderLabel = computed(() => {
+ return getRenderLabelFunc({
+ multiple: props.multiple,
+ customLabel,
+ showResourceName: true,
+ });
+});
+
+const renderTag = computed(() => {
+ return getRenderTagFunc({
+ multiple: props.multiple,
+ disabled: props.disabled,
+ size: props.size,
+ customLabel,
+ });
+});
</script>
diff --git a/frontend/src/components/v2/Select/RemoteResourceSelector.vue b/frontend/src/components/v2/Select/RemoteResourceSelector.vue
deleted file mode 100644
index ba19469f1e..0000000000
--- a/frontend/src/components/v2/Select/RemoteResourceSelector.vue
+++ /dev/null
@@ -1,257 +0,0 @@
-<template>
- <NSelect
- v-bind="$attrs"
- v-model:show="show"
- :loading="isLoadingNextPage"
- :filterable="true"
- :clearable="true"
- :remote="true"
- :virtual-scroll="true"
- :multiple="multiple"
- :value="selected"
- :disabled="disabled"
- :options="options"
- :fallback-option="false"
- :render-label="renderLabel"
- :render-tag="renderTag"
- :placeholder="placeholder ?? $t('common.search-for-more')"
- :size="size"
- :consistent-menu-width="consistentMenuWidth"
- :reset-menu-on-options-change="false"
- @search="handleSearch"
- @click="onSelectorOpen"
- @update:value="handleValueUpdated"
- >
- <template #empty>
- <slot name="empty">
- <BBSpin v-if="isLoadingNextPage" />
- </slot>
- </template>
- <template #action>
- <NButton
- v-if="pageToken"
- class="w-full!"
- quaternary
- :loading="isLoadingNextPage"
- @click="() => onNextPage(true)">
- {{ $t("common.load-more") }}
- </NButton>
- </template>
- </NSelect>
-</template>
-
-<script lang="tsx" setup generic="T extends { name: string }">
-import { useDebounceFn } from "@vueuse/core";
-import type { SelectOption } from "naive-ui";
-import { NButton, NCheckbox, NSelect, NTag } from "naive-ui";
-import type { SelectBaseOption } from "naive-ui/lib/select/src/interface";
-import { computed, nextTick, type Ref, ref, type VNodeChild, watch } from "vue";
-import { BBSpin } from "@/bbkit";
-import EllipsisText from "@/components/EllipsisText.vue";
-import { HighlightLabelText } from "@/components/v2";
-import { DEBOUNCE_SEARCH_DELAY } from "@/types";
-import { getDefaultPagination } from "@/utils";
-
-type ResourceSelectOption = SelectOption & {
- resource: T;
- value: string;
- label: string;
-};
-
-const props = withDefaults(
- defineProps<{
- placeholder?: string;
- multiple?: boolean;
- disabled?: boolean;
- value?: string | undefined | null;
- values?: string[] | undefined | null;
- consistentMenuWidth?: boolean;
- size?: "tiny" | "small" | "medium" | "large";
- showResourceName?: boolean;
- resourceNameClass?: string;
- additionalData?: T[];
- customLabel?: (resource: T, keyword: string) => VNodeChild;
- filter?: (resource: T) => boolean;
- search?: (params: {
- search: string;
- pageToken: string;
- pageSize: number;
- }) => Promise<{ nextPageToken: string | undefined; data: T[] }>;
- getOption: (resource: T) => { value: string; label: string };
- }>(),
- {
- customLabel: undefined,
- size: "medium",
- fallbackOption: false,
- disabled: false,
- multiple: false,
- showResourceName: true,
- resourceNameClass: "",
- consistentMenuWidth: true,
- additionalData: () => [],
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- filter: (resource: T) => true,
- }
-);
-
-const emit = defineEmits<{
- (event: "update:value", value: string | undefined): void;
- (event: "update:values", value: string[]): void;
-}>();
-
-const isLoadingNextPage = ref(false);
-const pageToken = ref<string | undefined>("");
-const searchText = ref("");
-const show = ref(false);
-const rawList = ref([]) as Ref<T[]>;
-
-const appendDataToRawList = (data: T[]) => {
- for (const item of data) {
- if (!rawList.value.find((raw) => raw.name === item.name)) {
- rawList.value.push(item);
- }
- }
-};
-
-watch(
- [
- () => props.additionalData,
- () => isLoadingNextPage.value,
- () => searchText.value,
- ],
- ([additionalData, isLoadingNextPage, searchText]) => {
- if (!isLoadingNextPage && !searchText) {
- appendDataToRawList(additionalData);
- }
- },
- { deep: true }
-);
-
-const onNextPage = async (openOptions: boolean) => {
- if (!props.search) {
- return;
- }
- isLoadingNextPage.value = true;
- const { nextPageToken, data } = await props.search({
- pageToken: pageToken.value ?? "",
- pageSize: getDefaultPagination(),
- search: searchText.value,
- });
-
- if (!pageToken.value) {
- rawList.value = data;
- } else {
- appendDataToRawList(data);
- }
-
- pageToken.value = nextPageToken;
- isLoadingNextPage.value = false;
-
- if (openOptions) {
- nextTick(() => (show.value = true));
- }
-};
-
-const selected = computed(() => {
- if (props.multiple) {
- return props.values || [];
- } else {
- return props.value;
- }
-});
-
-const onSelectorOpen = async () => {
- if (props.disabled) {
- return;
- }
- await handleSearch("");
-};
-
-const handleSearch = useDebounceFn(
- async (search: string, openOptions: boolean = true) => {
- searchText.value = search.trim().toLowerCase();
- pageToken.value = "";
- await onNextPage(openOptions);
- },
- DEBOUNCE_SEARCH_DELAY
-);
-
-const renderLabel = (option: SelectOption, selected: boolean) => {
- const { resource, label } = option as ResourceSelectOption;
- const node = (
- <div class="py-1">
- {props.customLabel ? (
- props.customLabel(resource, searchText.value)
- ) : (
- <HighlightLabelText keyword={searchText.value} text={label} />
- )}
- {props.showResourceName && (
- <div>
- <EllipsisText
- class={`opacity-60 textinfolabel ${props.resourceNameClass}`}
- >
- <HighlightLabelText
- keyword={searchText.value}
- text={resource.name}
- />
- </EllipsisText>
- </div>
- )}
- </div>
- );
- if (props.multiple) {
- return (
- <div class="flex items-center gap-x-2 py-2">
- <NCheckbox checked={selected} size="small" />
- {node}
- </div>
- );
- }
-
- return node;
-};
-
-const renderTag = ({
- option,
- handleClose,
-}: {
- option: SelectBaseOption;
- handleClose: () => void;
-}) => {
- const { resource, label } = option as ResourceSelectOption;
- const node = props.customLabel ? props.customLabel(resource, "") : label;
- if (props.multiple) {
- return (
- <NTag size={props.size} closable={!props.disabled} onClose={handleClose}>
- {node}
- </NTag>
- );
- }
- return node;
-};
-
-const handleValueUpdated = (value: string | string[] | undefined | null) => {
- if (props.multiple) {
- if (!value) {
- // normalize value
- value = [];
- }
- emit("update:values", value as string[]);
- } else {
- if (value === null) {
- // normalize value
- value = "";
- }
- emit("update:value", value as string);
- }
-};
-
-const options = computed((): ResourceSelectOption[] => {
- return rawList.value.filter(props.filter).map((data) => {
- return {
- resource: data,
- ...props.getOption(data),
- };
- });
-});
-</script>
diff --git a/frontend/src/components/v2/Select/RemoteResourceSelector/index.vue b/frontend/src/components/v2/Select/RemoteResourceSelector/index.vue
new file mode 100644
index 0000000000..d674a89cfc
--- /dev/null
+++ b/frontend/src/components/v2/Select/RemoteResourceSelector/index.vue
@@ -0,0 +1,181 @@
+<template>
+ <NSelect
+ v-bind="$attrs"
+ v-model:show="show"
+ :loading="isLoadingNextPage"
+ :filterable="true"
+ :clearable="true"
+ :remote="true"
+ :virtual-scroll="true"
+ :multiple="multiple"
+ :value="value"
+ :disabled="disabled"
+ :options="options"
+ :fallback-option="fallbackOption"
+ :render-label="customLabel"
+ :render-tag="renderTag"
+ :placeholder="placeholder ?? $t('common.search-for-more')"
+ :size="size"
+ :consistent-menu-width="consistentMenuWidth"
+ :reset-menu-on-options-change="false"
+ @search="handleSearch"
+ @click="onSelectorOpen"
+ @update:value="$emit('update:value', $event)"
+ >
+ <template #empty>
+ <slot name="empty">
+ <BBSpin v-if="isLoadingNextPage" />
+ </slot>
+ </template>
+ <template #action>
+ <NButton
+ v-if="pageToken"
+ class="w-full!"
+ quaternary
+ :loading="isLoadingNextPage"
+ @click="() => onNextPage(true)">
+ {{ $t("common.load-more") }}
+ </NButton>
+ </template>
+ </NSelect>
+</template>
+
+<script lang="tsx" setup generic="T">
+import { useDebounceFn } from "@vueuse/core";
+import type { SelectOption } from "naive-ui";
+import { NButton, NSelect } from "naive-ui";
+import type { SelectBaseOption } from "naive-ui/lib/select/src/interface";
+import { computed, nextTick, type Ref, ref, type VNodeChild, watch } from "vue";
+import { BBSpin } from "@/bbkit";
+import { DEBOUNCE_SEARCH_DELAY } from "@/types";
+import { getDefaultPagination } from "@/utils";
+import type { ResourceSelectOption, SelectSize } from "./types";
+
+const props = withDefaults(
+ defineProps<{
+ placeholder?: string;
+ multiple?: boolean;
+ disabled?: boolean;
+ value?: string[] | string | undefined;
+ consistentMenuWidth?: boolean;
+ size?: SelectSize;
+ additionalOptions?: ResourceSelectOption<T>[];
+ renderLabel?: (
+ option: SelectOption,
+ selected: boolean,
+ searchText: string
+ ) => VNodeChild;
+ renderTag?: (props: {
+ option: SelectBaseOption;
+ handleClose: () => void;
+ }) => VNodeChild;
+ filter?: (resource: T) => boolean;
+ search?: (params: {
+ search: string;
+ pageToken: string;
+ pageSize: number;
+ }) => Promise<{
+ nextPageToken: string | undefined;
+ options: ResourceSelectOption<T>[];
+ }>;
+ fallbackOption?: false | ((value: string) => ResourceSelectOption<T>);
+ }>(),
+ {
+ fallbackOption: false,
+ consistentMenuWidth: true,
+ additionalOptions: () => [],
+ }
+);
+
+const emit = defineEmits<{
+ (event: "update:value", value: string[] | string | undefined): void;
+ (event: "open"): void;
+}>();
+
+const isLoadingNextPage = ref(false);
+const pageToken = ref<string | undefined>("");
+const searchText = ref("");
+const show = ref(false);
+const rawOptions = ref([]) as Ref<ResourceSelectOption<T>[]>;
+
+const appendDataToRawList = (data: ResourceSelectOption<T>[]) => {
+ for (const item of data) {
+ if (!rawOptions.value.find((raw) => raw.value === item.value)) {
+ rawOptions.value.push(item);
+ }
+ }
+};
+
+watch(
+ [
+ () => props.additionalOptions,
+ () => isLoadingNextPage.value,
+ () => searchText.value,
+ ],
+ ([additionalOptions, isLoadingNextPage, searchText]) => {
+ if (!isLoadingNextPage && !searchText) {
+ appendDataToRawList(additionalOptions);
+ }
+ },
+ { deep: true }
+);
+
+const onNextPage = async (openOptions: boolean) => {
+ if (!props.search) {
+ return;
+ }
+ isLoadingNextPage.value = true;
+ const { nextPageToken, options } = await props.search({
+ pageToken: pageToken.value ?? "",
+ pageSize: getDefaultPagination(),
+ search: searchText.value,
+ });
+
+ if (!pageToken.value) {
+ rawOptions.value = options;
+ } else {
+ appendDataToRawList(options);
+ }
+
+ pageToken.value = nextPageToken;
+ isLoadingNextPage.value = false;
+
+ if (openOptions) {
+ nextTick(() => (show.value = true));
+ }
+};
+
+const onSelectorOpen = async () => {
+ if (props.disabled) {
+ return;
+ }
+ await handleSearch("");
+ emit("open");
+};
+
+const handleSearch = useDebounceFn(
+ async (search: string, openOptions: boolean = true) => {
+ searchText.value = search.trim().toLowerCase();
+ pageToken.value = "";
+ await onNextPage(openOptions);
+ },
+ DEBOUNCE_SEARCH_DELAY
+);
+
+const customLabel = computed(() => {
+ const renderLabel = props.renderLabel;
+ if (renderLabel) {
+ return (option: SelectOption, selected: boolean) =>
+ renderLabel(option, selected, searchText.value);
+ }
+ return undefined;
+});
+
+const options = computed((): ResourceSelectOption<T>[] => {
+ const filter = props.filter;
+ if (!filter) {
+ return rawOptions.value;
+ }
+ return rawOptions.value.filter((raw) => raw.resource && filter(raw.resource));
+});
+</script>
diff --git a/frontend/src/components/v2/Select/RemoteResourceSelector/types.ts b/frontend/src/components/v2/Select/RemoteResourceSelector/types.ts
new file mode 100644
index 0000000000..054841b9e1
--- /dev/null
+++ b/frontend/src/components/v2/Select/RemoteResourceSelector/types.ts
@@ -0,0 +1,9 @@
+import type { SelectOption } from "naive-ui";
+
+export type ResourceSelectOption<T> = SelectOption & {
+ resource?: T;
+ value: string;
+ label: string;
+};
+
+export type SelectSize = "tiny" | "small" | "medium" | "large";
diff --git a/frontend/src/components/v2/Select/RemoteResourceSelector/utils.tsx b/frontend/src/components/v2/Select/RemoteResourceSelector/utils.tsx
new file mode 100644
index 0000000000..3e72ca40e9
--- /dev/null
+++ b/frontend/src/components/v2/Select/RemoteResourceSelector/utils.tsx
@@ -0,0 +1,74 @@
+import type { SelectOption } from "naive-ui";
+import { NCheckbox, NTag } from "naive-ui";
+import type { SelectBaseOption } from "naive-ui/lib/select/src/interface";
+import { type VNodeChild } from "vue";
+import EllipsisText from "@/components/EllipsisText.vue";
+import { HighlightLabelText } from "@/components/v2";
+import type { ResourceSelectOption, SelectSize } from "./types";
+
+export const getRenderLabelFunc =
+ <T extends { name: string }>(params: {
+ showResourceName?: boolean;
+ multiple?: boolean;
+ customLabel?: (resource: T, keyword: string) => VNodeChild;
+ }) =>
+ (option: SelectOption, selected: boolean, searchText: string) => {
+ const { resource, label } = option as ResourceSelectOption<T>;
+ const node = (
+ <div class="py-1">
+ {params.customLabel && resource ? (
+ params.customLabel(resource, searchText)
+ ) : (
+ <HighlightLabelText keyword={searchText} text={label} />
+ )}
+ {params.showResourceName && resource && (
+ <div>
+ <EllipsisText class="opacity-60 textinfolabel">
+ <HighlightLabelText keyword={searchText} text={resource.name} />
+ </EllipsisText>
+ </div>
+ )}
+ </div>
+ );
+ if (params.multiple) {
+ return (
+ <div class="flex items-center gap-x-2 py-2">
+ <NCheckbox checked={selected} size="small" />
+ {node}
+ </div>
+ );
+ }
+
+ return node;
+ };
+
+export const getRenderTagFunc =
+ <T,>(params: {
+ multiple?: boolean;
+ size?: SelectSize;
+ disabled?: boolean;
+ customLabel?: (resource: T, keyword: string) => VNodeChild;
+ }) =>
+ ({
+ option,
+ handleClose,
+ }: {
+ option: SelectBaseOption;
+ handleClose: () => void;
+ }) => {
+ const { resource, label } = option as ResourceSelectOption<T>;
+ const node =
+ params.customLabel && resource ? params.customLabel(resource, "") : label;
+ if (params.multiple) {
+ return (
+ <NTag
+ size={params.size}
+ closable={!params.disabled}
+ onClose={handleClose}
+ >
+ {node}
+ </NTag>
+ );
+ }
+ return node;
+ };
diff --git a/frontend/src/components/v2/Select/RoleSelect.vue b/frontend/src/components/v2/Select/RoleSelect.vue
index f98360e866..21b1d68511 100644
--- a/frontend/src/components/v2/Select/RoleSelect.vue
+++ b/frontend/src/components/v2/Select/RoleSelect.vue
@@ -1,7 +1,7 @@
<template>
<NSelect
v-bind="$attrs"
- :value="value ? value : undefined"
+ :value="value"
:multiple="multiple"
:disabled="disabled"
:clearable="clearable"
@@ -44,19 +44,15 @@ const props = withDefaults(
multiple?: boolean;
suffix?: string;
includeWorkspaceRoles?: boolean;
- size?: "tiny" | "small" | "medium" | "large";
supportRoles?: string[];
}>(),
{
clearable: true,
- value: undefined,
- multiple: false,
includeWorkspaceRoles: true,
suffix: () =>
` (${t("common.optional")}, ${t(
"role.project-roles.apply-to-all-projects"
).toLocaleLowerCase()})`,
- size: "medium",
supportRoles: () => [],
}
);
diff --git a/frontend/src/components/v2/Select/UserSelect.vue b/frontend/src/components/v2/Select/UserSelect.vue
index ba82e48e58..c79eef7cfe 100644
--- a/frontend/src/components/v2/Select/UserSelect.vue
+++ b/frontend/src/components/v2/Select/UserSelect.vue
@@ -2,57 +2,52 @@
<RemoteResourceSelector
v-bind="$attrs"
:multiple="multiple"
- :value="user"
- :values="users"
- :additional-data="additionalData"
- :custom-label="renderLabel"
- :show-resource-name="false"
+ :disabled="disabled"
+ :size="size"
+ :value="value"
+ :additional-options="additionalOptions"
+ :render-label="renderLabel"
+ :render-tag="renderTag"
:search="handleSearch"
- :get-option="getOption"
:filter="filter"
- @update:value="(val) => $emit('update:user', val)"
- @update:values="(val) => $emit('update:users', val)"
+ @update:value="(val) => $emit('update:value', val)"
/>
</template>
<script lang="tsx" setup>
import { computedAsync } from "@vueuse/core";
+import { computed } from "vue";
import { HighlightLabelText } from "@/components/v2";
import { UserNameCell } from "@/components/v2/Model/cells";
import { type UserFilter, useUserStore } from "@/store";
-import { allUsersUser, isValidUserName } from "@/types";
+import { allUsersUser } from "@/types";
import { type User, UserType } from "@/types/proto-es/v1/user_service_pb";
-import { ensureUserFullName } from "@/utils";
-import RemoteResourceSelector from "./RemoteResourceSelector.vue";
+import RemoteResourceSelector from "./RemoteResourceSelector/index.vue";
+import type {
+ ResourceSelectOption,
+ SelectSize,
+} from "./RemoteResourceSelector/types";
+import {
+ getRenderLabelFunc,
+ getRenderTagFunc,
+} from "./RemoteResourceSelector/utils";
-const props = withDefaults(
- defineProps<{
- multiple?: boolean;
- user?: string;
- users?: string[];
- projectName?: string;
- // allUsers is a special user that represents all users in the project.
- includeAllUsers?: boolean;
- includeSystemBot?: boolean;
- includeServiceAccount?: boolean;
- includeWorkloadIdentity?: boolean;
- filter?: (user: User) => boolean;
- }>(),
- {
- multiple: false,
- user: undefined,
- users: undefined,
- projectName: undefined,
- includeAllUsers: false,
- includeSystemBot: false,
- includeServiceAccount: false,
- includeWorkloadIdentity: false,
- }
-);
+const props = defineProps<{
+ multiple?: boolean;
+ disabled?: boolean;
+ size?: SelectSize;
+ value?: string | string[] | undefined;
+ projectName?: string;
+ // allUsers is a special user that represents all users in the project.
+ includeAllUsers?: boolean;
+ includeSystemBot?: boolean;
+ includeServiceAccount?: boolean;
+ includeWorkloadIdentity?: boolean;
+ filter?: (user: User) => boolean;
+}>();
defineEmits<{
- (event: "update:user", value: string | undefined): void;
- (event: "update:users", value: string[]): void;
+ (event: "update:value", value: string[] | string | undefined): void;
}>();
const userStore = useUserStore();
@@ -80,29 +75,29 @@ const getFilter = (search: string): UserFilter => {
};
};
-const additionalData = computedAsync(async () => {
- const data = [];
+const getOption = (user: User): ResourceSelectOption<User> => ({
+ resource: user,
+ value: user.email,
+ label: user.title,
+});
+
+const additionalOptions = computedAsync(async () => {
+ const options: ResourceSelectOption<User>[] = [];
if (props.includeAllUsers) {
- data.unshift(allUsersUser());
+ options.unshift(getOption(allUsersUser()));
}
let userNames: string[] = [];
- if (props.user) {
- userNames = [props.user];
- } else if (props.users) {
- userNames = props.users;
+ if (Array.isArray(props.value)) {
+ userNames = props.value;
+ } else if (props.value) {
+ userNames = [props.value];
}
- for (const email of userNames) {
- if (!email) continue;
- const userName = ensureUserFullName(email);
- if (isValidUserName(userName)) {
- const user = await userStore.getOrFetchUserByIdentifier(userName);
- data.push(user);
- }
- }
+ const users = await userStore.batchGetUsers(userNames);
+ options.push(...users.map(getOption));
- return data;
+ return options;
}, []);
const handleSearch = async (params: {
@@ -117,11 +112,11 @@ const handleSearch = async (params: {
});
return {
nextPageToken,
- data: users,
+ options: users.map(getOption),
};
};
-const renderLabel = (user: User, keyword: string) => {
+const customLabel = (user: User, keyword: string) => {
return (
<UserNameCell
user={user}
@@ -143,8 +138,20 @@ const renderLabel = (user: User, keyword: string) => {
);
};
-const getOption = (user: User) => ({
- value: user.email,
- label: user.title,
+const renderLabel = computed(() => {
+ return getRenderLabelFunc({
+ multiple: props.multiple,
+ customLabel,
+ showResourceName: false,
+ });
+});
+
+const renderTag = computed(() => {
+ return getRenderTagFunc({
+ multiple: props.multiple,
+ disabled: props.disabled,
+ size: props.size,
+ customLabel,
+ });
});
</script>
diff --git a/frontend/src/components/v2/Select/index.ts b/frontend/src/components/v2/Select/index.ts
index c2d7f95ea3..660393d460 100644
--- a/frontend/src/components/v2/Select/index.ts
+++ b/frontend/src/components/v2/Select/index.ts
@@ -1,5 +1,4 @@
import AnnouncementLevelSelect from "./AnnouncementLevelSelect.vue";
-import DatabaseGroupSelect from "./DatabaseGroupSelect.vue";
import DatabaseSelect from "./DatabaseSelect.vue";
import EnvironmentSelect from "./EnvironmentSelect.vue";
import GroupSelect from "./GroupSelect.vue";
@@ -12,7 +11,6 @@ export {
AnnouncementLevelSelect,
InstanceSelect,
DatabaseSelect,
- DatabaseGroupSelect,
EnvironmentSelect,
ProjectSelect,
UserSelect,
diff --git a/frontend/src/views/project/ProjectMaskingExemption.vue b/frontend/src/views/project/ProjectMaskingExemption.vue
index b105fbf9d0..8fdc4b0516 100644
--- a/frontend/src/views/project/ProjectMaskingExemption.vue
+++ b/frontend/src/views/project/ProjectMaskingExemption.vue
@@ -8,7 +8,7 @@
:placeholder="$t('database.select')"
:project-name="project.name"
:show-instance="false"
- v-model:database-name="state.selectedDatabaseName"
+ v-model:value="state.selectedDatabaseName"
/>
<div class="flex-1 flex flex-row items-center justify-end gap-x-2">
diff --git a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
index 6ab6585db5..5d9e11fef7 100644
--- a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
@@ -19,12 +19,12 @@
style="width: 100%"
class="project-select"
:menu-props="{ class: 'project-select-menu' }"
- :project-name="project"
+ :value="project"
:include-all="false"
:clearable="false"
:include-default-project="allowAccessDefaultProject"
:loading="!projectContextReady"
- @update:project-name="handleSwitchProject"
+ @update:value="handleSwitchProject($event as (string | undefined))"
>
<template #empty>
<div class="text-sm text-control-placeholder flex flex-col gap-1">