box-ui-elements
Version:
Box UI Elements
73 lines (71 loc) • 2.32 kB
JavaScript
/**
*
* @file ContentSharing Element
* @description This is the top-level component for ContentSharing. It instantiates the API, which it then
* passes to the SharingModal component either immediately (when no custom button is provided) or on
* button click (when a custom button is provided).
* @author Box
*/
import 'regenerator-runtime/runtime';
import * as React from 'react';
import API from '../../api';
import SharingModal from './SharingModal';
import { CLIENT_NAME_CONTENT_SHARING, DEFAULT_HOSTNAME_API } from '../../constants';
import '../common/base.scss';
import '../common/fonts.scss';
import '../common/modal.scss';
const createAPI = (apiHost, itemID, itemType, token) => new API({
apiHost,
clientName: CLIENT_NAME_CONTENT_SHARING,
id: `${itemType}_${itemID}`,
token
});
function ContentSharing({
apiHost = DEFAULT_HOSTNAME_API,
config,
customButton,
displayInModal,
itemID,
itemType,
language,
messages,
token,
uuid
}) {
const [api, setAPI] = React.useState(createAPI(apiHost, itemID, itemType, token));
const [launchButton, setLaunchButton] = React.useState(null);
const [isVisible, setIsVisible] = React.useState(!customButton);
// Reset the API if necessary
React.useEffect(() => {
if (apiHost && itemID && itemType && token) {
setAPI(createAPI(apiHost, itemID, itemType, token));
}
}, [apiHost, itemID, itemType, token]);
// Reset state if the API has changed
React.useEffect(() => {
setIsVisible(!customButton);
}, [api, customButton, uuid]);
React.useEffect(() => {
if (customButton && !launchButton) {
setLaunchButton(/*#__PURE__*/React.cloneElement(customButton, {
onClick: () => {
return setIsVisible(true);
}
}));
}
}, [config, customButton, displayInModal, itemID, itemType, language, launchButton, messages, isVisible]);
return /*#__PURE__*/React.createElement(React.Fragment, null, launchButton, api && /*#__PURE__*/React.createElement(SharingModal, {
api: api,
config: config,
displayInModal: displayInModal,
isVisible: isVisible,
itemID: itemID,
itemType: itemType,
language: language,
messages: messages,
setIsVisible: setIsVisible,
uuid: uuid
}));
}
export default ContentSharing;
//# sourceMappingURL=ContentSharing.js.map