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

181 lines
6.6 KiB
TypeScript
Raw Normal View History

/*
2021-01-05 15:29:48 +03:00
Copyright 2021 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 Modal from "../../../Modal";
import QuestionDialog from './QuestionDialog';
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;
loadIframe: boolean;
}
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,
loadIframe: false,
};
2021-01-04 17:05:49 +03:00
this.hostSignupSetupUrl = SdkConfig.get().host_signup.url;
}
private messageHandler = async (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:
await 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(
QuestionDialog,
{
title: _t("Confirm Abort Of Host Creation"),
description: _t(
"Are you sure you wish to abort creation of the host? The process cannot be continued.",
),
button: _t("Abort"),
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(),
},
});
}
private loadIframe = () => {
window.addEventListener("message", this.messageHandler);
this.setState({
loadIframe: true,
});
}
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}
title=""
hasCancel={true}
>
2021-01-04 17:05:49 +03:00
<div className="mx_HostSignupDialog_container">
{this.state.loadIframe &&
<iframe
src={this.hostSignupSetupUrl}
ref={this.iframeRef}
sandbox="allow-forms allow-scripts allow-same-origin"
/>
}
{!this.state.loadIframe &&
<div className="mx_HostSignupDialog_info">
<img
alt="image of planet"
src={require("../../../../res/img/host_signup.png")}
/>
<div className="mx_HostSignupDialog_content">
<h1>Unlock the power of Element</h1>
<p>
Congratulations! You taken your first steps into unlocking the full power of&nbsp;
the Element app. In a few minutes, you'll be able to see how powerful our&nbsp;
Matrix services are and take control of your conversation data.
</p>
</div>
<div>
<button onClick={this.props.requestClose}>Maybe later</button>
<button onClick={this.loadIframe} className="mx_Dialog_primary">
Lets get started
</button>
</div>
</div>
}
{this.state.error &&
<div>
{this.state.error}
</div>
}
</div>
</BaseDialog>
);
}
}