Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	File size: 2,820 Bytes
			
			6bcb42f  | 
								1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102  | 
								import bindAll from 'lodash.bindall';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import {setUsername, setUsernameInvalid, setUsernameLoggedIn} from '../reducers/tw';
let origin = "https://penguinmod.com";
class HomeCommunication extends React.Component {
    constructor (props) {
        super(props);
        bindAll(this, [
            'wrapperEventHandler'
        ]);
        
        this.state = {
            frame: null,
            canSetUsername: true,
        };
    }
    
    componentDidMount() {
        window.addEventListener('message', this.wrapperEventHandler);
        const iframe = document.createElement('iframe');
        iframe.src = `${origin}/embed/editor?external=${encodeURIComponent(window.origin)}`;
        iframe.width = 100;
        iframe.height = 100;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
        
        this.setState({
            frame: iframe
        });
    }
    componentWillUnmount() {
        window.removeEventListener('message', this.wrapperEventHandler);
        const iframe = this.state.frame;
        if (iframe) {
            iframe.remove();
        }
        this.setState({
            frame: null
        });
    }
    
    async wrapperEventHandler(e) {
        const data = e.data;
        // Don't recursively try to run this event.
        if (e.origin === window.origin) {
            return;
        }
        if (!data.type) return;
        if (!data.packet) return;
        switch (data.type) {
            case 'login': {
                if (data.packet.loggedIn !== true) return;
                if (!data.packet.username) return;
                if (!this.state.canSetUsername) return;
                this.props.onSetUsername(data.packet.username);
                this.setState({
                    canSetUsername: false
                });
            }
        }
    }
    render () {
        return (
            <div></div>
        );
    }
}
HomeCommunication.propTypes = {
    projectId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    isPlayground: PropTypes.bool,
    onSetUsername: PropTypes.func,
    username: PropTypes.string,
    usernameInvalid: PropTypes.bool,
    usernameLoggedIn: PropTypes.bool
};
const mapStateToProps = state => ({
    username: state.scratchGui.tw.username,
    usernameInvalid: state.scratchGui.tw.usernameInvalid,
    usernameLoggedIn: state.scratchGui.tw.usernameLoggedIn
});
const mapDispatchToProps = dispatch => ({
    onSetUsername: username => {
        dispatch(setUsername(username));
        dispatch(setUsernameLoggedIn(true));
        dispatch(setUsernameInvalid(false));
    }
});
export default connect(
    mapStateToProps,
    mapDispatchToProps
)(HomeCommunication);
 |