import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import { ActionGroup, ActionListItem, Alert, AlertActionLink, AlertVariant, Button, Checkbox, FormGroup, PageSection, Switch, } from "@patternfly/react-core"; import { Controller, useForm, useWatch } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import { HelpItem } from "ui-shared"; import { adminClient } from "../admin-client"; import { useAlerts } from "../components/alert/Alerts"; import { FormAccess } from "../components/form/FormAccess"; import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput"; import { PasswordInput } from "../components/password-input/PasswordInput"; import { FormPanel } from "../components/scroll-form/FormPanel"; import { useRealm } from "../context/realm-context/RealmContext"; import { toUser } from "../user/routes/User"; import { emailRegexPattern } from "../util"; import { useCurrentUser } from "../utils/useCurrentUser"; import useToggle from "../utils/useToggle"; import "./realm-settings-section.css"; type RealmSettingsEmailTabProps = { realm: RealmRepresentation; save: (realm: RealmRepresentation) => void; }; export const RealmSettingsEmailTab = ({ realm, save, }: RealmSettingsEmailTabProps) => { const { t } = useTranslation("realm-settings"); const { realm: realmName } = useRealm(); const { addAlert, addError } = useAlerts(); const currentUser = useCurrentUser(); const { register, control, handleSubmit, watch, reset: resetForm, getValues, formState: { errors }, } = useForm({ defaultValues: realm }); const reset = () => resetForm(realm); const watchFromValue = watch("smtpServer.from", ""); const watchHostValue = watch("smtpServer.host", ""); const [isTesting, toggleTest] = useToggle(); const authenticationEnabled = useWatch({ control, name: "smtpServer.auth", defaultValue: "", }); const testConnection = async () => { const toNumber = (value: string) => Number(value); const toBoolean = (value: string) => value === true.toString(); const valueMapper = new Map unknown>([ ["port", toNumber], ["ssl", toBoolean], ["starttls", toBoolean], ["auth", toBoolean], ]); const serverSettings = { ...getValues()["smtpServer"] }; for (const [key, mapperFn] of valueMapper.entries()) { serverSettings[key] = mapperFn(serverSettings[key]); } // For default value, back end is expecting null instead of 0 if (serverSettings.port === 0) serverSettings.port = null; try { toggleTest(); await adminClient.realms.testSMTPConnection( { realm: realm.realm! }, serverSettings ); addAlert(t("testConnectionSuccess"), AlertVariant.success); } catch (error) { addError("realm-settings:testConnectionError", error); } toggleTest(); }; return ( } > } > } > ( field.onChange("" + value)} /> )} /> ( field.onChange("" + value)} /> )} /> ( { field.onChange("" + value); }} aria-label={t("authentication")} /> )} /> {authenticationEnabled === "true" && ( <> } > )} {currentUser && ( {currentUser.email ? ( ) : ( ( )} > {t("testConnectionHint.withoutEmailAction")} } /> )} )} ); };