UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

61 lines (60 loc) 2.24 kB
import { jsx as _jsx } from "react/jsx-runtime"; import React from 'react'; const defaultOauthPopupProps = { onClose: () => { }, width: 500, height: 500, url: '', title: '', }; const OauthPopup = props => { let externalWindow; React.useEffect(() => { return () => { if (externalWindow) { externalWindow.close(); } }; }, // eslint-disable-next-line react-hooks/exhaustive-deps []); const createPopup = () => { const { url, title, width, height, onCode } = { ...defaultOauthPopupProps, ...props }; const left = window.screenX + (window.outerWidth - width) / 2; const top = window.screenY + (window.outerHeight - height) / 2.5; const windowFeatures = `toolbar=0,scrollbars=1,status=1,resizable=0,location=1,menuBar=0,width=${width},height=${height},top=${top},left=${left}`; externalWindow = window.open(url, title, windowFeatures); const storageListener = () => { try { const authStatus = localStorage.getItem('auth_status'); if (authStatus) { onCode(authStatus); localStorage.removeItem('auth_status'); if (externalWindow) { externalWindow.close(); } window.removeEventListener('storage', storageListener); } } catch (e) { console.error('Error occurred while closing auth window', e); window.removeEventListener('storage', storageListener); } }; window.addEventListener('storage', storageListener); if (externalWindow) { try { externalWindow.addEventListener('beforeunload', () => { if (!!props.onClose) { props.onClose(); } }, false); } catch (e) { console.error('Error occurred while adding beforeunload event listener'); } } }; return _jsx(props.button, { onClick: createPopup, children: props.children }); }; export default OauthPopup;