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 { useCallback, useEffect, useMemo, useRef } from "react";
import { useTranslation } from "react-i18next";
import { EngineIcon } from "@/components/EngineIcon";
import { EnvironmentLabel } from "@/components/EnvironmentLabel";
import { HighlightLabelText } from "@/components/HighlightLabelText";
import { Combobox } from "@/components/ui/combobox";
import { usePaginatedSelect } from "@/hooks/usePaginatedSelect";
import { useAppStore } from "@/stores/app";
import type { Engine } from "@/types/proto-es/v1/common_pb";
import type { Instance } from "@/types/proto-es/v1/instance_service_pb";
import { extractInstanceResourceName, getDefaultPagination } from "@/utils";
export interface InstanceSelectProps {
value: string;
onChange: (value: string, instance: Instance | undefined) => void;
placeholder?: string;
disabled?: boolean;
className?: string;
portal?: boolean;
/** Only show instances with these engines */
engines?: Engine[];
}
export function InstanceSelect({
value,
onChange,
placeholder,
disabled,
className,
portal,
engines,
}: InstanceSelectProps) {
const { t } = useTranslation();
// Stabilize engines array to avoid re-fetching on every render
const enginesRef = useRef(engines);
const stableEngines = useMemo(() => {
const prev = enginesRef.current;
if (
prev &&
engines &&
prev.length === engines.length &&
prev.every((e, i) => e === engines[i])
) {
return prev;
}
enginesRef.current = engines;
return engines;
}, [engines]);
const fetchPage = useCallback(
async (query: string, pageToken: string) => {
const result = await useAppStore.getState().fetchInstanceList({
pageSize: getDefaultPagination(),
pageToken,
filter: { query, engines: stableEngines },
});
return {
items: result.instances,
nextPageToken: result.nextPageToken,
};
},
[stableEngines]
);
const {
items: instances,
search,
hasMore,
loadingMore,
loadMore,
} = usePaginatedSelect({ fetchPage });
useEffect(() => {
search("");
}, [search]);
const handleChange = useCallback(
(name: string) => {
const inst = instances.find((i) => i.name === name);
onChange(name, inst);
},
[instances, onChange]
);
return (
<Combobox
value={value}
onChange={handleChange}
placeholder={placeholder ?? t("common.instance")}
noResultsText={t("common.no-data")}
onSearch={search}
hasMore={hasMore}
loadingMore={loadingMore}
onLoadMore={loadMore}
disabled={disabled}
className={className}
portal={portal}
renderValue={(opt) => {
const inst = instances.find((i) => i.name === opt.value);
if (!inst) return opt.label;
return (
<span className="flex items-center gap-1.5 truncate">
<EngineIcon engine={inst.engine} className="h-4 w-4" />
{inst.title}
</span>
);
}}
options={instances.map((inst) => ({
value: inst.name,
label: inst.title,
description: extractInstanceResourceName(inst.name),
render: (keyword) => (
<div className="flex flex-col gap-1">
<div className="flex items-center gap-1.5">
{inst.environment && (
<EnvironmentLabel environmentName={inst.environment} />
)}
<EngineIcon engine={inst.engine} className="h-4 w-4" />
<HighlightLabelText text={inst.title} keyword={keyword} />
</div>
<HighlightLabelText
text={extractInstanceResourceName(inst.name)}
keyword={keyword}
className="text-xs text-control-placeholder"
/>
</div>
),
}))}
/>
);
}