2020-10-21 15:22:35 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 Element
|
|
|
|
|
|
|
|
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-11-13 16:31:54 +03:00
|
|
|
import * as sdk from '../../../index';
|
|
|
|
import Modal from "../../../Modal";
|
|
|
|
import SdkConfig from "../../../SdkConfig";
|
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2020-10-21 15:22:35 +03:00
|
|
|
|
2020-11-13 16:23:54 +03:00
|
|
|
interface IProps {
|
|
|
|
requestClose(): void,
|
|
|
|
}
|
2020-10-21 15:22:35 +03:00
|
|
|
|
2020-10-28 17:38:47 +03:00
|
|
|
interface IState {
|
|
|
|
error: string,
|
|
|
|
}
|
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 = {
|
|
|
|
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) {
|
|
|
|
case 'openid_credentials_request':
|
|
|
|
// noinspection JSIgnoredPromiseFromCall
|
|
|
|
this.fetchOpenIDToken();
|
|
|
|
break;
|
2020-11-13 16:23:54 +03:00
|
|
|
case 'setup_complete':
|
|
|
|
this.onFinished(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private onFinished = (result: boolean) => {
|
|
|
|
if (result) {
|
|
|
|
// We're done, close
|
|
|
|
this.props.requestClose();
|
|
|
|
} else {
|
|
|
|
const ConfirmDialog = sdk.getComponent('views.dialogs.ConfirmCloseHostingSignupDialog');
|
|
|
|
Modal.createDialog(
|
|
|
|
ConfirmDialog,
|
|
|
|
{
|
|
|
|
onFinished: result => {
|
|
|
|
if (result) {
|
|
|
|
// TODO call an API endpoint to clean up?
|
|
|
|
this.props.requestClose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendMessage = (message) => {
|
|
|
|
this.iframeRef.contentWindow.postMessage(
|
|
|
|
message,
|
|
|
|
this.hostingSignupUrl,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private async fetchOpenIDToken() {
|
|
|
|
const token = await MatrixClientPeg.get().getOpenIdToken();
|
|
|
|
if (token && token.access_token) {
|
|
|
|
this.sendMessage({
|
|
|
|
action: 'openid_credentials',
|
|
|
|
tokenData: token,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
error: "Failed to connect to your homeserver. Please close this dialog and try again.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-13 16:23:54 +03:00
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
2020-10-21 15:22:35 +03:00
|
|
|
return (
|
2020-11-13 16:23:54 +03:00
|
|
|
<BaseDialog
|
|
|
|
onFinished={this.onFinished}
|
|
|
|
title="Set up your own personal Element host"
|
|
|
|
hasCancel={true}
|
|
|
|
>
|
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|