@carbon/ibm-products
Version:
Carbon for IBM Products
81 lines (77 loc) • 2.48 kB
JavaScript
/**
* 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.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { useState, useEffect } from 'react';
import PropTypes from '../../_virtual/index.js';
import { pkg } from '../../settings.js';
const componentName = 'APIKeyDownloader';
const APIKeyDownloader = _ref => {
let {
apiKey,
body,
fileName,
fileType,
linkText,
downloadLinkLabel
} = _ref;
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'
});
const href = await URL.createObjectURL(blob);
const download = `${fileName || 'apikey'}.${fileType}`;
const props = {
href,
download
};
setLinkProps(props);
};
generateLinkProps();
}, [apiKey, fileName, fileType]);
return /*#__PURE__*/React__default.createElement("div", {
className: `${pkg.prefix}--apikey-modal__download-container`
}, /*#__PURE__*/React__default.createElement("p", {
className: `${pkg.prefix}--apikey-modal__messaging-text`
}, body, ' ', /*#__PURE__*/React__default.createElement("a", _extends({}, 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
};
export { APIKeyDownloader };