/* eslint-disable */ // @ts-nocheck import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation"; import { KeycloakSelect, SelectVariant } from "../../../shared/keycloak-ui-shared"; import { Button, SelectOption, TextInput } from "../../../shared/@patternfly/react-core"; import { MinusCircleIcon, PlusCircleIcon } from "../../../shared/@patternfly/react-icons"; import { Table, Tbody, Td, Th, Thead, Tr } from "../../../shared/@patternfly/react-table"; import { camelCase } from "lodash-es"; import { useEffect, useMemo, useState } from "react"; import { Controller, useFieldArray, useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { defaultContextAttributes } from "../utils"; import "./key-based-attribute-input.css"; export type AttributeType = { key?: string; name: string; custom?: boolean; values?: { [key: string]: string; }[]; }; type AttributeInputProps = { name: string; selectableValues?: AttributeType[]; resources?: ResourceRepresentation[]; }; type ValueInputProps = { name: string; rowIndex: number; attribute: any; selectableValues?: AttributeType[]; resources?: ResourceRepresentation[]; }; const ValueInput = ({ name, rowIndex, attribute, selectableValues, resources, }: ValueInputProps) => { const { t } = useTranslation(); const { control, register, getValues } = useFormContext(); const [isValueOpenArray, setIsValueOpenArray] = useState([false]); const toggleValueSelect = (rowIndex: number, open: boolean) => { const arr = [...isValueOpenArray]; arr[rowIndex] = open; setIsValueOpenArray(arr); }; const attributeValues = useMemo(() => { let values: AttributeType[] | undefined = []; if (selectableValues) { values = defaultContextAttributes.find( (attr) => attr.key === getValues().context?.[rowIndex]?.key, )?.values; } return values; }, [getValues]); const renderSelectOptionType = () => { const scopeValues = resources?.find( (resource) => resource.name === getValues().resources?.[rowIndex]?.key, )?.scopes; if (attributeValues?.length && !resources) { return attributeValues.map((attr) => ( {attr.name} )); } else if (scopeValues?.length) { return scopeValues.map((scope) => ( {scope.name} )); } }; const getMessageBundleKey = (attributeName: string) => camelCase(attributeName).replace(/\W/g, ""); return ( {resources || attributeValues?.length ? ( ( toggleValueSelect(rowIndex, open)} isOpen={isValueOpenArray[rowIndex]} variant={SelectVariant.typeahead} typeAheadAriaLabel={t("selectOrTypeAKey")} placeholderText={t("selectOrTypeAKey")} selections={field.value} onSelect={(v) => { field.onChange(v); toggleValueSelect(rowIndex, false); }} > {renderSelectOptionType()} )} /> ) : ( )} ); }; export const KeyBasedAttributeInput = ({ name, selectableValues, resources, }: AttributeInputProps) => { const { t } = useTranslation(); const { control, watch } = useFormContext(); const { fields, append, remove } = useFieldArray({ control: control, name, }); const [isKeyOpenArray, setIsKeyOpenArray] = useState([false]); const toggleKeySelect = (rowIndex: number, open: boolean) => { const arr = [...isKeyOpenArray]; arr[rowIndex] = open; setIsKeyOpenArray(arr); }; useEffect(() => { if (!fields.length) { append({ key: "", value: "" }, { shouldFocus: false }); } }, [fields]); const watchLastValue = watch(`${name}.${fields.length - 1}.value`, ""); return ( {fields.map((attribute, rowIndex) => ( ))}
{t("key")} {t("value")}
( toggleKeySelect(rowIndex, open)} isOpen={isKeyOpenArray[rowIndex]} variant={SelectVariant.typeahead} typeAheadAriaLabel={t("selectOrTypeAKey")} placeholderText={t("selectOrTypeAKey")} selections={field.value} onSelect={(v) => { field.onChange(v.toString()); toggleKeySelect(rowIndex, false); }} > {selectableValues?.map((attribute) => ( {attribute.name} ))} )} />
); };