Add publish history & fix topic input

This commit is contained in:
Thomas Nordquist
2019-01-11 10:17:02 +01:00
parent c956afdee0
commit f7843bc348

View File

@@ -21,16 +21,23 @@ interface Props {
connectionId?: string connectionId?: string
} }
interface Message {
topic: string
payload?: string
sent: Date
}
interface State { interface State {
customTopic?: string customTopic?: string
payload?: string payload?: string
mode: string mode: string
history: Message[]
} }
class Publisher extends React.Component<Props, State> { class Publisher extends React.Component<Props, State> {
constructor(props: any) { constructor(props: any) {
super(props) super(props)
this.state = { mode: 'json' } this.state = { mode: 'json', history: [] }
} }
private updatePayload = (value: string, event?: any) => { private updatePayload = (value: string, event?: any) => {
@@ -47,14 +54,21 @@ class Publisher extends React.Component<Props, State> {
private publish = (e: React.MouseEvent) => { private publish = (e: React.MouseEvent) => {
e.stopPropagation() e.stopPropagation()
if (!this.props.connectionId || !this.state.customTopic) { const topic = this.state.customTopic
return const payload = this.state.payload
if (this.props.connectionId && topic) {
rendererEvents.emit(makePublishEvent(this.props.connectionId), { topic, payload })
this.addMessageToHistory(topic, payload)
}
} }
rendererEvents.emit(makePublishEvent(this.props.connectionId), { private addMessageToHistory(topic: string, payload?: string) {
topic: this.state.customTopic, // Remove duplicates
payload: this.state.payload, 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() { public render() {
@@ -62,13 +76,15 @@ class Publisher extends React.Component<Props, State> {
<div style={{ flexGrow: 1, marginLeft: '8px' }}> <div style={{ flexGrow: 1, marginLeft: '8px' }}>
{this.topic()} {this.topic()}
{this.editor()} {this.editor()}
{this.history()}
</div> </div>
) )
} }
private topic() { private topic() {
const { node } = this.props const { node } = this.props
const topicStr = this.state.customTopic || (node ? node.path() : '') const topicStr = (this.state.customTopic !== undefined) ? this.state.customTopic : (node ? node.path() : '')
return ( return (
<div> <div>
<Typography>Topic</Typography> <Typography>Topic</Typography>
@@ -79,11 +95,18 @@ class Publisher extends React.Component<Props, State> {
value={topicStr} value={topicStr}
multiline={true} multiline={true}
onChange={this.updateTopic} onChange={this.updateTopic}
InputProps={{ onBlur: this.onTopicBlur }}
/> />
</div> </div>
) )
} }
private onTopicBlur = (e: React.FocusEvent<HTMLInputElement>) => {
if (!e.target.value) {
this.setState({ customTopic: undefined })
}
}
private editorOptions = { private editorOptions = {
enableBasicAutocompletion: true, enableBasicAutocompletion: true,
enableLiveAutocompletion: true, enableLiveAutocompletion: true,
@@ -146,6 +169,31 @@ class Publisher extends React.Component<Props, State> {
) )
} }
private loadHistoryEntry(entry: Message) {
this.setState({
customTopic: entry.topic,
payload: entry.payload,
})
}
private history() {
const entries = this.state.history.map(message => (
<Typography onClick={() => this.loadHistoryEntry(message)}>
<div style={{ width: '100%', cursor: 'pointer', marginTop: '8px' }}>
<div><b>{message.topic}</b></div>
<div><i>{message.payload}</i></div>
</div>
</Typography>
))
return (
<div style={{ marginTop: '8px' }}>
<Typography>History</Typography>
{entries}
</div>
)
}
private editor() { private editor() {
return ( return (
<div style={{ width: '100%', display: 'block' }}> <div style={{ width: '100%', display: 'block' }}>