import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { Controller, useForm, useFormContext, useWatch } from "react-hook-form"; import { ActionGroup, AlertVariant, Button, Divider, FormGroup, PageSection, Select, SelectGroup, SelectOption, SelectVariant, Switch, TextContent, ToolbarItem, } from "@patternfly/react-core"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/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 { HelpItem } from "../components/help-enabler/HelpItem"; import { useRealm } from "../context/realm-context/RealmContext"; import { DEFAULT_LOCALE } from "../i18n"; 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, }: LocalizationTabProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const [addMessageBundleModalOpen, setAddMessageBundleModalOpen] = useState(false); const [supportedLocalesOpen, setSupportedLocalesOpen] = useState(false); const [defaultLocaleOpen, setDefaultLocaleOpen] = useState(false); const [filterDropdownOpen, setFilterDropdownOpen] = useState(false); const [selectMenuLocale, setSelectMenuLocale] = useState(DEFAULT_LOCALE); const { getValues, control, handleSubmit, formState } = useFormContext(); const [selectMenuValueSelected, setSelectMenuValueSelected] = 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", }); const internationalizationEnabled = useWatch({ control, name: "internationalizationEnabled", defaultValue: false, }); const loader = async () => { try { const result = await adminClient.realms.getRealmLocalizationTexts({ realm: realm.realm!, selectedLocale: selectMenuLocale || getValues("defaultLocale") || DEFAULT_LOCALE, }); return Object.entries(result); } catch (error) { return []; } }; const tableLoader = async () => { try { const result = await adminClient.realms.getRealmLocalizationTexts({ realm: currentRealm, selectedLocale: selectMenuLocale, }); return Object.entries(result); } catch (error) { return []; } }; const handleModalToggle = () => { setAddMessageBundleModalOpen(!addMessageBundleModalOpen); }; const options = [ {watchSupportedLocales ?.filter((item) => item === realm.defaultLocale) .map((locale) => ( {t(`allSupportedLocales.${locale}`)} ))} , , {watchSupportedLocales ?.filter((item) => item !== realm.defaultLocale) .map((locale) => ( {t(`allSupportedLocales.${locale}`)} ))} , ]; const [tableKey, setTableKey] = useState(0); const refreshTable = () => { setTableKey(new Date().getTime()); }; const addKeyValue = async (pair: KeyValueType): Promise => { try { adminClient.setConfig({ requestConfig: { headers: { "Content-Type": "text/plain" } }, }); await adminClient.realms.addLocalization( { realm: currentRealm!, selectedLocale: selectMenuLocale || getValues("defaultLocale") || DEFAULT_LOCALE, key: pair.key, }, pair.value ); adminClient.setConfig({ realmName: currentRealm!, }); refreshTable(); addAlert(t("pairCreatedSuccess"), AlertVariant.success); } catch (error) { addError("realm-settings:pairCreatedError", error); } }; return ( <> {addMessageBundleModalOpen && ( { addKeyValue(pair); handleModalToggle(); }} form={bundleForm} /> )} } > ( )} /> {internationalizationEnabled && ( <> ( )} /> ( )} /> )} {t("messageBundleDescription")}
} toolbarItem={ } searchPlaceholderKey=" " emptyState={ } canSelectAll columns={[ { name: "Key", cellRenderer: (row) => row[0], }, { name: "Value", cellRenderer: (row) => row[1], }, ]} />
); };