@carbon/ibm-products
Version:
Carbon for IBM Products
76 lines (74 loc) • 2.72 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_settings = require("../../settings.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(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] = (0, react.useState)({});
(0, react.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.default.createElement("div", { className: `${require_settings.pkg.prefix}--apikey-modal__download-container` }, /* @__PURE__ */ react.default.createElement("p", { className: `${require_settings.pkg.prefix}--apikey-modal__messaging-text` }, body, " ", /* @__PURE__ */ react.default.createElement("a", {
...linkProps,
className: `${require_settings.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: prop_types.default.string.isRequired,
/**
* body content for the downloader
*/
body: prop_types.default.string,
/**
* aria-label for the download link
*/
downloadLinkLabel: prop_types.default.string,
/**
* designates the name of downloadable json file with the key. if not specified will default to 'apikey'
*/
fileName: prop_types.default.string.isRequired,
/**
* designates the file type for the downloadable key
*/
fileType: prop_types.default.oneOf(["txt", "json"]).isRequired,
/**
* anchor text for the download link
*/
linkText: prop_types.default.string.isRequired
};
//#endregion
exports.APIKeyDownloader = APIKeyDownloader;