2019-01-03 17:14:25 +03:00
|
|
|
import React, { Component } from 'react';
|
2020-10-26 02:32:04 +03:00
|
|
|
import { connect, ResolveThunks } from 'react-redux';
|
2019-01-10 20:23:33 +03:00
|
|
|
import { Container, Form } from 'devui';
|
2020-10-26 02:32:04 +03:00
|
|
|
import {
|
|
|
|
JSONSchema7Definition,
|
|
|
|
JSONSchema7Type,
|
|
|
|
JSONSchema7TypeName,
|
|
|
|
} from 'json-schema';
|
|
|
|
import { ConnectionType, saveSocketSettings } from '../../actions';
|
|
|
|
import { StoreState } from '../../reducers';
|
|
|
|
import { ConnectionOptions } from '../../reducers/connection';
|
|
|
|
import { IChangeEvent, ISubmitEvent } from '@rjsf/core';
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
declare module 'json-schema' {
|
|
|
|
export interface JSONSchema7 {
|
|
|
|
enumNames?: JSONSchema7Type[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Schema {
|
|
|
|
type: JSONSchema7TypeName;
|
|
|
|
required?: string[];
|
|
|
|
properties: {
|
|
|
|
[key: string]: JSONSchema7Definition;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultSchema: Schema = {
|
2019-01-03 17:14:25 +03:00
|
|
|
type: 'object',
|
|
|
|
required: [],
|
|
|
|
properties: {
|
|
|
|
type: {
|
|
|
|
title: 'Connection settings (for getting reports and remote debugging)',
|
|
|
|
type: 'string',
|
2021-09-06 21:29:41 +03:00
|
|
|
enum: ['disabled', 'custom'],
|
|
|
|
enumNames: ['no remote connection', 'use local (custom) server'],
|
2019-01-03 17:14:25 +03:00
|
|
|
},
|
|
|
|
hostname: {
|
2020-08-08 23:26:39 +03:00
|
|
|
type: 'string',
|
2019-01-03 17:14:25 +03:00
|
|
|
},
|
|
|
|
port: {
|
2020-08-08 23:26:39 +03:00
|
|
|
type: 'number',
|
2019-01-03 17:14:25 +03:00
|
|
|
},
|
|
|
|
secure: {
|
2020-08-08 23:26:39 +03:00
|
|
|
type: 'boolean',
|
|
|
|
},
|
|
|
|
},
|
2019-01-03 17:14:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const uiSchema = {
|
|
|
|
type: {
|
2020-08-08 23:26:39 +03:00
|
|
|
'ui:widget': 'radio',
|
|
|
|
},
|
2019-01-03 17:14:25 +03:00
|
|
|
};
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
type StateProps = ReturnType<typeof mapStateToProps>;
|
|
|
|
type DispatchProps = ResolveThunks<typeof actionCreators>;
|
|
|
|
type Props = StateProps & DispatchProps;
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
interface FormData extends ConnectionOptions {
|
|
|
|
readonly type: ConnectionType;
|
|
|
|
}
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
interface State {
|
|
|
|
readonly formData: FormData;
|
|
|
|
readonly type: ConnectionType;
|
|
|
|
readonly schema: Schema;
|
|
|
|
readonly changed: boolean | undefined;
|
|
|
|
}
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
export class Connection extends Component<Props, State> {
|
|
|
|
setFormData = (type: ConnectionType, changed?: boolean) => {
|
|
|
|
let schema: Schema;
|
2019-01-03 17:14:25 +03:00
|
|
|
if (type !== 'custom') {
|
|
|
|
schema = {
|
|
|
|
type: 'object',
|
2020-08-08 23:26:39 +03:00
|
|
|
properties: { type: defaultSchema.properties.type },
|
2019-01-03 17:14:25 +03:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
schema = defaultSchema;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
formData: {
|
|
|
|
type,
|
2020-08-08 23:26:39 +03:00
|
|
|
...this.props.options,
|
2019-01-03 17:14:25 +03:00
|
|
|
},
|
|
|
|
type,
|
|
|
|
schema,
|
2020-08-08 23:26:39 +03:00
|
|
|
changed,
|
2019-01-03 17:14:25 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
state: State = this.setFormData(this.props.type);
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps: Props, nextState: State) {
|
|
|
|
return this.state !== nextState;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
|
|
|
if (this.props.options !== nextProps.options) {
|
|
|
|
this.setState({
|
|
|
|
formData: { ...nextProps.options, type: nextProps.type },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSave = (data: ISubmitEvent<FormData>) => {
|
|
|
|
this.props.saveSettings(data.formData);
|
|
|
|
this.setState({ changed: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleChange = (data: IChangeEvent<FormData>) => {
|
2019-01-03 17:14:25 +03:00
|
|
|
const formData = data.formData;
|
|
|
|
const type = formData.type;
|
|
|
|
if (type !== this.state.type) {
|
|
|
|
this.setState(this.setFormData(type, true));
|
|
|
|
} else if (!this.state.changed) {
|
|
|
|
this.setState({ changed: true, formData });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const type = this.state.type || 'disabled';
|
|
|
|
const changed = this.state.changed;
|
|
|
|
const disabled = type === 'disabled';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<Form
|
|
|
|
primaryButton={changed}
|
|
|
|
noSubmit={disabled && !changed}
|
|
|
|
submitText={disabled ? 'Disconnect' : 'Connect'}
|
|
|
|
formData={this.state.formData}
|
|
|
|
schema={this.state.schema}
|
|
|
|
uiSchema={uiSchema}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onSubmit={this.handleSave}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
const mapStateToProps = (state: StoreState) => state.connection;
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
const actionCreators = {
|
|
|
|
saveSettings: saveSocketSettings,
|
|
|
|
};
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
export default connect(mapStateToProps, actionCreators)(Connection);
|