import 'react-ace' import 'brace/mode/json' import 'brace/mode/text' import 'brace/mode/xml' import 'brace/theme/monokai' import * as React from 'react' import * as q from '../../../../../backend/src/Model' import { Button, FormControlLabel, Radio, RadioGroup, TextField, IconButton, FormControl, InputLabel, Input, Checkbox, MenuItem, Tooltip, } from '@material-ui/core' // tslint:disable-next-line import { default as AceEditor } from 'react-ace' import { AppState } from '../../../reducers' import History from '../History' import Message from './Model/Message' import Navigation from '@material-ui/icons/Navigation' import Clear from '@material-ui/icons/Clear' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { publishActions } from '../../../actions' interface Props { node?: q.TreeNode connectionId?: string topic?: string payload?: string actions: typeof publishActions emptyPayload: boolean retain: boolean editorMode: string qos: 0 | 1 | 2 } interface State { history: Message[] } class Publish extends React.Component { constructor(props: any) { super(props) this.state = { history: [] } } private updatePayload = (payload: string, event?: any) => { this.props.actions.setPayload(payload) } private updateTopic = (e: React.ChangeEvent) => { this.props.actions.setTopic(e.target.value) } private updateMode = (e: React.ChangeEvent<{}>, value: string) => { this.props.actions.setEditorMode(value) } private publish = (e: React.MouseEvent) => { e.stopPropagation() if (!this.props.connectionId) { return } this.props.actions.publish(this.props.connectionId) const topic = this.props.topic || '' const payload = this.props.payload if (this.props.connectionId && topic) { this.addMessageToHistory(topic, payload) } } private addMessageToHistory(topic: string, payload?: string) { // Remove duplicates let filteredHistory = this.state.history.filter(e => e.payload !== payload || e.topic !== topic) filteredHistory = filteredHistory.slice(-7) const history: Message[] = [...filteredHistory, { topic, payload, sent: new Date() }] this.setState({ history }) } public render() { return (
{this.topic()} {this.editor()} {this.history()}
) } private clearTopic = () => { this.props.actions.setTopic('') } private topic() { const topicStr = this.props.topic || '' return (
Topic } endAdornment={} onBlur={this.onTopicBlur} onChange={this.updateTopic} multiline={true} placeholder="example/topic" />
) } private onTopicBlur = (e: React.FocusEvent) => { if (!e.target.value) { this.props.actions.setTopic(undefined) } } private editorOptions = { showLineNumbers: false, tabSize: 2, } private publishButton() { return ( ) } private editorMode() { return (
{this.renderEditorModeSelection()}
{this.publishButton()}
) } private renderEditorModeSelection() { if (this.props.emptyPayload) { return null } const labelStyle = { margin: '0 8px 0 8px' } return ( } label="raw" labelPlacement="top" /> } label="xml" labelPlacement="top" /> } label="json" labelPlacement="top" /> ) } private onChangeQoS = (event: React.ChangeEvent) => { const value = parseInt(event.target.value, 10) if (value !== 0 && value !== 1 && value !== 2) { return } this.props.actions.setQoS(value) } private publishMode() { const labelStyle = { margin: '0 8px 0 8px' } const itemStyle = { padding: '0' } const tooltipStyle = { textAlign: 'center' as 'center', width: '100%' } const qosSelect = (
0
1
2
) return (
} label="no message" labelPlacement="end" /> } label="retain" labelPlacement="end" />
) } private history() { const items = this.state.history.reverse().map(message => ({ title: message.topic, value: message.payload, })) return } private didSelectHistoryEntry = (index: number) => { const message = this.state.history[index] this.props.actions.setTopic(message.topic) this.props.actions.setPayload(message.payload) } private editor() { return (
{this.editorMode()} {this.renderEditor()} {this.publishMode()}
) } private renderEditor() { if (this.props.emptyPayload) { return null } return ( ) } } const mapDispatchToProps = (dispatch: any) => { return { actions: bindActionCreators(publishActions, dispatch), } } const mapStateToProps = (state: AppState) => { return { topic: state.publish.topic, payload: state.publish.payload, emptyPayload: state.publish.emptyPayload, editorMode: state.publish.editorMode, retain: state.publish.retain, qos: state.publish.qos, } } export default connect(mapStateToProps, mapDispatchToProps)(Publish)