2020-10-21 15:22:35 +03:00
|
|
|
/*
|
2020-11-27 15:34:48 +03:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-10-21 15:22:35 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as React from "react";
|
2020-12-11 17:25:22 +03:00
|
|
|
import BaseDialog from '../../views/dialogs/BaseDialog';
|
|
|
|
import ConfirmCloseHostingSignupDialog from '../../views/dialogs/ConfirmCloseHostingSignupDialog';
|
|
|
|
import HostingSignupAccountDataConfirmDialog from '../../views/dialogs/HostingSignupAccountDataConfirmDialog';
|
2020-11-13 16:31:54 +03:00
|
|
|
import Modal from "../../../Modal";
|
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2020-11-27 15:42:45 +03:00
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
2020-12-11 17:03:18 +03:00
|
|
|
import { OwnProfileStore } from "../../../stores/OwnProfileStore";
|
2020-10-21 15:22:35 +03:00
|
|
|
|
2020-11-13 16:23:54 +03:00
|
|
|
interface IProps {
|
2020-12-01 14:50:42 +03:00
|
|
|
requestClose(): void;
|
2020-11-13 16:23:54 +03:00
|
|
|
}
|
2020-10-21 15:22:35 +03:00
|
|
|
|
2020-10-28 17:38:47 +03:00
|
|
|
interface IState {
|
2020-12-01 14:50:58 +03:00
|
|
|
completed: boolean;
|
|
|
|
error: string;
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
2020-10-21 15:22:35 +03:00
|
|
|
|
2020-11-13 13:56:42 +03:00
|
|
|
export default class HostingSignupDialog extends React.PureComponent<IProps, IState> {
|
2020-10-28 17:38:47 +03:00
|
|
|
iframeRef;
|
|
|
|
hostingSignupUrl: string;
|
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-11-26 13:40:55 +03:00
|
|
|
completed: false,
|
2020-10-28 17:38:47 +03:00
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.iframeRef = React.createRef();
|
2020-11-13 13:56:42 +03:00
|
|
|
this.hostingSignupUrl = SdkConfig.get().hosting_signup.url;
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private messageHandler = (message) => {
|
|
|
|
if (!this.hostingSignupUrl.startsWith(message.origin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (message.data.action) {
|
2020-12-11 17:03:18 +03:00
|
|
|
case 'element_pro_account_details_request':
|
2020-12-11 17:25:22 +03:00
|
|
|
Modal.createDialog(
|
|
|
|
HostingSignupAccountDataConfirmDialog,
|
|
|
|
{
|
|
|
|
onFinished: result => {
|
|
|
|
if (result) {
|
|
|
|
return this.sendAccountDetails();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2020-11-13 16:36:56 +03:00
|
|
|
break;
|
2020-11-26 13:40:55 +03:00
|
|
|
case 'setup_complete':
|
|
|
|
// Set as completed but let the user close the modal themselves
|
|
|
|
// so they have time to finish reading any information
|
|
|
|
this.setState({
|
|
|
|
completed: true,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'close_dialog':
|
2020-11-13 16:23:54 +03:00
|
|
|
this.onFinished(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private onFinished = (result: boolean) => {
|
2020-11-26 13:40:55 +03:00
|
|
|
if (result || this.state.completed) {
|
2020-11-13 16:23:54 +03:00
|
|
|
// We're done, close
|
|
|
|
this.props.requestClose();
|
|
|
|
} else {
|
|
|
|
Modal.createDialog(
|
2020-12-11 17:25:22 +03:00
|
|
|
ConfirmCloseHostingSignupDialog,
|
2020-11-13 16:23:54 +03:00
|
|
|
{
|
|
|
|
onFinished: result => {
|
|
|
|
if (result) {
|
|
|
|
this.props.requestClose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-12-01 14:51:46 +03:00
|
|
|
);
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendMessage = (message) => {
|
|
|
|
this.iframeRef.contentWindow.postMessage(
|
|
|
|
message,
|
|
|
|
this.hostingSignupUrl,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-11 17:03:18 +03:00
|
|
|
private async sendAccountDetails() {
|
|
|
|
const openIdToken = await MatrixClientPeg.get().getOpenIdToken();
|
|
|
|
if (!openIdToken || !openIdToken.access_token) {
|
2020-10-28 17:38:47 +03:00
|
|
|
this.setState({
|
2020-11-27 15:42:45 +03:00
|
|
|
error: _t("Failed to connect to your homeserver. Please close this dialog and try again."),
|
2020-10-28 17:38:47 +03:00
|
|
|
});
|
2020-12-11 17:03:18 +03:00
|
|
|
return;
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
2020-11-13 16:36:56 +03:00
|
|
|
this.sendMessage({
|
2020-12-11 17:03:18 +03:00
|
|
|
action: 'element_pro_account_details',
|
|
|
|
account: {
|
2020-11-18 18:53:27 +03:00
|
|
|
accessToken: await MatrixClientPeg.get().getAccessToken(),
|
2020-12-11 17:03:18 +03:00
|
|
|
name: OwnProfileStore.instance.displayName,
|
|
|
|
openIdToken: openIdToken.access_token,
|
2020-11-18 18:53:27 +03:00
|
|
|
serverName: await MatrixClientPeg.get().getDomain(),
|
|
|
|
userLocalpart: await MatrixClientPeg.get().getUserIdLocalpart(),
|
|
|
|
},
|
2020-11-13 16:36:56 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:38:47 +03:00
|
|
|
public componentDidMount() {
|
|
|
|
window.addEventListener("message", this.messageHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
window.removeEventListener("message", this.messageHandler);
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:22:35 +03:00
|
|
|
public render(): React.ReactNode {
|
|
|
|
return (
|
2020-11-13 16:23:54 +03:00
|
|
|
<BaseDialog
|
2020-11-13 17:29:35 +03:00
|
|
|
className="mx_HostingSignupBaseDialog"
|
2020-11-13 16:23:54 +03:00
|
|
|
onFinished={this.onFinished}
|
2020-11-27 15:42:45 +03:00
|
|
|
title={_t("Set up your own personal Element host")}
|
2020-11-13 16:23:54 +03:00
|
|
|
hasCancel={true}
|
2020-11-26 14:04:04 +03:00
|
|
|
fixedWidth={false}
|
2020-11-13 16:23:54 +03:00
|
|
|
>
|
|
|
|
<div className="mx_HostingSignupDialog_container">
|
|
|
|
<iframe
|
|
|
|
src={this.hostingSignupUrl}
|
|
|
|
ref={ref => this.iframeRef = ref}
|
|
|
|
/>
|
|
|
|
{this.state.error &&
|
|
|
|
<div>
|
|
|
|
{this.state.error}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</BaseDialog>
|
2020-10-21 15:22:35 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|