2020-10-06 19:51:21 +00:00
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
AlertVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
2020-10-29 17:02:06 +00:00
|
|
|
ModalVariant,
|
2020-10-06 19:51:21 +00:00
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Stack,
|
|
|
|
StackItem,
|
|
|
|
TextArea,
|
|
|
|
} from "@patternfly/react-core";
|
2020-10-13 17:57:35 +00:00
|
|
|
import FileSaver from "file-saver";
|
2021-08-05 10:42:29 +00:00
|
|
|
import React, { useEffect, useMemo, useState } from "react";
|
2020-10-06 19:51:21 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-04-29 06:28:59 +00:00
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useServerInfo } from "../../context/server-info/ServerInfoProvider";
|
2021-10-08 18:21:09 +00:00
|
|
|
import { prettyPrintJSON } from "../../util";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { ConfirmDialogModal } from "../confirm-dialog/ConfirmDialog";
|
|
|
|
import { useHelp } from "../help-enabler/HelpHeader";
|
|
|
|
import { HelpItem } from "../help-enabler/HelpItem";
|
2020-10-06 19:51:21 +00:00
|
|
|
|
2021-02-28 20:02:31 +00:00
|
|
|
type DownloadDialogProps = {
|
2020-10-06 19:51:21 +00:00
|
|
|
id: string;
|
|
|
|
protocol?: string;
|
|
|
|
open: boolean;
|
|
|
|
toggleDialog: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DownloadDialog = ({
|
|
|
|
id,
|
|
|
|
open,
|
|
|
|
toggleDialog,
|
|
|
|
protocol = "openid-connect",
|
2021-02-28 20:02:31 +00:00
|
|
|
}: DownloadDialogProps) => {
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-10-06 19:51:21 +00:00
|
|
|
const { t } = useTranslation("common");
|
2021-07-21 09:30:18 +00:00
|
|
|
const { enabled } = useHelp();
|
2020-10-13 17:57:35 +00:00
|
|
|
const serverInfo = useServerInfo();
|
2020-10-06 19:51:21 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const configFormats = serverInfo.clientInstallations![protocol];
|
2020-10-06 19:51:21 +00:00
|
|
|
const [selected, setSelected] = useState(
|
|
|
|
configFormats[configFormats.length - 1].id
|
|
|
|
);
|
|
|
|
const [snippet, setSnippet] = useState("");
|
|
|
|
const [openType, setOpenType] = useState(false);
|
|
|
|
|
2021-08-04 12:24:55 +00:00
|
|
|
const selectedConfig = useMemo(
|
2021-10-08 18:21:09 +00:00
|
|
|
() => configFormats.find((config) => config.id === selected) ?? null,
|
2021-08-04 12:24:55 +00:00
|
|
|
[selected]
|
|
|
|
);
|
|
|
|
|
2021-10-14 07:52:36 +00:00
|
|
|
const sanitizeSnippet = (snippet: string) =>
|
|
|
|
snippet.replace(
|
|
|
|
/(?<=<PrivateKeyPem>).*(?=<\/PrivateKeyPem>)/gs,
|
|
|
|
t("clients:privateKeyMask")
|
|
|
|
);
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
const snippet = await adminClient.clients.getInstallationProviders({
|
|
|
|
id,
|
|
|
|
providerId: selected,
|
|
|
|
});
|
|
|
|
if (typeof snippet === "string") {
|
2021-10-14 07:52:36 +00:00
|
|
|
return sanitizeSnippet(snippet);
|
2021-04-29 06:28:59 +00:00
|
|
|
} else {
|
2021-10-08 18:21:09 +00:00
|
|
|
return prettyPrintJSON(snippet);
|
2021-04-29 06:28:59 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(snippet) => setSnippet(snippet),
|
|
|
|
[id, selected]
|
|
|
|
);
|
2021-08-05 10:42:29 +00:00
|
|
|
|
|
|
|
// Clear snippet when selected config changes, this prevents old snippets from being displayed during fetch.
|
|
|
|
useEffect(() => setSnippet(""), [id, selected]);
|
|
|
|
|
2020-10-06 19:51:21 +00:00
|
|
|
return (
|
|
|
|
<ConfirmDialogModal
|
|
|
|
titleKey={t("clients:downloadAdaptorTitle")}
|
|
|
|
continueButtonLabel={t("download")}
|
2020-10-13 17:57:35 +00:00
|
|
|
onConfirm={() => {
|
|
|
|
const config = configFormats.find((config) => config.id === selected)!;
|
|
|
|
FileSaver.saveAs(
|
|
|
|
new Blob([snippet], { type: config.mediaType }),
|
|
|
|
config.filename
|
|
|
|
);
|
|
|
|
}}
|
2020-10-06 19:51:21 +00:00
|
|
|
open={open}
|
|
|
|
toggleDialog={toggleDialog}
|
2020-10-29 17:02:06 +00:00
|
|
|
variant={ModalVariant.medium}
|
2020-10-06 19:51:21 +00:00
|
|
|
>
|
|
|
|
<Form>
|
|
|
|
<Stack hasGutter>
|
2020-10-13 17:57:35 +00:00
|
|
|
{enabled && (
|
|
|
|
<StackItem>
|
|
|
|
<Alert
|
|
|
|
id={id}
|
|
|
|
title={t("clients:description")}
|
|
|
|
variant={AlertVariant.info}
|
|
|
|
isInline
|
|
|
|
>
|
|
|
|
{
|
|
|
|
configFormats.find(
|
|
|
|
(configFormat) => configFormat.id === selected
|
|
|
|
)?.helpText
|
|
|
|
}
|
|
|
|
</Alert>
|
|
|
|
</StackItem>
|
|
|
|
)}
|
2020-10-06 19:51:21 +00:00
|
|
|
<StackItem>
|
|
|
|
<FormGroup
|
|
|
|
fieldId="type"
|
|
|
|
label={t("clients:formatOption")}
|
2020-10-09 06:48:50 +00:00
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("clients-help:downloadType")}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:formatOption"
|
2020-10-09 06:48:50 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-10-06 19:51:21 +00:00
|
|
|
>
|
|
|
|
<Select
|
2020-10-09 06:48:50 +00:00
|
|
|
toggleId="type"
|
2020-10-06 19:51:21 +00:00
|
|
|
isOpen={openType}
|
2021-08-05 10:42:29 +00:00
|
|
|
onToggle={(isExpanded) => setOpenType(isExpanded)}
|
2020-10-06 19:51:21 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
value={selected}
|
|
|
|
selections={selected}
|
|
|
|
onSelect={(_, value) => {
|
2021-08-04 12:24:55 +00:00
|
|
|
setSelected(value.toString());
|
2020-10-06 19:51:21 +00:00
|
|
|
setOpenType(false);
|
|
|
|
}}
|
|
|
|
aria-label="Select Input"
|
2021-08-05 13:01:48 +00:00
|
|
|
menuAppendTo={() => document.body}
|
2020-10-06 19:51:21 +00:00
|
|
|
>
|
|
|
|
{configFormats.map((configFormat) => (
|
|
|
|
<SelectOption
|
|
|
|
key={configFormat.id}
|
|
|
|
value={configFormat.id}
|
|
|
|
isSelected={selected === configFormat.id}
|
|
|
|
>
|
|
|
|
{configFormat.displayType}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormGroup>
|
|
|
|
</StackItem>
|
2021-08-04 12:24:55 +00:00
|
|
|
{!selectedConfig?.downloadOnly && (
|
|
|
|
<StackItem isFilled>
|
|
|
|
<FormGroup
|
|
|
|
fieldId="details"
|
2021-09-03 13:54:23 +00:00
|
|
|
label={t("details")}
|
2021-08-04 12:24:55 +00:00
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("clients-help:details")}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:details"
|
2021-08-04 12:24:55 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<TextArea
|
|
|
|
id="details"
|
|
|
|
readOnly
|
|
|
|
rows={12}
|
|
|
|
resizeOrientation="vertical"
|
|
|
|
value={snippet}
|
|
|
|
aria-label="text area example"
|
2020-10-09 06:48:50 +00:00
|
|
|
/>
|
2021-08-04 12:24:55 +00:00
|
|
|
</FormGroup>
|
|
|
|
</StackItem>
|
|
|
|
)}
|
2020-10-06 19:51:21 +00:00
|
|
|
</Stack>
|
|
|
|
</Form>
|
|
|
|
</ConfirmDialogModal>
|
|
|
|
);
|
|
|
|
};
|