element-web/src/components/views/dialogs/CreateRoomDialog.js

230 lines
8.5 KiB
JavaScript
Raw Normal View History

2017-05-08 13:18:31 +03:00
/*
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2020 The Matrix.org Foundation C.I.C.
2017-05-08 13:18:31 +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 React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';
import * as sdk from '../../../index';
import SdkConfig from '../../../SdkConfig';
import withValidation from '../elements/Validation';
import { _t } from '../../../languageHandler';
2019-12-21 00:13:46 +03:00
import {MatrixClientPeg} from '../../../MatrixClientPeg';
import {Key} from "../../../Keyboard";
import SettingsStore from "../../../settings/SettingsStore";
2017-05-08 13:18:31 +03:00
export default createReactClass({
displayName: 'CreateRoomDialog',
2017-05-08 13:18:31 +03:00
propTypes: {
onFinished: PropTypes.func.isRequired,
2017-05-08 13:18:31 +03:00
},
getInitialState() {
const config = SdkConfig.get();
return {
2019-09-20 18:43:14 +03:00
isPublic: false,
isEncrypted: true,
name: "",
2019-09-20 18:41:03 +03:00
topic: "",
2019-09-20 18:46:14 +03:00
alias: "",
2019-09-20 18:44:07 +03:00
detailsOpen: false,
noFederate: config.default_federate === false,
nameIsValid: false,
};
2017-05-08 13:18:31 +03:00
},
_roomCreateOptions() {
const opts = {};
const createOpts = opts.createOpts = {};
createOpts.name = this.state.name;
2019-09-20 18:43:14 +03:00
if (this.state.isPublic) {
createOpts.visibility = "public";
createOpts.preset = "public_chat";
opts.guestAccess = false;
2019-09-20 18:46:14 +03:00
const {alias} = this.state;
const localPart = alias.substr(1, alias.indexOf(":") - 1);
createOpts['room_alias_name'] = localPart;
}
2019-09-20 18:41:03 +03:00
if (this.state.topic) {
createOpts.topic = this.state.topic;
}
if (this.state.noFederate) {
createOpts.creation_content = {'m.federate': false};
}
if (!this.state.isPublic && SettingsStore.isFeatureEnabled("feature_cross_signing")) {
createOpts.encryption = this.state.isEncrypted;
}
return opts;
},
componentDidMount() {
this._detailsRef.addEventListener("toggle", this.onDetailsToggled);
// move focus to first field when showing dialog
this._nameFieldRef.focus();
},
componentWillUnmount() {
this._detailsRef.removeEventListener("toggle", this.onDetailsToggled);
},
2019-10-02 17:26:52 +03:00
_onKeyDown: function(event) {
if (event.key === Key.ENTER) {
2019-10-02 17:26:52 +03:00
this.onOk();
event.preventDefault();
event.stopPropagation();
}
},
onOk: async function() {
const activeElement = document.activeElement;
if (activeElement) {
activeElement.blur();
}
await this._nameFieldRef.validate({allowEmpty: false});
if (this._aliasFieldRef) {
await this._aliasFieldRef.validate({allowEmpty: false});
}
// Validation and state updates are async, so we need to wait for them to complete
// first. Queue a `setState` callback and wait for it to resolve.
await new Promise(resolve => this.setState({}, resolve));
if (this.state.nameIsValid && (!this._aliasFieldRef || this._aliasFieldRef.isValid)) {
this.props.onFinished(true, this._roomCreateOptions());
} else {
let field;
if (!this.state.nameIsValid) {
field = this._nameFieldRef;
} else if (this._aliasFieldRef && !this._aliasFieldRef.isValid) {
field = this._aliasFieldRef;
}
if (field) {
field.focus();
field.validate({ allowEmpty: false, focused: true });
}
}
2017-05-08 13:18:31 +03:00
},
onCancel: function() {
this.props.onFinished(false);
},
onNameChange(ev) {
this.setState({name: ev.target.value});
},
2019-09-20 18:41:03 +03:00
onTopicChange(ev) {
this.setState({topic: ev.target.value});
},
2019-09-20 18:43:14 +03:00
onPublicChange(isPublic) {
this.setState({isPublic});
},
onEncryptedChange(isEncrypted) {
this.setState({isEncrypted});
},
2019-09-20 18:46:14 +03:00
onAliasChange(alias) {
this.setState({alias});
},
onDetailsToggled(ev) {
this.setState({detailsOpen: ev.target.open});
},
onNoFederateChange(noFederate) {
this.setState({noFederate});
},
collectDetailsRef(ref) {
this._detailsRef = ref;
},
async onNameValidate(fieldState) {
const result = await this._validateRoomName(fieldState);
this.setState({nameIsValid: result.valid});
return result;
},
_validateRoomName: withValidation({
rules: [
{
key: "required",
test: async ({ value }) => !!value,
invalid: () => _t("Please enter a name for the room"),
},
],
}),
2017-05-08 13:18:31 +03:00
render: function() {
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
const Field = sdk.getComponent('views.elements.Field');
const LabelledToggleSwitch = sdk.getComponent('views.elements.LabelledToggleSwitch');
2019-09-20 18:46:14 +03:00
const RoomAliasField = sdk.getComponent('views.elements.RoomAliasField');
2019-09-20 18:43:14 +03:00
let privateLabel;
let publicLabel;
2019-09-20 18:46:14 +03:00
let aliasField;
2019-09-20 18:43:14 +03:00
if (this.state.isPublic) {
publicLabel = (<p>{_t("Set a room alias to easily share your room with other people.")}</p>);
2019-09-20 18:46:14 +03:00
const domain = MatrixClientPeg.get().getDomain();
aliasField = (
<div className="mx_CreateRoomDialog_aliasContainer">
<RoomAliasField ref={ref => this._aliasFieldRef = ref} onChange={this.onAliasChange} domain={domain} value={this.state.alias} />
2019-09-20 18:46:14 +03:00
</div>
);
2019-09-20 18:43:14 +03:00
} else {
privateLabel = (<p>{_t("This room is private, and can only be joined by invitation.")}</p>);
}
let e2eeSection;
if (!this.state.isPublic && SettingsStore.isFeatureEnabled("feature_cross_signing")) {
e2eeSection = <React.Fragment>
<LabelledToggleSwitch label={ _t("Enable end-to-end encryption")} onChange={this.onEncryptedChange} value={this.state.isEncrypted} />
{ _t("Bridges and most bots will not function in end-to-end encrypted rooms.") }
</React.Fragment>;
}
2019-09-20 18:43:14 +03:00
const title = this.state.isPublic ? _t('Create a public room') : _t('Create a private room');
2017-05-08 13:18:31 +03:00
return (
<BaseDialog className="mx_CreateRoomDialog" onFinished={this.props.onFinished}
2019-09-20 18:43:14 +03:00
title={title}
2017-05-08 13:18:31 +03:00
>
2019-10-02 17:26:52 +03:00
<form onSubmit={this.onOk} onKeyDown={this._onKeyDown}>
<div className="mx_Dialog_content">
<Field ref={ref => this._nameFieldRef = ref} label={ _t('Name') } onChange={this.onNameChange} onValidate={this.onNameValidate} value={this.state.name} className="mx_CreateRoomDialog_name" />
<Field label={ _t('Topic (optional)') } onChange={this.onTopicChange} value={this.state.topic} />
2019-09-20 18:43:14 +03:00
<LabelledToggleSwitch label={ _t("Make this room public")} onChange={this.onPublicChange} value={this.state.isPublic} />
{ e2eeSection }
2019-09-20 18:43:14 +03:00
{ privateLabel }
{ publicLabel }
2019-09-20 18:46:14 +03:00
{ aliasField }
<details ref={this.collectDetailsRef} className="mx_CreateRoomDialog_details">
<summary className="mx_CreateRoomDialog_details_summary">{ this.state.detailsOpen ? _t('Hide advanced') : _t('Show advanced') }</summary>
<LabelledToggleSwitch label={ _t('Block users on other matrix homeservers from joining this room (This setting cannot be changed later!)')} onChange={this.onNoFederateChange} value={this.state.noFederate} />
</details>
</div>
</form>
<DialogButtons primaryButton={_t('Create Room')}
onPrimaryButtonClick={this.onOk}
onCancel={this.onCancel} />
2017-05-08 13:18:31 +03:00
</BaseDialog>
);
},
});