import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import { Button, Flex, FlexItem, Form, FormGroup, Label, Modal, ModalVariant, Text, TextContent, TextVariants, } from "@patternfly/react-core"; import { Table, Tbody, Td, Th, Thead, Tr } from "@patternfly/react-table"; import { SearchIcon } from "@patternfly/react-icons"; import { useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { FormProvider, useForm } from "react-hook-form"; import { useRealm } from "../../../context/realm-context/RealmContext"; import { useWhoAmI } from "../../../context/whoami/WhoAmI"; import { adminClient } from "../../../admin-client"; import { PaginatingTableToolbar } from "../../../components/table-toolbar/PaginatingTableToolbar"; import { ListEmptyState } from "../../../components/list-empty-state/ListEmptyState"; import { useFetch } from "../../../utils/useFetch"; import { localeToDisplayName } from "../../../util"; import { DEFAULT_LOCALE } from "../../../i18n/i18n"; import { TextControl } from "ui-shared"; type TranslationForm = { locale: string; value: string; }; type Translations = { key: string; translations: TranslationForm[]; }; export type AddTranslationsDialogProps = { translationKey: string; onCancel: () => void; toggleDialog: () => void; onTranslationsAdded: (translations: Translations) => void; }; export const AddTranslationsDialog = ({ translationKey, onCancel, toggleDialog, onTranslationsAdded, }: AddTranslationsDialogProps) => { const { t } = useTranslation(); const { realm: realmName } = useRealm(); const [realm, setRealm] = useState(); const { whoAmI } = useWhoAmI(); const [max, setMax] = useState(10); const [first, setFirst] = useState(0); const [filter, setFilter] = useState(""); const form = useForm<{ key: string; translations: TranslationForm[]; }>({ mode: "onChange", }); const { getValues, handleSubmit, setValue, formState: { isValid }, } = form; useFetch( () => adminClient.realms.findOne({ realm: realmName }), (realm) => { if (!realm) { throw new Error(t("notFound")); } setRealm(realm); }, [], ); const defaultSupportedLocales = useMemo(() => { return realm?.supportedLocales!.length ? realm.supportedLocales : [DEFAULT_LOCALE]; }, [realm]); const defaultLocales = useMemo(() => { return realm?.defaultLocale!.length ? [realm.defaultLocale] : []; }, [realm]); const combinedLocales = useMemo(() => { return Array.from(new Set([...defaultLocales, ...defaultSupportedLocales])); }, [defaultLocales, defaultSupportedLocales]); const filteredLocales = useMemo(() => { return combinedLocales.filter((locale) => localeToDisplayName(locale, whoAmI.getLocale())! .toLowerCase() .includes(filter.toLowerCase()), ); }, [combinedLocales, filter, whoAmI]); useEffect(() => { combinedLocales.forEach((locale, rowIndex) => { setValue(`translations.${rowIndex}`, { locale, value: "", }); setValue("key", translationKey); }); }, [combinedLocales, translationKey, setValue]); const handleOk = () => { const formData = getValues(); onTranslationsAdded(formData); toggleDialog(); }; return ( {t("addTranslationDialogOkBtn")} , , ]} > {t("addTranslationsModalSubTitle")}{" "} {t("addTranslationsModalSubTitleBolded")}
{t("translationsTableHeading")} { setFirst(first); setMax(max); }} inputGroupName={"search"} inputGroupOnEnter={(search) => { setFilter(search); setFirst(0); setMax(10); }} inputGroupPlaceholder={t("searchForLanguage")} > {filteredLocales.length === 0 && !filter && ( )} {filteredLocales.length === 0 && filter && ( )} {filteredLocales.length !== 0 && ( {filteredLocales.map((locale, rowIndex) => ( ))}
{t("supportedLanguagesTableColumnName")} {t("translationTableColumnName")}
{localeToDisplayName( locale, whoAmI.getLocale(), )} {locale === defaultLocales.toString() && ( )} {locale === defaultLocales.toString() && ( )} {locale !== defaultLocales.toString() && ( )}
)}
); };