import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { Controller, useFormContext } from "react-hook-form"; import { Select, SelectOption, SelectVariant } from "@patternfly/react-core"; import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation"; import type { Clients } from "@keycloak/keycloak-admin-client/lib/resources/clients"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; type ResourcesPolicySelectProps = { name: string; clientId: string; searchFunction: keyof Pick; variant?: SelectVariant; preSelected?: string; }; type Policies = { id?: string; name?: string; }; export const ResourcesPolicySelect = ({ name, searchFunction, clientId, variant = SelectVariant.typeaheadMulti, preSelected, }: ResourcesPolicySelectProps) => { const { t } = useTranslation("clients"); const adminClient = useAdminClient(); const { control } = useFormContext(); const [items, setItems] = useState([]); const [search, setSearch] = useState(""); const [open, setOpen] = useState(false); useFetch( async () => ( await adminClient.clients[searchFunction]( Object.assign( { id: clientId, first: 0, max: 10 }, search === "" ? null : { name: search } ) ) ).map((p) => ({ id: "_id" in p ? p._id : "id" in p ? p.id : undefined, name: p.name, })), (policies) => setItems(policies), [search] ); const toSelectOptions = () => items.map((p) => ( {p.name} )); return ( ( )} /> ); };