import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { Controller, useForm, useFormContext, useWatch } from "react-hook-form"; import { ActionGroup, AlertVariant, Button, FormGroup, PageSection, Select, SelectOption, SelectVariant, Switch, TextContent, } from "@patternfly/react-core"; import type RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation"; import { FormAccess } from "../components/form-access/FormAccess"; import { useServerInfo } from "../context/server-info/ServerInfoProvider"; import { FormPanel } from "../components/scroll-form/FormPanel"; import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable"; import { useAdminClient } from "../context/auth/AdminClient"; import { ListEmptyState } from "../components/list-empty-state/ListEmptyState"; import { AddMessageBundleModal } from "./AddMessageBundleModal"; import { useAlerts } from "../components/alert/Alerts"; import { useRealm } from "../context/realm-context/RealmContext"; type LocalizationTabProps = { save: (realm: RealmRepresentation) => void; reset: () => void; refresh: () => void; realm: RealmRepresentation; }; export type KeyValueType = { key: string; value: string }; export type BundleForm = { messageBundle: KeyValueType; }; export const LocalizationTab = ({ save, reset, realm, refresh, }: LocalizationTabProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const [addMessageBundleModalOpen, setAddMessageBundleModalOpen] = useState(false); const [key, setKey] = useState(0); const [supportedLocalesOpen, setSupportedLocalesOpen] = useState(false); const [defaultLocaleOpen, setDefaultLocaleOpen] = useState(false); const { getValues, control, handleSubmit } = useFormContext(); const [valueSelected, setValueSelected] = useState(false); const themeTypes = useServerInfo().themes!; const bundleForm = useForm({ mode: "onChange" }); const { addAlert, addError } = useAlerts(); const { realm: currentRealm } = useRealm(); const watchSupportedLocales = useWatch({ control, name: "supportedLocales", defaultValue: themeTypes?.account![0].locales, }); const internationalizationEnabled = useWatch({ control, name: "internationalizationEnabled", defaultValue: realm?.internationalizationEnabled, }); const loader = async () => { try { const result = await adminClient.realms.getRealmLocalizationTexts({ realm: realm.realm!, selectedLocale: getValues("defaultLocale") || "en", }); return Object.keys(result).map((key) => [key, result[key]]); } catch (error) { return [[]]; } }; const handleModalToggle = () => { setAddMessageBundleModalOpen(!addMessageBundleModalOpen); }; const addKeyValue = async (pair: KeyValueType): Promise => { try { adminClient.setConfig({ requestConfig: { headers: { "Content-Type": "text/plain" } }, }); await adminClient.realms.addLocalization( { realm: currentRealm!, selectedLocale: getValues("defaultLocale") || "en", key: pair.key, }, pair.value ); adminClient.setConfig({ realmName: currentRealm!, }); refresh(); addAlert(t("pairCreatedSuccess"), AlertVariant.success); } catch (error) { addError("realm-settings:pairCreatedError", error); } }; return ( <> {addMessageBundleModalOpen && ( { addKeyValue(pair); handleModalToggle(); }} form={bundleForm} /> )} ( )} /> {internationalizationEnabled && ( <> ( )} /> ( )} /> )} {t("messageBundleDescription")}
setAddMessageBundleModalOpen(true)} > {t("addMessageBundle")} } searchPlaceholderKey=" " emptyState={ } canSelectAll columns={[ { name: "Key", cellRenderer: (row) => row[0], }, { name: "Value", cellRenderer: (row) => row[1], }, ]} />
); };