Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { FeatureBadge } from "@/components/FeatureBadge";
import { HighlightLabelText } from "@/components/HighlightLabelText";
import { Combobox, type ComboboxGroup } from "@/components/ui/combobox";
import {
displayRoleDescriptionFromList,
displayRoleTitleFromList,
} from "@/lib/role";
import { useAppStore } from "@/stores/app";
import {
PRESET_PROJECT_ROLES,
PRESET_ROLES,
PRESET_WORKSPACE_ROLES,
} from "@/types/iam";
import type { Role } from "@/types/proto-es/v1/role_service_pb";
import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";
import { displayRoleDescription, displayRoleTitle } from "@/utils";
interface RoleSelectProps {
value: string[];
onChange: (roles: string[]) => void;
disabled?: boolean;
scope?: "project";
multiple?: boolean;
placeholder?: string;
filterRole?: (role: Role) => boolean;
}
export function RoleSelect({
value,
onChange,
disabled,
scope,
multiple = true,
placeholder,
filterRole,
}: RoleSelectProps) {
const { t } = useTranslation();
const roleList = useAppStore((state) => state.roleList);
const hasCustomRoleFeature = useAppStore((s) =>
s.hasInstanceFeature(PlanFeature.FEATURE_CUSTOM_ROLES)
);
const groups: ComboboxGroup[] = useMemo(() => {
const isCustomRole = (name: string) => !PRESET_ROLES.includes(name);
const visibleRole = (name: string) => {
if (!filterRole) {
return true;
}
const role = roleList.find((role) => role.name === name);
return role ? filterRole(role) : false;
};
const workspace = PRESET_WORKSPACE_ROLES.filter(visibleRole).map(
(name) => ({
value: name,
label: displayRoleTitle(name),
description: displayRoleDescription(name),
})
);
const project = PRESET_PROJECT_ROLES.filter(visibleRole).map((name) => ({
value: name,
label: displayRoleTitle(name),
description: displayRoleDescription(name),
}));
const custom = roleList
.filter((r) => !PRESET_ROLES.includes(r.name))
.filter((r) => !filterRole || filterRole(r))
.map((r) => ({
value: r.name,
label: displayRoleTitleFromList(r.name, roleList),
description: displayRoleDescriptionFromList(r.name, roleList),
disabled: !hasCustomRoleFeature,
render: (keyword: string) => (
<div className="flex items-center gap-x-1">
<HighlightLabelText
text={displayRoleTitleFromList(r.name, roleList)}
keyword={keyword}
/>
{isCustomRole(r.name) && !hasCustomRoleFeature && (
<FeatureBadge
feature={PlanFeature.FEATURE_CUSTOM_ROLES}
clickable={false}
/>
)}
</div>
),
}));
const result: ComboboxGroup[] = [];
if (scope !== "project" && workspace.length > 0)
result.push({
label: t("role.workspace-roles.self"),
options: workspace,
});
if (project.length > 0)
result.push({ label: t("role.project-roles.self"), options: project });
if (custom.length > 0)
result.push({ label: t("role.custom-roles.self"), options: custom });
return result;
}, [filterRole, roleList, hasCustomRoleFeature, scope, t]);
const defaultPlaceholder = multiple
? t("settings.members.select-role", { count: 2 })
: t("settings.members.assign-role");
if (multiple) {
return (
<Combobox
multiple
value={value}
onChange={onChange}
options={groups}
placeholder={placeholder || defaultPlaceholder}
disabled={disabled}
portal
/>
);
}
return (
<Combobox
value={value[0] ?? ""}
onChange={(v) => onChange(v ? [v] : [])}
options={groups}
placeholder={placeholder || defaultPlaceholder}
disabled={disabled}
portal
/>
);
}