@keycloakify/keycloak-admin-ui
Version:
Repackaged Keycloak Admin UI
58 lines • 5.05 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { fetchWithError } from "@keycloak/keycloak-admin-client";
import { HelpItem, useFetch, useHelp } from "../../ui-shared";
import { Form, FormGroup, MenuToggle, ModalVariant, Select, SelectList, SelectOption, Stack, StackItem, TextArea, } from "@patternfly/react-core";
import { saveAs } from "file-saver";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useAdminClient } from "../../admin-client";
import { useRealm } from "../../context/realm-context/RealmContext";
import { useServerInfo } from "../../context/server-info/ServerInfoProvider";
import { addTrailingSlash, prettyPrintJSON } from "../../util";
import { getAuthorizationHeaders } from "../../utils/getAuthorizationHeaders";
import { ConfirmDialogModal } from "../../components/confirm-dialog/ConfirmDialog";
export const DownloadDialog = ({ id, open, toggleDialog, protocol = "openid-connect", }) => {
const { adminClient } = useAdminClient();
const { realm } = useRealm();
const { t } = useTranslation();
const { enabled } = useHelp();
const serverInfo = useServerInfo();
const configFormats = serverInfo.clientInstallations[protocol];
const [selected, setSelected] = useState(configFormats[configFormats.length - 1].id);
const [snippet, setSnippet] = useState();
const [openType, setOpenType] = useState(false);
const selectedConfig = useMemo(() => { var _a; return (_a = configFormats.find((config) => config.id === selected)) !== null && _a !== void 0 ? _a : null; }, [selected]);
const sanitizeSnippet = (snippet) => snippet.replace(/<PrivateKeyPem>.*<\/PrivateKeyPem>/gs, `<PrivateKeyPem>${t("privateKeyMask")}</PrivateKeyPem>`);
useFetch(async () => {
if ((selectedConfig === null || selectedConfig === void 0 ? void 0 : selectedConfig.mediaType) === "application/zip") {
const response = await fetchWithError(`${addTrailingSlash(adminClient.baseUrl)}admin/realms/${realm}/clients/${id}/installation/providers/${selected}`, {
method: "GET",
headers: getAuthorizationHeaders(await adminClient.getAccessToken()),
});
return response.arrayBuffer();
}
else {
const snippet = await adminClient.clients.getInstallationProviders({
id,
providerId: selected,
});
if (typeof snippet === "string") {
return sanitizeSnippet(snippet);
}
else {
return prettyPrintJSON(snippet);
}
}
}, (snippet) => setSnippet(snippet), [id, selected]);
// Clear snippet when selected config changes, this prevents old snippets from being displayed during fetch.
useEffect(() => setSnippet(""), [id, selected]);
return (_jsx(ConfirmDialogModal, { titleKey: t("downloadAdaptorTitle"), continueButtonLabel: t("download"), onConfirm: () => {
saveAs(new Blob([snippet], { type: selectedConfig === null || selectedConfig === void 0 ? void 0 : selectedConfig.mediaType }), selectedConfig === null || selectedConfig === void 0 ? void 0 : selectedConfig.filename);
}, open: open, toggleDialog: toggleDialog, variant: ModalVariant.medium, children: _jsx(Form, { children: _jsxs(Stack, { hasGutter: true, children: [_jsx(StackItem, { children: _jsx(FormGroup, { fieldId: "type", label: t("formatOption"), labelIcon: _jsx(HelpItem, { helpText: t("downloadType"), fieldLabelId: "formatOption" }), children: _jsx(Select, { isOpen: openType, onOpenChange: (isOpen) => setOpenType(isOpen), toggle: (ref) => (_jsx(MenuToggle, { id: "type", ref: ref, onClick: () => setOpenType(!openType), isExpanded: openType, children: selected })), selected: selected, onSelect: (_, value) => {
setSelected((value === null || value === void 0 ? void 0 : value.toString()) || "");
setOpenType(false);
}, "aria-label": t("selectOne"), popperProps: {
appendTo: document.body,
}, children: _jsx(SelectList, { children: configFormats.map((configFormat) => (_jsx(SelectOption, { value: configFormat.id, isSelected: selected === configFormat.id, description: enabled ? configFormat.helpText : undefined, children: configFormat.displayType }, configFormat.id))) }) }) }) }), !(selectedConfig === null || selectedConfig === void 0 ? void 0 : selectedConfig.downloadOnly) && (_jsx(StackItem, { isFilled: true, children: _jsx(FormGroup, { fieldId: "details", label: t("details"), labelIcon: _jsx(HelpItem, { helpText: t("detailsHelp"), fieldLabelId: "details" }), children: _jsx(TextArea, { id: "details", readOnly: true, rows: 12, resizeOrientation: "vertical", value: snippet && typeof snippet === "string" ? snippet : "", "aria-label": "text area example" }) }) }))] }) }) }));
};
//# sourceMappingURL=DownloadDialog.js.map