2020-10-21 15:22:35 +03:00
|
|
|
/*
|
2021-01-05 15:29:48 +03:00
|
|
|
Copyright 2021 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";
|
2021-01-15 16:32:30 +03:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
2020-11-13 16:31:54 +03:00
|
|
|
import Modal from "../../../Modal";
|
2021-01-12 18:08:42 +03:00
|
|
|
import PersistedElement from "../elements/PersistedElement";
|
2021-01-05 15:47:39 +03:00
|
|
|
import QuestionDialog from './QuestionDialog';
|
2020-11-13 16:31:54 +03:00
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2020-12-18 17:05:57 +03:00
|
|
|
import {_t} from "../../../languageHandler";
|
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2021-01-14 13:20:42 +03:00
|
|
|
import {HostSignupStore} from "../../../stores/HostSignupStore";
|
2020-12-18 17:05:57 +03:00
|
|
|
import {OwnProfileStore} from "../../../stores/OwnProfileStore";
|
2021-01-15 16:32:30 +03:00
|
|
|
import {
|
|
|
|
IHostSignupConfig,
|
|
|
|
IPostmessage,
|
|
|
|
IPostmessageResponseData,
|
|
|
|
PostmessageAction,
|
|
|
|
} from "./HostSignupDialogTypes";
|
2020-10-21 15:22:35 +03:00
|
|
|
|
2021-01-14 12:53:20 +03:00
|
|
|
interface IProps {}
|
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;
|
2021-01-12 15:08:09 +03:00
|
|
|
minimized: boolean;
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
2020-10-21 15:22:35 +03:00
|
|
|
|
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-15 16:32:30 +03:00
|
|
|
private readonly config: IHostSignupConfig;
|
2020-10-28 17:38:47 +03:00
|
|
|
|
|
|
|
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,
|
2021-01-12 15:08:09 +03:00
|
|
|
minimized: false,
|
2020-10-28 17:38:47 +03:00
|
|
|
};
|
|
|
|
|
2021-01-15 16:32:30 +03:00
|
|
|
this.config = SdkConfig.get().hostSignup;
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-11 16:47:21 +03:00
|
|
|
private messageHandler = async (message: IPostmessage) => {
|
2021-01-15 16:32:30 +03:00
|
|
|
if (!this.config.url.startsWith(message.origin)) {
|
2020-10-28 17:38:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (message.data.action) {
|
2021-01-04 17:05:49 +03:00
|
|
|
case PostmessageAction.HostSignupAccountDetailsRequest:
|
2021-01-29 16:55:14 +03:00
|
|
|
this.onAccountDetailsRequest();
|
2020-11-13 16:36:56 +03:00
|
|
|
break;
|
2021-01-14 12:53:20 +03:00
|
|
|
case PostmessageAction.Maximize:
|
2021-02-04 19:30:12 +03:00
|
|
|
this.setState({
|
|
|
|
minimized: false,
|
|
|
|
});
|
2021-01-14 12:53:20 +03:00
|
|
|
break;
|
2021-01-12 15:08:09 +03:00
|
|
|
case PostmessageAction.Minimize:
|
2021-02-04 19:30:12 +03:00
|
|
|
this.setState({
|
|
|
|
minimized: true,
|
|
|
|
});
|
2021-01-12 15:08:09 +03:00
|
|
|
break;
|
2020-12-18 17:05:57 +03:00
|
|
|
case PostmessageAction.SetupComplete:
|
2020-11-26 13:40:55 +03:00
|
|
|
this.setState({
|
|
|
|
completed: true,
|
|
|
|
});
|
|
|
|
break;
|
2020-12-18 17:05:57 +03:00
|
|
|
case PostmessageAction.CloseDialog:
|
2021-01-15 16:32:30 +03:00
|
|
|
return this.closeDialog();
|
2020-11-13 16:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:08:09 +03:00
|
|
|
private maximizeDialog = () => {
|
2021-02-04 19:30:12 +03:00
|
|
|
this.setState({
|
|
|
|
minimized: false,
|
|
|
|
});
|
|
|
|
// Send this action to the iframe so it can act accordingly
|
|
|
|
this.sendMessage({
|
|
|
|
action: PostmessageAction.Maximize,
|
|
|
|
});
|
2021-01-12 15:08:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private minimizeDialog = () => {
|
|
|
|
this.setState({
|
|
|
|
minimized: true,
|
|
|
|
});
|
2021-02-04 19:30:12 +03:00
|
|
|
// Send this action to the iframe so it can act accordingly
|
|
|
|
this.sendMessage({
|
|
|
|
action: PostmessageAction.Minimize,
|
|
|
|
});
|
2021-01-14 12:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private closeDialog = async () => {
|
|
|
|
window.removeEventListener("message", this.messageHandler);
|
|
|
|
// Ensure we destroy the host signup persisted element
|
|
|
|
PersistedElement.destroyElement("host_signup");
|
|
|
|
// Finally clear the flag in
|
2021-01-14 13:20:42 +03:00
|
|
|
return HostSignupStore.instance.setHostSignupActive(false);
|
2021-01-12 15:08:09 +03:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:32:30 +03:00
|
|
|
private onCloseClick = async () => {
|
|
|
|
if (this.state.completed) {
|
2020-11-13 16:23:54 +03:00
|
|
|
// We're done, close
|
2021-01-14 12:53:20 +03:00
|
|
|
return this.closeDialog();
|
2020-11-13 16:23:54 +03:00
|
|
|
} else {
|
|
|
|
Modal.createDialog(
|
2021-01-05 15:47:39 +03:00
|
|
|
QuestionDialog,
|
2020-11-13 16:23:54 +03:00
|
|
|
{
|
2021-01-05 15:47:39 +03:00
|
|
|
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"),
|
2020-11-13 16:23:54 +03:00
|
|
|
onFinished: result => {
|
|
|
|
if (result) {
|
2021-01-14 12:53:20 +03:00
|
|
|
return this.closeDialog();
|
2020-11-13 16:23:54 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-12-01 14:51:46 +03:00
|
|
|
);
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:05:57 +03:00
|
|
|
private sendMessage = (message: IPostmessageResponseData) => {
|
2021-01-15 16:32:30 +03:00
|
|
|
this.iframeRef.current.contentWindow.postMessage(message, this.config.url);
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
|
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-12-15 11:50:38 +03:00
|
|
|
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."),
|
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({
|
2021-01-04 17:05:49 +03:00
|
|
|
action: PostmessageAction.HostSignupAccountDetails,
|
2020-12-11 17:03:18 +03:00
|
|
|
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(),
|
2021-01-29 16:55:14 +03:00
|
|
|
termsAccepted: true,
|
2020-11-18 18:53:27 +03:00
|
|
|
},
|
2020-11-13 16:36:56 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:55:14 +03:00
|
|
|
private onAccountDetailsDialogFinished = async (result) => {
|
|
|
|
if (result) {
|
|
|
|
return this.sendAccountDetails();
|
|
|
|
}
|
|
|
|
return this.closeDialog();
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:55:14 +03:00
|
|
|
private onAccountDetailsRequest = () => {
|
2021-02-09 13:23:27 +03:00
|
|
|
const termsDialog = this.config.termsDialog;
|
|
|
|
const textComponent = (
|
|
|
|
<>
|
|
|
|
<p>
|
|
|
|
{termsDialog.text}
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
{_t("Learn more in our")}
|
|
|
|
<a href={termsDialog.privacyPolicy.href} target="_blank" rel="noreferrer noopener">
|
|
|
|
{termsDialog.privacyPolicy.text}
|
|
|
|
</a>,
|
|
|
|
<a href={termsDialog.termsOfService.href} target="_blank" rel="noreferrer noopener">
|
|
|
|
{termsDialog.termsOfService.text}
|
|
|
|
</a> {_t("and")}
|
|
|
|
<a href={termsDialog.cookiePolicy.href} target="_blank" rel="noreferrer noopener">
|
|
|
|
{termsDialog.cookiePolicy.text}
|
|
|
|
</a>.
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
);
|
2021-01-15 16:32:30 +03:00
|
|
|
Modal.createDialog(
|
|
|
|
QuestionDialog,
|
|
|
|
{
|
2021-02-09 13:23:27 +03:00
|
|
|
title: termsDialog.title,
|
|
|
|
description: textComponent,
|
|
|
|
button: termsDialog.acceptText,
|
2021-01-29 16:55:14 +03:00
|
|
|
onFinished: this.onAccountDetailsDialogFinished,
|
2021-01-15 16:32:30 +03:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:55:14 +03:00
|
|
|
public componentDidMount() {
|
|
|
|
window.addEventListener("message", this.messageHandler);
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:38:47 +03:00
|
|
|
public componentWillUnmount() {
|
2021-01-14 13:20:42 +03:00
|
|
|
if (HostSignupStore.instance.isHostSignupActive) {
|
2021-01-14 12:53:20 +03:00
|
|
|
// Run the close dialog actions if we're still active, otherwise good to go
|
|
|
|
return this.closeDialog();
|
|
|
|
}
|
2020-10-28 17:38:47 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:22:35 +03:00
|
|
|
public render(): React.ReactNode {
|
|
|
|
return (
|
2021-01-14 12:53:20 +03:00
|
|
|
<div className="mx_HostSignup_persisted">
|
2021-01-12 18:08:42 +03:00
|
|
|
<PersistedElement key="host_signup" persistKey="host_signup">
|
2021-01-14 12:53:20 +03:00
|
|
|
<div className={this.state.minimized ? "" : "mx_Dialog_wrapper"}>
|
2021-01-15 16:32:30 +03:00
|
|
|
<div className={["mx_Dialog",
|
|
|
|
this.state.minimized ? "mx_HostSignupDialog_minimized" : "mx_HostSignupDialog"].join(" ")
|
2021-01-14 12:53:20 +03:00
|
|
|
}>
|
2021-01-29 16:55:14 +03:00
|
|
|
{this.state.minimized &&
|
|
|
|
<div className="mx_Dialog_header mx_Dialog_headerWithButton">
|
|
|
|
<div className="mx_Dialog_title">
|
|
|
|
{this.config.minimizedDialogTitle}
|
|
|
|
</div>
|
|
|
|
<AccessibleButton
|
|
|
|
className="mx_HostSignup_maximize_button"
|
|
|
|
onClick={this.maximizeDialog}
|
|
|
|
aria-label={_t("Maximize dialog")}
|
2021-01-15 16:32:30 +03:00
|
|
|
/>
|
2021-01-29 16:55:14 +03:00
|
|
|
</div>
|
2021-01-12 18:08:42 +03:00
|
|
|
}
|
2021-01-29 16:55:14 +03:00
|
|
|
{!this.state.minimized &&
|
|
|
|
<div className="mx_Dialog_header mx_Dialog_headerWithCancel">
|
|
|
|
<AccessibleButton
|
2021-02-05 00:09:58 +03:00
|
|
|
onClick={this.minimizeDialog} className="mx_HostSignup_minimize_button"
|
|
|
|
aria-label={_t("Minimize dialog")}
|
|
|
|
/>
|
|
|
|
<AccessibleButton
|
2021-01-29 16:55:14 +03:00
|
|
|
onClick={this.onCloseClick} className="mx_Dialog_cancelButton"
|
|
|
|
aria-label={_t("Close dialog")}
|
|
|
|
/>
|
2021-01-12 15:08:09 +03:00
|
|
|
</div>
|
2021-01-12 18:08:42 +03:00
|
|
|
}
|
|
|
|
{this.state.error &&
|
2021-01-12 15:08:09 +03:00
|
|
|
<div>
|
2021-01-12 18:08:42 +03:00
|
|
|
{this.state.error}
|
2021-01-12 15:08:09 +03:00
|
|
|
</div>
|
2021-01-12 18:08:42 +03:00
|
|
|
}
|
2021-01-29 16:55:14 +03:00
|
|
|
{!this.state.error &&
|
|
|
|
<iframe
|
|
|
|
src={this.config.url}
|
|
|
|
ref={this.iframeRef}
|
2021-02-09 13:29:23 +03:00
|
|
|
sandbox="allow-forms allow-scripts allow-same-origin allow-popups"
|
2021-01-29 16:55:14 +03:00
|
|
|
/>
|
|
|
|
}
|
2021-01-12 18:08:42 +03:00
|
|
|
</div>
|
2021-01-14 12:53:20 +03:00
|
|
|
</div>
|
2021-01-12 18:08:42 +03:00
|
|
|
</PersistedElement>
|
2021-01-12 15:08:09 +03:00
|
|
|
</div>
|
2020-10-21 15:22:35 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|