Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: unify environment select (#20407)
ecmadaob3f9bfa031d6
diff --git a/frontend/src/react/components/EnvironmentMultiSelect.tsx b/frontend/src/react/components/EnvironmentMultiSelect.tsxdeleted file mode 100644index ef7806b508..0000000000--- a/frontend/src/react/components/EnvironmentMultiSelect.tsx+++ /dev/null@@ -1,124 +0,0 @@-import { Check, ChevronDown, X } from "lucide-react";-import { useRef, useState } from "react";-import { useTranslation } from "react-i18next";-import { EnvironmentLabel } from "@/react/components/EnvironmentLabel";-import {- Popover,- PopoverContent,- PopoverTrigger,-} from "@/react/components/ui/popover";-import { useEnvironmentList } from "@/react/hooks/useAppState";-import { cn } from "@/react/lib/utils";--export function EnvironmentMultiSelect({- value,- onChange,-}: Readonly<{- value: string[];- onChange: (envs: string[]) => void;-}>) {- const { t } = useTranslation();- const environmentList = useEnvironmentList();- const [open, setOpen] = useState(false);- const containerRef = useRef<HTMLDivElement>(null);-- const toggle = (name: string) => {- onChange(- value.includes(name) ? value.filter((v) => v !== name) : [...value, name]- );- };-- const remove = (name: string) => {- onChange(value.filter((v) => v !== name));- };-- // Use native <button> elements for the trigger and option rows so they- // get keyboard (Enter/Space) and focus semantics for free — SonarCloud's- // S6848 / S1082 a11y rules require interactive content to be native.- // The selected-chip X buttons are rendered as siblings of the trigger- // (not children) to avoid nested <button>, which is invalid HTML.- return (- <Popover open={open} onOpenChange={setOpen}>- <div ref={containerRef} className="relative">- <div- className={cn(- "flex items-center flex-wrap gap-1 min-h-[2.25rem] w-full rounded-xs border border-control-border bg-transparent px-2 py-1 text-sm",- open && "border-accent"- )}- >- {value.length === 0 && (- <span className="text-control-placeholder">- {t("environment.select")}- </span>- )}- {value.map((name) => (- <span- key={name}- className="inline-flex items-center gap-x-1 rounded-xs border border-control-border px-1 py-0.5 text-xs"- >- <EnvironmentLabel environmentName={name} className="text-xs" />- <button- type="button"- className="text-control-light hover:text-control"- onClick={() => remove(name)}- aria-label={t("common.delete")}- >- <X className="h-3 w-3" />- </button>- </span>- ))}- <PopoverTrigger- render={- <button- type="button"- className="ml-auto flex items-center text-control-light cursor-pointer"- aria-label={t("environment.select")}- />- }- >- <ChevronDown className="h-4 w-4 shrink-0" />- </PopoverTrigger>- </div>- </div>- <PopoverContent- anchor={containerRef}- side="bottom"- align="start"- initialFocus={false}- finalFocus={false}- style={{ width: "var(--anchor-width)" }}- className="max-h-60 overflow-auto p-0"- >- {environmentList.length === 0 && (- <div className="px-3 py-2 text-sm text-control-light">- {t("common.no-data")}- </div>- )}- {environmentList.map((env) => {- const selected = value.includes(env.name);- return (- <button- key={env.name}- type="button"- className="flex w-full cursor-pointer items-center gap-x-2 px-3 py-1.5 text-left text-sm hover:bg-control-bg"- onClick={() => toggle(env.name)}- aria-pressed={selected}- >- <div- className={cn(- "flex size-4 shrink-0 items-center justify-center rounded-xs border",- selected- ? "border-accent bg-accent text-accent-text"- : "border-control-border"- )}- >- {selected && <Check className="h-3 w-3" />}- </div>- <EnvironmentLabel environment={env} />- </button>- );- })}- </PopoverContent>- </Popover>- );-}diff --git a/frontend/src/react/components/EnvironmentSelect.tsx b/frontend/src/react/components/EnvironmentSelect.tsxindex adbe92b831..cffac597df 100644--- a/frontend/src/react/components/EnvironmentSelect.tsx+++ b/frontend/src/react/components/EnvironmentSelect.tsx@@ -6,29 +6,32 @@ import { useEnvironmentList } from "@/react/hooks/useAppState";import { formatEnvironmentName } from "@/types";import type { Environment } from "@/types/v1/environment";-export interface EnvironmentSelectProps {+interface SingleProps {+ multiple?: false;value: string;onChange: (value: string) => void;+ /** Extra content rendered next to the selected env in the trigger and rows. */+ renderSuffix?: (environment: Environment) => React.ReactNode;+}++interface MultiProps {+ multiple: true;+ value: string[];+ onChange: (values: string[]) => void;+}++export type EnvironmentSelectProps = (SingleProps | MultiProps) & {placeholder?: string;disabled?: boolean;className?: string;portal?: boolean;clearable?: boolean;- renderSuffix?: (environment: Environment) => React.ReactNode;-}+};-export function EnvironmentSelect({- value,- onChange,- placeholder,- disabled,- className,- portal,- clearable = true,- renderSuffix,-}: EnvironmentSelectProps) {+export function EnvironmentSelect(props: EnvironmentSelectProps) {const { t } = useTranslation();const environments = useEnvironmentList();+ const renderSuffix = !props.multiple ? props.renderSuffix : undefined;const options = useMemo(() =>@@ -50,15 +53,35 @@ export function EnvironmentSelect({[environments, renderSuffix]);+ const placeholder = props.placeholder ?? t("environment.select");+ const clearable = props.clearable ?? true;++ if (props.multiple) {+ return (+ <Combobox+ multiple+ value={props.value}+ onChange={props.onChange}+ placeholder={placeholder}+ noResultsText={t("common.no-data")}+ disabled={props.disabled}+ className={props.className}+ portal={props.portal}+ clearable={clearable}+ options={options}+ />+ );+ }+return (<Combobox- value={value}- onChange={onChange}- placeholder={placeholder ?? t("environment.select")}+ value={props.value}+ onChange={props.onChange}+ placeholder={placeholder}noResultsText={t("common.no-data")}- disabled={disabled}- className={className}- portal={portal}+ disabled={props.disabled}+ className={props.className}+ portal={props.portal}clearable={clearable}renderValue={(opt) => {const env = environments.find(diff --git a/frontend/src/react/pages/settings/MembersPage.tsx b/frontend/src/react/pages/settings/MembersPage.tsxindex aa00cfd56a..a2b2dfa4c3 100644--- a/frontend/src/react/pages/settings/MembersPage.tsx+++ b/frontend/src/react/pages/settings/MembersPage.tsx@@ -21,7 +21,7 @@ import { useTranslation } from "react-i18next";import { v4 as uuidv4 } from "uuid";import { AccountMultiSelect } from "@/react/components/AccountMultiSelect";import { DatabaseResourceSelector as DatabaseResourceSelectorComponent } from "@/react/components/DatabaseResourceSelector";-import { EnvironmentMultiSelect } from "@/react/components/EnvironmentMultiSelect";+import { EnvironmentSelect } from "@/react/components/EnvironmentSelect";import { FeatureBadge } from "@/react/components/FeatureBadge";import { LearnMoreLink } from "@/react/components/LearnMoreLink";import { PermissionGuard } from "@/react/components/PermissionGuard";@@ -1120,7 +1120,9 @@ function ProjectRoleBindingForm({{t("common.environments")}</label><DDLWarningCallout type="drawer" kind={envKind} />- <EnvironmentMultiSelect+ <EnvironmentSelect+ multiple+ portalvalue={form.environments}onChange={(envs) => onChange({ ...form, environments: envs })}/>diff --git a/frontend/src/react/pages/settings/RequestRoleSheet.test.tsx b/frontend/src/react/pages/settings/RequestRoleSheet.test.tsxindex b4a81f3ff4..d6e5e5b498 100644--- a/frontend/src/react/pages/settings/RequestRoleSheet.test.tsx+++ b/frontend/src/react/pages/settings/RequestRoleSheet.test.tsx@@ -13,12 +13,12 @@ import type { Permission } from "@/types";).IS_REACT_ACT_ENVIRONMENT = true;// ----------------------------------------------------------------------------// Stub EnvironmentMultiSelect — the real component mounts Pinia-backed+// Stub EnvironmentSelect — the real component mounts Pinia-backed// environment state that's not worth wiring up for these tests.// ----------------------------------------------------------------------------vi.mock("@/react/components/EnvironmentMultiSelect", () => ({- EnvironmentMultiSelect: () =>+vi.mock("@/react/components/EnvironmentSelect", () => ({+ EnvironmentSelect: () =>createElement("div", { "data-testid": "env-multi-select" }),}));diff --git a/frontend/src/react/pages/settings/RequestRoleSheet.tsx b/frontend/src/react/pages/settings/RequestRoleSheet.tsxindex 8140f886ad..99b95674ab 100644--- a/frontend/src/react/pages/settings/RequestRoleSheet.tsx+++ b/frontend/src/react/pages/settings/RequestRoleSheet.tsx@@ -12,7 +12,7 @@ import {wrapAsGroup,} from "@/plugins/cel";import { DatabaseResourceSelector as DatabaseResourceSelectorComponent } from "@/react/components/DatabaseResourceSelector";-import { EnvironmentMultiSelect } from "@/react/components/EnvironmentMultiSelect";+import { EnvironmentSelect } from "@/react/components/EnvironmentSelect";import type { OptionConfig } from "@/react/components/ExprEditor";import { ExprEditor } from "@/react/components/ExprEditor";import { IssueLabelSelect } from "@/react/components/IssueLabelSelect";@@ -561,7 +561,9 @@ function RequestRoleForm({{t("common.environments")}</label><DDLWarningCallout type="drawer" kind={envKind} />- <EnvironmentMultiSelect+ <EnvironmentSelect+ multiple+ portalvalue={environments}onChange={setEnvironments}/>