Update ServerTypeSelector for registration to use a server config

pull/21833/head
Travis Ralston 2019-05-02 23:02:01 -06:00
parent 636cb8a5cc
commit 6b45e60314
4 changed files with 55 additions and 33 deletions

View File

@ -446,13 +446,6 @@ module.exports = React.createClass({
onEditServerDetailsClick = this.onEditServerDetailsClick; onEditServerDetailsClick = this.onEditServerDetailsClick;
} }
// If the current HS URL is the default HS URL, then we can label it
// with the default HS name (if it exists).
let hsName;
if (this.state.hsUrl === this.props.defaultHsUrl) {
hsName = this.props.defaultServerName;
}
return <RegistrationForm return <RegistrationForm
defaultUsername={this.state.formVals.username} defaultUsername={this.state.formVals.username}
defaultEmail={this.state.formVals.email} defaultEmail={this.state.formVals.email}
@ -462,8 +455,7 @@ module.exports = React.createClass({
onRegisterClick={this.onFormSubmit} onRegisterClick={this.onFormSubmit}
onEditServerDetailsClick={onEditServerDetailsClick} onEditServerDetailsClick={onEditServerDetailsClick}
flows={this.state.flows} flows={this.state.flows}
hsName={hsName} serverConfig={this.props.serverConfig}
hsUrl={this.state.hsUrl}
/>; />;
} }
}, },

View File

@ -26,6 +26,7 @@ import { _t } from '../../../languageHandler';
import SdkConfig from '../../../SdkConfig'; import SdkConfig from '../../../SdkConfig';
import { SAFE_LOCALPART_REGEX } from '../../../Registration'; import { SAFE_LOCALPART_REGEX } from '../../../Registration';
import withValidation from '../elements/Validation'; import withValidation from '../elements/Validation';
import {ValidatedServerConfig} from "../../../utils/AutoDiscoveryUtils";
const FIELD_EMAIL = 'field_email'; const FIELD_EMAIL = 'field_email';
const FIELD_PHONE_NUMBER = 'field_phone_number'; const FIELD_PHONE_NUMBER = 'field_phone_number';
@ -51,11 +52,7 @@ module.exports = React.createClass({
onRegisterClick: PropTypes.func.isRequired, // onRegisterClick(Object) => ?Promise onRegisterClick: PropTypes.func.isRequired, // onRegisterClick(Object) => ?Promise
onEditServerDetailsClick: PropTypes.func, onEditServerDetailsClick: PropTypes.func,
flows: PropTypes.arrayOf(PropTypes.object).isRequired, flows: PropTypes.arrayOf(PropTypes.object).isRequired,
// This is optional and only set if we used a server name to determine serverConfig: PropTypes.instanceOf(ValidatedServerConfig).isRequired,
// the HS URL via `.well-known` discovery. The server name is used
// instead of the HS URL when talking about "your account".
hsName: PropTypes.string,
hsUrl: PropTypes.string,
}, },
getDefaultProps: function() { getDefaultProps: function() {
@ -499,20 +496,14 @@ module.exports = React.createClass({
}, },
render: function() { render: function() {
let yourMatrixAccountText = _t('Create your Matrix account'); let yourMatrixAccountText = _t('Create your Matrix account on %(serverName)s', {
if (this.props.hsName) { serverName: this.props.serverConfig.hsName,
yourMatrixAccountText = _t('Create your Matrix account on %(serverName)s', { });
serverName: this.props.hsName, if (this.props.serverConfig.hsNameIsDifferent) {
// TODO: TravisR - Use tooltip to underline
yourMatrixAccountText = _t('Create your Matrix account on <underlinedServerName />', {}, {
'underlinedServerName': () => <u>{this.props.serverConfig.hsName}</u>,
}); });
} else {
try {
const parsedHsUrl = new URL(this.props.hsUrl);
yourMatrixAccountText = _t('Create your Matrix account on %(serverName)s', {
serverName: parsedHsUrl.hostname,
});
} catch (e) {
// ignore
}
} }
let editLink = null; let editLink = null;

View File

@ -19,6 +19,8 @@ import PropTypes from 'prop-types';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import sdk from '../../../index'; import sdk from '../../../index';
import classnames from 'classnames'; import classnames from 'classnames';
import {ValidatedServerConfig} from "../../../utils/AutoDiscoveryUtils";
import {makeType} from "../../../utils/TypeUtils";
const MODULAR_URL = 'https://modular.im/?utm_source=riot-web&utm_medium=web&utm_campaign=riot-web-authentication'; const MODULAR_URL = 'https://modular.im/?utm_source=riot-web&utm_medium=web&utm_campaign=riot-web-authentication';
@ -32,8 +34,13 @@ export const TYPES = {
label: () => _t('Free'), label: () => _t('Free'),
logo: () => <img src={require('../../../../res/img/matrix-org-bw-logo.svg')} />, logo: () => <img src={require('../../../../res/img/matrix-org-bw-logo.svg')} />,
description: () => _t('Join millions for free on the largest public server'), description: () => _t('Join millions for free on the largest public server'),
hsUrl: 'https://matrix.org', serverConfig: makeType(ValidatedServerConfig, {
isUrl: 'https://vector.im', hsUrl: "https://matrix.org",
hsName: "matrix.org",
hsNameIsDifferent: false,
isUrl: "https://vector.im",
identityEnabled: true,
}),
}, },
PREMIUM: { PREMIUM: {
id: PREMIUM, id: PREMIUM,
@ -44,6 +51,7 @@ export const TYPES = {
{sub} {sub}
</a>, </a>,
}), }),
identityServerUrl: "https://vector.im",
}, },
ADVANCED: { ADVANCED: {
id: ADVANCED, id: ADVANCED,
@ -56,10 +64,11 @@ export const TYPES = {
}, },
}; };
export function getTypeFromHsUrl(hsUrl) { export function getTypeFromServerConfig(config) {
const {hsUrl} = config;
if (!hsUrl) { if (!hsUrl) {
return null; return null;
} else if (hsUrl === TYPES.FREE.hsUrl) { } else if (hsUrl === TYPES.FREE.serverConfig.hsUrl) {
return FREE; return FREE;
} else if (new URL(hsUrl).hostname.endsWith('.modular.im')) { } else if (new URL(hsUrl).hostname.endsWith('.modular.im')) {
// This is an unlikely case to reach, as Modular defaults to hiding the // This is an unlikely case to reach, as Modular defaults to hiding the
@ -76,7 +85,7 @@ export default class ServerTypeSelector extends React.PureComponent {
selected: PropTypes.string, selected: PropTypes.string,
// Handler called when the selected type changes. // Handler called when the selected type changes.
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
} };
constructor(props) { constructor(props) {
super(props); super(props);
@ -106,7 +115,7 @@ export default class ServerTypeSelector extends React.PureComponent {
e.stopPropagation(); e.stopPropagation();
const type = e.currentTarget.dataset.id; const type = e.currentTarget.dataset.id;
this.updateSelectedType(type); this.updateSelectedType(type);
} };
render() { render() {
const AccessibleButton = sdk.getComponent('elements.AccessibleButton'); const AccessibleButton = sdk.getComponent('elements.AccessibleButton');

30
src/utils/TypeUtils.js Normal file
View File

@ -0,0 +1,30 @@
/*
Copyright 2019 New Vector Ltd
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.
*/
/**
* Creates a class of a given type using the objects defined. This
* is a stopgap function while we don't have TypeScript interfaces.
* In future, we'd define the `type` as an interface and just cast
* it instead of cheating like we are here.
* @param {Type} Type The type of class to construct.
* @param {*} opts The options (properties) to set on the object.
* @returns {*} The created object.
*/
export function makeType(Type: any, opts: any) {
const c = new Type();
Object.assign(c, opts);
return c;
}