@carbon/ibm-products
Version:
Carbon for IBM Products
73 lines (71 loc) • 2.39 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { pkg } from "../../settings.js";
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
//#region src/components/APIKeyModal/APIKeyDownloader.tsx
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const componentName = "APIKeyDownloader";
const APIKeyDownloader = (props) => {
const { apiKey, body, fileName, fileType, linkText, downloadLinkLabel } = props;
const [linkProps, setLinkProps] = useState({});
useEffect(() => {
const generateLinkProps = async () => {
const data = fileType === "txt" ? apiKey : JSON.stringify({ apiKey });
const blob = new Blob([data], { type: fileType === "txt" ? "text/plain" : "application/json" });
setLinkProps({
href: await URL.createObjectURL(blob),
download: `${fileName || "apikey"}.${fileType}`
});
};
generateLinkProps();
}, [
apiKey,
fileName,
fileType
]);
return /* @__PURE__ */ React.createElement("div", { className: `${pkg.prefix}--apikey-modal__download-container` }, /* @__PURE__ */ React.createElement("p", { className: `${pkg.prefix}--apikey-modal__messaging-text` }, body, " ", /* @__PURE__ */ React.createElement("a", {
...linkProps,
className: `${pkg.prefix}--apikey-modal__download-link`,
"aria-label": downloadLinkLabel ?? linkText,
role: "button"
}, downloadLinkLabel ?? linkText)));
};
APIKeyDownloader.displayName = componentName;
APIKeyDownloader.propTypes = {
/**
* the api key that's displayed to the user when a request to create is fulfilled.
*/
apiKey: PropTypes.string.isRequired,
/**
* body content for the downloader
*/
body: PropTypes.string,
/**
* aria-label for the download link
*/
downloadLinkLabel: PropTypes.string,
/**
* designates the name of downloadable json file with the key. if not specified will default to 'apikey'
*/
fileName: PropTypes.string.isRequired,
/**
* designates the file type for the downloadable key
*/
fileType: PropTypes.oneOf(["txt", "json"]).isRequired,
/**
* anchor text for the download link
*/
linkText: PropTypes.string.isRequired
};
//#endregion
export { APIKeyDownloader };