|
| 1 | +/* |
| 2 | + * Copyright 2024 EPAM Systems |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import React, { useRef, useEffect, useState } from 'react'; |
| 18 | +import PropTypes from 'prop-types'; |
| 19 | +import { connect } from 'react-redux'; |
| 20 | +import { userInfoSelector } from 'controllers/user'; |
| 21 | +import { projectInfoSelector } from 'controllers/project'; |
| 22 | +import { extensionType } from '../extensionTypes'; |
| 23 | + |
| 24 | +// http://localhost:3000/#superadmin_personal/plugin/BrowserKube |
| 25 | +// TODO: add loader while loading the iframe |
| 26 | +// TODO: configure sandbox for iframe |
| 27 | +function StandaloneExtensionLoader({ extension, userInfo, projectInfo }) { |
| 28 | + const [loaded, setLoaded] = useState(false); |
| 29 | + const ref = useRef(); |
| 30 | + |
| 31 | + const onLoad = () => { |
| 32 | + setLoaded(true); |
| 33 | + }; |
| 34 | + |
| 35 | + const sendRpContext = () => { |
| 36 | + const consumerOrigin = new URL(extension.url).origin; |
| 37 | + const data = { |
| 38 | + user: userInfo, |
| 39 | + project: projectInfo, |
| 40 | + }; |
| 41 | + ref?.current?.contentWindow.postMessage(data, consumerOrigin); |
| 42 | + }; |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (loaded) { |
| 46 | + sendRpContext(); |
| 47 | + } |
| 48 | + }, [loaded, userInfo, projectInfo]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <iframe |
| 52 | + ref={ref} |
| 53 | + name={extension.pluginName} |
| 54 | + title={extension.pluginName} |
| 55 | + src={extension.url} |
| 56 | + style={{ width: '100%', height: '100%' }} |
| 57 | + onLoad={onLoad} |
| 58 | + seamless |
| 59 | + /> |
| 60 | + ); |
| 61 | +} |
| 62 | +StandaloneExtensionLoader.propTypes = { |
| 63 | + extension: extensionType, |
| 64 | + userInfo: PropTypes.object.isRequired, |
| 65 | + projectInfo: PropTypes.object.isRequired, |
| 66 | +}; |
| 67 | +StandaloneExtensionLoader.defaultProps = { |
| 68 | + extension: {}, |
| 69 | +}; |
| 70 | + |
| 71 | +const withConnect = connect((state) => ({ |
| 72 | + userInfo: userInfoSelector(state), |
| 73 | + projectInfo: projectInfoSelector(state), |
| 74 | +})); |
| 75 | + |
| 76 | +const ConnectedStandaloneExtensionLoader = withConnect(StandaloneExtensionLoader); |
| 77 | + |
| 78 | +export { ConnectedStandaloneExtensionLoader as StandaloneExtensionLoader }; |
0 commit comments