element-web/src/components/views/dialogs/HostSignupDialog.tsx

158 lines
5.3 KiB
TypeScript
Raw Normal View History

/*
2020-11-27 15:34:48 +03:00
Copyright 2020 The Matrix.org Foundation C.I.C.
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";
import BaseDialog from '../../views/dialogs/BaseDialog';
import ElementProConfirmCloseDialog from './ElementProConfirmCloseDialog';
import ElementProDataConfirmDialog from './ElementProDataConfirmDialog';
import Modal from "../../../Modal";
import SdkConfig from "../../../SdkConfig";
2020-12-18 17:05:57 +03:00
import {_t} from "../../../languageHandler";
import {MatrixClientPeg} from "../../../MatrixClientPeg";
import {OwnProfileStore} from "../../../stores/OwnProfileStore";
2021-01-04 17:05:49 +03:00
import {IPostmessage, IPostmessageResponseData, PostmessageAction} from "./HostSignupDialogTypes";
interface IProps {
requestClose(): void;
}
interface IState {
completed: boolean;
error: string;
}
2021-01-04 17:05:49 +03:00
export default class HostSignupDialog extends React.PureComponent<IProps, IState> {
2020-12-18 17:05:57 +03:00
private iframeRef: React.RefObject<HTMLIFrameElement> = React.createRef();
2021-01-04 17:05:49 +03:00
private readonly hostSignupSetupUrl: string;
constructor(props: IProps) {
super(props);
this.state = {
completed: false,
error: null,
};
2021-01-04 17:05:49 +03:00
this.hostSignupSetupUrl = SdkConfig.get().host_signup.url;
}
2020-12-18 17:05:57 +03:00
private messageHandler = (message: IPostmessage) => {
2021-01-04 17:05:49 +03:00
if (!this.hostSignupSetupUrl.startsWith(message.origin)) {
return;
}
switch (message.data.action) {
2021-01-04 17:05:49 +03:00
case PostmessageAction.HostSignupAccountDetailsRequest:
Modal.createDialog(
ElementProDataConfirmDialog,
{
onFinished: result => {
if (result) {
return this.sendAccountDetails();
}
},
},
);
break;
2020-12-18 17:05:57 +03:00
case PostmessageAction.SetupComplete:
// 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;
2020-12-18 17:05:57 +03:00
case PostmessageAction.CloseDialog:
this.onFinished(true);
break;
}
}
private onFinished = (result: boolean) => {
if (result || this.state.completed) {
// We're done, close
this.props.requestClose();
} else {
Modal.createDialog(
ElementProConfirmCloseDialog,
{
onFinished: result => {
if (result) {
this.props.requestClose();
}
},
},
);
}
}
2020-12-18 17:05:57 +03:00
private sendMessage = (message: IPostmessageResponseData) => {
2021-01-04 17:05:49 +03:00
this.iframeRef.current.contentWindow.postMessage(message, this.hostSignupSetupUrl);
}
private async sendAccountDetails() {
const openIdToken = await MatrixClientPeg.get().getOpenIdToken();
if (!openIdToken || !openIdToken.access_token) {
this.setState({
completed: true,
2020-11-27 15:42:45 +03:00
error: _t("Failed to connect to your homeserver. Please close this dialog and try again."),
});
return;
}
this.sendMessage({
2021-01-04 17:05:49 +03:00
action: PostmessageAction.HostSignupAccountDetails,
account: {
accessToken: await MatrixClientPeg.get().getAccessToken(),
name: OwnProfileStore.instance.displayName,
openIdToken: openIdToken.access_token,
serverName: await MatrixClientPeg.get().getDomain(),
userLocalpart: await MatrixClientPeg.get().getUserIdLocalpart(),
},
});
}
public componentDidMount() {
window.addEventListener("message", this.messageHandler);
}
public componentWillUnmount() {
window.removeEventListener("message", this.messageHandler);
}
public render(): React.ReactNode {
return (
<BaseDialog
2021-01-04 17:05:49 +03:00
className="mx_HostSignupBaseDialog"
onFinished={this.onFinished}
2020-11-27 15:42:45 +03:00
title={_t("Set up your own personal Element host")}
hasCancel={true}
fixedWidth={false}
>
2021-01-04 17:05:49 +03:00
<div className="mx_HostSignupDialog_container">
<iframe
2021-01-04 17:05:49 +03:00
src={this.hostSignupSetupUrl}
2020-12-18 17:05:57 +03:00
ref={this.iframeRef}
sandbox="allow-forms allow-scripts allow-same-origin"
/>
{this.state.error &&
<div>
{this.state.error}
</div>
}
</div>
</BaseDialog>
);
}
}