import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import type { UserProfileConfig } from "@keycloak/keycloak-admin-client/lib/defs/userProfileMetadata"; import { Divider, FormGroup, Radio, Select, SelectOption, SelectVariant, Switch, } from "@patternfly/react-core"; import { isEqual } from "lodash-es"; import { useState } from "react"; import { Controller, useFormContext, useWatch } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { HelpItem } from "ui-shared"; import { adminClient } from "../../../admin-client"; import { FormAccess } from "../../../components/form/FormAccess"; import { KeycloakSpinner } from "../../../components/keycloak-spinner/KeycloakSpinner"; import { KeycloakTextInput } from "../../../components/keycloak-text-input/KeycloakTextInput"; import { useFetch } from "../../../utils/useFetch"; import { useParams } from "../../../utils/useParams"; import { USERNAME_EMAIL } from "../../NewAttributeSettings"; import type { AttributeParams } from "../../routes/Attribute"; import "../../realm-settings-section.css"; 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 { t } = useTranslation(); const form = useFormContext(); const [clientScopes, setClientScopes] = useState(); const [config, setConfig] = useState(); const [selectEnabledWhenOpen, setSelectEnabledWhenOpen] = useState(false); const [selectRequiredForOpen, setSelectRequiredForOpen] = useState(false); const [isAttributeGroupDropdownOpen, setIsAttributeGroupDropdownOpen] = useState(false); 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); } return ( } fieldId="kc-attribute-name" isRequired validated={form.formState.errors.name ? "error" : "default"} helperTextInvalid={t("validateAttributeName")} > } fieldId="kc-attribute-display-name" > } fieldId="kc-attributeGroup" > ( )} > {!USERNAME_EMAIL.includes(attributeName) && ( <> } fieldId="enabledWhen" hasNoPaddingTop > setHasSelector(false)} className="pf-u-mb-md" /> setHasSelector(true)} className="pf-u-mb-md" /> {hasSelector && ( ( )} /> )} )} {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-u-mb-md" /> setHasRequiredScopes(true)} className="pf-u-mb-md" /> {hasRequiredScopes && ( ( )} /> )} )} )}
); };