/* eslint-disable */ // @ts-nocheck import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import type { UserProfileConfig } from "@keycloak/keycloak-admin-client/lib/defs/userProfileMetadata"; import { HelpItem, KeycloakSelect, KeycloakSpinner, SelectControl, SelectVariant, TextControl, useFetch, } from "../../../../shared/keycloak-ui-shared"; import { Divider, FormGroup, Radio, SelectOption, Switch, } from "../../../../shared/@patternfly/react-core"; import { isEqual } from "lodash-es"; import { useState } from "react"; import { Controller, FormProvider, useFormContext, useWatch, } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../../admin-client"; import { FormAccess } from "../../../components/form/FormAccess"; import { DefaultSwitchControl } from "../../../components/SwitchControl"; import { useParams } from "../../../utils/useParams"; import { USERNAME_EMAIL } from "../../NewAttributeSettings"; import { AttributeParams } from "../../routes/Attribute"; import { TranslatableField } from "./TranslatableField"; import "../../realm-settings-section.css"; import useLocaleSort, { mapByKey } from "../../../utils/useLocaleSort"; const REQUIRED_FOR = [ { label: "requiredForLabel.both", value: ["admin", "user"] }, { label: "requiredForLabel.users", value: ["user"] }, { label: "requiredForLabel.admins", value: ["admin"] }, ] as const; export const AttributeGeneralSettings = () => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const form = useFormContext(); const [clientScopes, setClientScopes] = useState(); const [config, setConfig] = useState(); const [selectEnabledWhenOpen, setSelectEnabledWhenOpen] = useState(false); const [selectRequiredForOpen, setSelectRequiredForOpen] = useState(false); const [enabledWhenSearch, setEnableWhenSearch] = useState(""); const localeSort = useLocaleSort(); const { attributeName } = useParams(); const editMode = attributeName ? true : false; const hasSelector = useWatch({ control: form.control, name: "hasSelector", }); const hasRequiredScopes = useWatch({ control: form.control, name: "hasRequiredScopes", }); const required = useWatch({ control: form.control, name: "isRequired", defaultValue: false, }); useFetch(() => adminClient.clientScopes.find(), setClientScopes, []); useFetch(() => adminClient.users.getProfile(), setConfig, []); if (!clientScopes) { return ; } function setHasSelector(hasSelector: boolean) { form.setValue("hasSelector", hasSelector); } function setHasRequiredScopes(hasRequiredScopes: boolean) { form.setValue("hasRequiredScopes", hasRequiredScopes); } const items = () => localeSort(clientScopes, mapByKey("name")) .filter( (s) => enabledWhenSearch === "" || s.name?.includes(enabledWhenSearch), ) .map((option) => ( {option.name} )); const ROOT_ATTRIBUTE = [...USERNAME_EMAIL, "firstName", "lastName"]; return ( } fieldId="kc-attribute-displayName" > {!ROOT_ATTRIBUTE.includes(attributeName) && ( )} ({ key: g.name!, value: g.name!, })) || []), ]} /> {!USERNAME_EMAIL.includes(attributeName) && ( <> } fieldId="enabledWhen" hasNoPaddingTop > setHasSelector(false)} className="pf-v5-u-mb-md" /> setHasSelector(true)} className="pf-v5-u-mb-md" /> {hasSelector && ( ( { setEnableWhenSearch(value); return items(); }} typeAheadAriaLabel="Select" chipGroupProps={{ numChips: 3, expandedText: t("hide"), collapsedText: t("showRemaining"), }} onToggle={(isOpen) => setSelectEnabledWhenOpen(isOpen)} selections={field.value} onSelect={(selectedValue) => { const option = selectedValue.toString(); let changedValue = [""]; if (field.value) { changedValue = field.value.includes(option) ? field.value.filter( (item: string) => item !== option, ) : [...field.value, option]; } else { changedValue = [option]; } field.onChange(changedValue); }} onClear={() => { field.onChange([]); }} isOpen={selectEnabledWhenOpen} aria-labelledby={"scope"} > {items()} )} /> )} )} {attributeName !== "username" && ( <> } fieldId="kc-required" hasNoPaddingTop > ( )} /> {required && ( <> (
{REQUIRED_FOR.map((option) => ( { field.onChange(option.value); }} label={t(option.label)} className="kc-requiredFor-option" /> ))}
)} />
} fieldId="requiredWhen" hasNoPaddingTop > setHasRequiredScopes(false)} className="pf-v5-u-mb-md" /> setHasRequiredScopes(true)} className="pf-v5-u-mb-md" /> {hasRequiredScopes && ( ( setSelectRequiredForOpen(isOpen) } selections={field.value} onSelect={(selectedValue) => { const option = selectedValue.toString(); let changedValue = [""]; if (field.value) { changedValue = field.value.includes(option) ? field.value.filter( (item: string) => item !== option, ) : [...field.value, option]; } else { changedValue = [option]; } field.onChange(changedValue); }} onClear={() => { field.onChange([]); }} isOpen={selectRequiredForOpen} aria-labelledby={"scope"} > {clientScopes.map((option) => ( {option.name} ))} )} /> )} )} )}
); };