File size: 7,402 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import classNames from 'classnames';
import { connect } from 'react-redux';
import { FormattedMessage, injectIntl } from 'react-intl';
import PropTypes from 'prop-types';
import React from 'react';
import bindAll from 'lodash.bindall';
import Button from '../button/button.jsx';

import loadingIcon from './share-loading.svg';
import styles from './share-button.css';

const getProjectThumbnail = () => new Promise(resolve => {
    window.vm.renderer.requestSnapshot(uri => {
        resolve(uri);
    });
});
const getProjectUri = () => new Promise(resolve => {
    window.vm.saveProjectSb3().then(blob => new Promise(resolve => {
        const reader = new FileReader();
        reader.onload = element => {
            resolve(element.target.result);
        };
        reader.readAsDataURL(blob);
    }))
        .then(resolve);
});

const isUploadAvailable = async () => {
    let res = null;
    try {
        res = await fetch('https://projects.penguinmod.com/api/v1/projects/canuploadprojects').then(res => res.json());
    } catch {
        // failed to fetch entirely
        return false;
    }
    return res.canUpload;
};

class ShareButton extends React.Component {
    constructor(props) {
        super(props);
        bindAll(this, [
            'handleMessageEvent',
            'wrapperEventHandler',
            'onUploadProject'
        ]);
        this.state = {
            loading: false,
            imageUri: ''
        };
    }
    componentDidMount() {
        window.addEventListener('message', this.wrapperEventHandler);
    }
    componentWillUnmount() {
        window.removeEventListener('message', this.wrapperEventHandler);
    }

    wrapperEventHandler(e) {
        this.handleMessageEvent(e);
    }
    async handleMessageEvent(e) {
        if (!e.origin.startsWith(`https://penguinmod.com`)) {
            return;
        }

        if (!e.data.p4) {
            return;
        }

        const packagerData = e.data.p4;
        if (packagerData.type !== 'validate') {
            return;
        }

        const imageUri = this.state.imageUri;
        e.source.postMessage({
            p4: {
                type: 'image',
                uri: imageUri
            }
        }, e.origin);
        const projectUri = await getProjectUri();
        e.source.postMessage({
            p4: {
                type: 'project',
                uri: projectUri
            }
        }, e.origin);

        e.source.postMessage({
            p4: {
                type: 'finished'
            }
        }, e.origin);
    }
    async onUploadProject() {
        if (this.state.loading) return;
        if (!window.vm) return;
        if (!window.vm.runtime) return;
        if (!window.vm.renderer) return;

        // get the project thumbnail
        await new Promise((resolve) => {
            getProjectThumbnail().then(dataUrl => {
                this.setState({
                    imageUri: dataUrl
                });
                resolve();
            });
            window.vm.renderer.draw(); // force the callback to run
            setTimeout(() => {
                window.vm.renderer.draw(); // force the callback to run
            }, 50);
            setTimeout(() => {
                window.vm.renderer.draw(); // force the callback to run
            }, 100);
        });

        this.setState({
            loading: true
        });
        isUploadAvailable().then(available => {
            this.setState({
                loading: false
            });
            if (!available) {
                // error?
                console.warn('Project Server did not respond. Uploading is not available.');
                alert('Uploading is currently unavailable. Please wait for the server to be restored.');
                return;
            }

            const isEdit = this.props.usernameLoggedIn
                && this.props.extraProjectInfo?.author === this.props.username;

            let editPiece = '';
            let remixPiece = '';
            const id = location.hash.replace('#', '');
            if (this.props.extraProjectInfo?.isRemix) {
                remixPiece = `&remix=${id}`;
            }

            let targetPage = 'upload';
            if (isEdit) {
                targetPage = 'edit';
                editPiece = `&id=${id}`;
            }

            const url = location.origin;
            window.open(`https://penguinmod.com/${targetPage}?name=${this.props.projectTitle}${editPiece}${remixPiece}&external=${url}`, '_blank');
        });
    }
    render() {
        const isRemix = this.props.extraProjectInfo?.isRemix;
        const isEdit = this.props.usernameLoggedIn
            && this.props.extraProjectInfo?.author === this.props.username;
        return (
            <Button
                className={classNames(
                    this.props.className,
                    styles.shareButton,
                    { [styles.shareButtonIsShared]: this.props.isShared },
                    { [styles.disabled]: this.state.loading },
                )}
                onClick={this.onUploadProject}
            >
                <div className={classNames(styles.shareContent)}>
                    {isEdit ? <FormattedMessage
                            defaultMessage="Upload Edits"
                            description="Text for uploading edits for projects on PenguinMod"
                            id="gui.menuBar.pmedit"
                        /> :
                        (isRemix ?
                            <FormattedMessage
                                defaultMessage="Remix"
                                description="Menu bar item for remixing"
                                id="gui.menuBar.remix"
                            /> :
                            <FormattedMessage
                                defaultMessage="Upload"
                                description="Label for project share button"
                                id="gui.menuBar.pmshare"
                            />)}
                    {this.state.loading ? (
                        <img
                            className={classNames(styles.icon)}
                            draggable={false}
                            src={loadingIcon}
                            height={20}
                            width={20}
                        />
                    ) : null}
                </div>
            </Button>
        );
    }
}

ShareButton.propTypes = {
    className: PropTypes.string,
    isShared: PropTypes.bool,
    projectTitle: PropTypes.string,
    extraProjectInfo: PropTypes.shape({
        accepted: PropTypes.bool,
        isRemix: PropTypes.bool,
        remixId: PropTypes.number,
        tooLarge: PropTypes.bool,
        author: PropTypes.string,
        releaseDate: PropTypes.shape(Date),
        isUpdated: PropTypes.bool
    }),
    username: PropTypes.string,
    usernameLoggedIn: PropTypes.bool
};

const mapStateToProps = state => ({
    projectTitle: state.scratchGui.projectTitle,
    extraProjectInfo: state.scratchGui.tw.extraProjectInfo,
    username: state.scratchGui.tw.username,
    usernameLoggedIn: state.scratchGui.tw.usernameLoggedIn
});

// eslint-disable-next-line no-unused-vars
const mapDispatchToProps = dispatch => ({});

export default injectIntl(connect(
    mapStateToProps,
    mapDispatchToProps
)(ShareButton));