import { FormGroup, Spinner, Switch } from "@patternfly/react-core"; import debouncePromise from "p-debounce"; import { ReactNode, useMemo, useState } from "react"; import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { HelpItem, TextControl } from "@keycloak/keycloak-ui-shared"; import { useAdminClient } from "../../admin-client"; type DiscoveryEndpointFieldProps = { id: string; fileUpload: ReactNode; children: (readOnly: boolean) => ReactNode; }; export const DiscoveryEndpointField = ({ id, fileUpload, children, }: DiscoveryEndpointFieldProps) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { setValue, clearErrors, formState: { errors }, } = useFormContext(); const [discovery, setDiscovery] = useState(true); const [discovering, setDiscovering] = useState(false); const [discoveryResult, setDiscoveryResult] = useState>(); const setupForm = (result: Record) => { Object.keys(result).map((k) => setValue(`config.${k}`, result[k])); }; const discover = async (fromUrl: string) => { setDiscovering(true); try { const result = await adminClient.identityProviders.importFromUrl({ providerId: id, fromUrl, }); setupForm(result); setDiscoveryResult(result); } catch (error) { return (error as Error).message; } finally { setDiscovering(false); } }; const discoverDebounced = useMemo(() => debouncePromise(discover, 1000), []); return ( <> } > { clearErrors("discoveryError"); setDiscovery(checked); }} aria-label={t( id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor", )} /> {discovery && ( : undefined} rules={{ required: t("required"), validate: (value: string) => discoverDebounced(value), }} /> )} {!discovery && fileUpload} {discovery && !errors.discoveryError && children(true)} {!discovery && children(false)} ); };