UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

83 lines (78 loc) 2.54 kB
/** * Copyright IBM Corp. 2020, 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. */ 'use strict'; var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var React = require('react'); var index = require('../../_virtual/index.js'); var settings = require('../../settings.js'); const componentName = 'APIKeyDownloader'; const APIKeyDownloader = _ref => { let { apiKey, body, fileName, fileType, linkText, downloadLinkLabel } = _ref; const [linkProps, setLinkProps] = React.useState({}); 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' }); const href = await URL.createObjectURL(blob); const download = `${fileName || 'apikey'}.${fileType}`; const props = { href, download }; setLinkProps(props); }; generateLinkProps(); }, [apiKey, fileName, fileType]); return /*#__PURE__*/React.createElement("div", { className: `${settings.pkg.prefix}--apikey-modal__download-container` }, /*#__PURE__*/React.createElement("p", { className: `${settings.pkg.prefix}--apikey-modal__messaging-text` }, body, ' ', /*#__PURE__*/React.createElement("a", _rollupPluginBabelHelpers.extends({}, linkProps, { className: `${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: index.default.string.isRequired, /** * body content for the downloader */ body: index.default.string, /** * aria-label for the download link */ downloadLinkLabel: index.default.string, /** * designates the name of downloadable json file with the key. if not specified will default to 'apikey' */ fileName: index.default.string.isRequired, /** * designates the file type for the downloadable key */ fileType: index.default.oneOf(['txt', 'json']).isRequired, /** * anchor text for the download link */ linkText: index.default.string.isRequired }; exports.APIKeyDownloader = APIKeyDownloader;