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

300 lines
13 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";
2021-01-15 16:32:30 +03:00
import AccessibleButton from "../elements/AccessibleButton";
import Modal from "../../../Modal";
2021-01-12 18:08:42 +03:00
import PersistedElement from "../elements/PersistedElement";
import QuestionDialog from './QuestionDialog';
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";
2021-01-14 12:53:20 +03:00
interface IProps {}
interface IState {
completed: boolean;
error: string;
loadIframe: boolean;
2021-01-12 15:08:09 +03:00
minimized: boolean;
2021-01-15 16:32:30 +03:00
termsAccepted: 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-15 16:32:30 +03:00
private readonly config: IHostSignupConfig;
constructor(props: IProps) {
super(props);
this.state = {
completed: false,
error: null,
loadIframe: false,
2021-01-12 15:08:09 +03:00
minimized: false,
2021-01-15 16:32:30 +03:00
termsAccepted: false,
};
2021-01-15 16:32:30 +03:00
this.config = SdkConfig.get().hostSignup;
}
private messageHandler = async (message: IPostmessage) => {
2021-01-15 16:32:30 +03:00
if (!this.config.url.startsWith(message.origin)) {
return;
}
switch (message.data.action) {
2021-01-04 17:05:49 +03:00
case PostmessageAction.HostSignupAccountDetailsRequest:
await this.sendAccountDetails();
break;
2021-01-14 12:53:20 +03:00
case PostmessageAction.Maximize:
this.maximizeDialog();
break;
2021-01-12 15:08:09 +03:00
case PostmessageAction.Minimize:
this.minimizeDialog();
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:
2021-01-15 16:32:30 +03:00
return this.closeDialog();
}
}
2021-01-12 15:08:09 +03:00
private maximizeDialog = () => {
2021-01-15 16:32:30 +03:00
if (this.state.minimized) {
this.setState({
minimized: false,
});
}
2021-01-12 15:08:09 +03:00
}
private minimizeDialog = () => {
this.setState({
minimized: true,
});
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) {
// We're done, close
2021-01-14 12:53:20 +03:00
return this.closeDialog();
} 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) {
2021-01-14 12:53:20 +03:00
return this.closeDialog();
}
},
},
);
}
}
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);
}
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(),
2021-01-15 16:32:30 +03:00
termsAccepted: this.state.termsAccepted,
},
});
}
private loadIframe = () => {
window.addEventListener("message", this.messageHandler);
this.setState({
loadIframe: true,
});
}
2021-01-15 16:32:30 +03:00
private onStartClick = () => {
Modal.createDialog(
QuestionDialog,
{
title: this.config.termsDialog.title,
description: this.config.termsDialog.text,
button: this.config.termsDialog.acceptText,
onFinished: result => {
if (result) {
this.setState({
termsAccepted: true,
});
this.loadIframe();
}
},
},
);
}
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();
}
}
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-12 18:08:42 +03:00
{this.state.loadIframe &&
2021-01-15 16:32:30 +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")}
/>
</div>
}
{!this.state.minimized &&
<div className="mx_Dialog_header mx_Dialog_headerWithCancel">
<AccessibleButton
onClick={this.onCloseClick} className="mx_Dialog_cancelButton"
aria-label={_t("Close dialog")}
/>
</div>
}
<iframe
src={this.config.url}
ref={this.iframeRef}
sandbox="allow-forms allow-scripts allow-same-origin"
/>
</>
2021-01-12 18:08:42 +03:00
}
{!this.state.loadIframe &&
<div className="mx_HostSignupDialog_info">
2021-01-15 16:32:30 +03:00
{this.config.info.image &&
<img
alt={this.config.info.image.alt}
src={this.config.info.image.src}
/>
2021-01-14 12:53:20 +03:00
}
2021-01-15 16:32:30 +03:00
<div className="mx_HostSignupDialog_content_top">
<h1 className="mx_HostSignupDialog_text_dark">
{this.config.info.title}
</h1>
{this.config.info.additionalParagraphs &&
<div className="mx_HostSignupDialog_paragraphs">
{this.config.info.additionalParagraphs.map((para, index) => {
return <p className="mx_HostSignupDialog_text_light" key={index}>
{para}
</p>;
})}
</div>
}
{this.config.info.additionalInfoLink &&
<p><small>
<a href={this.config.info.additionalInfoLink.href} target="_blank"
rel="noopener noreferrer"
title={this.config.info.additionalInfoLink.text}
>
{this.config.info.additionalInfoLink.text}
</a>
</small></p>
}
2021-01-12 18:08:42 +03:00
</div>
2021-01-15 16:32:30 +03:00
<div className="mx_HostSignupDialog_buttons">
{/*TODO: what about accessibility? the signup flow is possibly not reader optimized*/}
<button onClick={this.closeDialog}>{this.config.info.cancelText}</button>
<button onClick={this.onStartClick} className="mx_Dialog_primary">
{this.config.info.continueText}
2021-01-12 18:08:42 +03:00
</button>
</div>
2021-01-15 16:32:30 +03:00
{this.config.info.footer &&
<div className="mx_HostSignupDialog_text_light">
<small>
<p>
{this.config.info.footer.image &&
<img
alt={this.config.info.footer.image.alt}
src={this.config.info.footer.image.src}
/>
}
{this.config.info.footer.text}
</p>
</small>
</div>
}
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
}
</div>
2021-01-14 12:53:20 +03:00
{!this.state.minimized &&
2021-01-15 16:32:30 +03:00
<div className="mx_Dialog_background mx_HostSignupDialog_background" />
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>
);
}
}